Class: ValidatesStructure::Validator::NestedValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/validates-structure.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/validates-structure.rb', line 160

def validate_each(record, attribute, value)
  return unless value.is_a?(Hash)
  return if record.errors.keys.map(&:to_s).include?(attribute)

  validator = record.class.nested_validators[attribute]
  return if validator.nil?

  nested = validator.new(value)
  nested.valid?
  nested.errors.each do |nested_attribute, message|
    record.errors.add("#{attribute}/#{nested_attribute}", message)
  end
end