Class: ActiveModel::Validations::TypeValidator
- Inherits:
-
EachValidator
- Object
- EachValidator
- ActiveModel::Validations::TypeValidator
- Defined in:
- lib/validates_type.rb
Instance Method Summary collapse
-
#initialize(options) ⇒ TypeValidator
constructor
Set default message for failure here.
-
#validate_each(record, attribute, value) ⇒ Object
Validate that the value is the type we expect.
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() = { message: :type }.merge() super() 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([:type]) add_errors_or_raise(, record, attribute) unless value_to_test.is_a?(expected_type) end |