module TTY::Option::ParamValidation
Public Class Methods
call(param, values)
click to toggle source
Validate parameter value against validation rule
@example
param = Parameter::Option.create(:foo, validate: '\d+') ParamValidation[param, "12"] # => "12"
@api public
# File lib/tty/option/param_validation.rb, line 15 def call(param, values) return Result.success(values) unless param.validate? errors = [] result = Array(values).reduce([]) do |acc, value| valid = case param.validate when Proc param.validate.(value) when Regexp !param.validate.match(value.to_s).nil? end if valid acc << value else errors << TTY::Option::InvalidArgument.new(param, value) end acc end if errors.empty? Result.success(result.size <= 1 ? result.first : result) else Result.failure(errors) end end
Also aliased as: []
Private Instance Methods
call(param, values)
click to toggle source
Validate parameter value against validation rule
@example
param = Parameter::Option.create(:foo, validate: '\d+') ParamValidation[param, "12"] # => "12"
@api public
# File lib/tty/option/param_validation.rb, line 15 def call(param, values) return Result.success(values) unless param.validate? errors = [] result = Array(values).reduce([]) do |acc, value| valid = case param.validate when Proc param.validate.(value) when Regexp !param.validate.match(value.to_s).nil? end if valid acc << value else errors << TTY::Option::InvalidArgument.new(param, value) end acc end if errors.empty? Result.success(result.size <= 1 ? result.first : result) else Result.failure(errors) end end
Also aliased as: []