Class: ActiveModel::Validations::TypeValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/validates_type.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TypeValidator

Set default message for failure here



20
21
22
23
24
# File 'lib/validates_type.rb', line 20

def initialize(options)
  merged_options = { message: :type }.merge(options)

  super(merged_options)
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object

Validate that the value is the type we expect



33
34
35
36
37
38
# File 'lib/validates_type.rb', line 33

def validate_each(record, attribute, value)
  value_to_test = type_before_coercion(record, attribute, value)
  expected_type = type_class(options[:type])

  add_errors_or_raise(options, record, attribute) unless value_to_test.is_a?(expected_type)
end