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
25
26
# 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



35
36
37
38
39
40
# File 'lib/validates_type.rb', line 35

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