Class: ValidityValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/tram/validators/validity_validator.rb

Overview

Checks that value of some attribute is valid per se Otherwise populates the record with the attribute’s validation errors

With option ‘original_keys: true` it copies value’s errors to the record under their original keys, as if the [#record] had attributes of the [#value]. Otherwise the errors are copied under the [#attribute] key.

This is necessary for validation of the form models, as well as other decorated objects.

Examples:

Checks that user.valid? returns true

validates :user, validity: { nested_keys: true }

Instance Method Summary collapse

Instance Method Details

#validate_each(record, key, value) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/tram/validators/validity_validator.rb', line 15

def validate_each(record, key, value)
  if !value.respond_to? :valid?
    record.errors
          .add attribute, :valid, record: record, attribute: key, value: value
  elsif !value.valid?
    Tram::Validators.copy_errors(value, record, key, :valid, value, options)
  end
end