Class: CsvValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/active_validation/validators/csv_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_validation/validators/csv_validator.rb', line 22

def validate_each(record, attribute, value)
  assert_valid_options!
  values = parse_values(record, attribute, value)

  if values.nil?
    record.errors.add(attribute, 'not a valid csv')
    return
  end

  options.slice(*CHECKS.keys).each do |option, option_value|
    option_value = option_value.call(record) if option_value.is_a?(Proc)

    next unless values.any? { |val| !valid_size?(val, option, option_value) }

    error_text = filtered_options(values).merge!(detect_error_options(option_value))
    error_text = options[:message] ||
                 I18n.t("active_validation.errors.messages.csv.#{option}", error_text)
    record.errors[attribute] << (options[:message] || error_text)
  end
end