Class: Quby::ArrayAttributeValidValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/active_model_modules/array_attribute_valid_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/active_model_modules/array_attribute_valid_validator.rb', line 2

def validate_each(record, attribute, value)
  value&.each&.with_index do |element, index|
    if !element.respond_to?(:valid?)
      record.errors.add(attribute, "element ##{index} does not respond_to valid?")
    elsif !element.valid?
      record.errors.add(attribute, "element ##{index} #{element.errors.full_messages.join(', ')}")
    end
  end
end