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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/attributor/type.rb', line 43

def validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, attribute) # rubocop:disable Style/OptionalArguments
  errors = []
  attribute.options.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