Method: Attributor::Type::ClassMethods#validate
- Defined in:
- lib/attributor/type.rb
#validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, attribute) ⇒ Object
TODO: refactor this to take just the options instead of the full attribute? TODO: delegate to subclass
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/attributor/type.rb', line 98 def validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, attribute) # rubocop:disable Style/OptionalArguments errors = [] attribute..each do |option, opt_definition| case option when :max errors << "#{Attributor.humanize_context(context)} value (#{value}) is larger than the allowed max (#{opt_definition.inspect})" unless value <= opt_definition when :min errors << "#{Attributor.humanize_context(context)} value (#{value}) is smaller than the allowed min (#{opt_definition.inspect})" unless value >= opt_definition when :regexp errors << "#{Attributor.humanize_context(context)} value (#{value}) does not match regexp (#{opt_definition.inspect})" unless value =~ opt_definition end end errors end |