Class: BlockKit::Validators::ArrayInclusionValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/block_kit/validators/array_inclusion_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/block_kit/validators/array_inclusion_validator.rb', line 36

def validate_each(record, attribute, value)
  values = Array(value) || []
  not_allowed_values = []

  if options[:in]
    not_allowed_values.concat(values - options[:in])
  end

  if options[:proc]
    not_allowed_values.concat(values.find_all { |v| !options[:proc].call(v) })
  end

  unless not_allowed_values.blank?
    formatted_rejected = not_allowed_values.uniq.collect(&:inspect).join(",")
    record.errors.add(attribute, :inclusion, **options.except(:in).merge!(rejected_values: formatted_rejected, value: value, invalid_values: not_allowed_values))
  end
end