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



17
18
19
20
21
22
23
# File 'lib/validates_type.rb', line 17

def initialize(options)
  merged_options = {
    :message => "is expected to be a #{ symbol_class(options[:type]) } and is not."
  }.merge(options)

  super(merged_options)
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object

Validate that the value is the type we expect



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

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

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