Class: SwifterEnum::SwifterEnumValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/swifter_enum/swifter_enum_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, a_value) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/swifter_enum/swifter_enum_validator.rb', line 5

def validate_each(record, attribute, a_value)
  if a_value.nil?
    return if options[:allow_nil]

    record.errors.add(attribute, "nil value for #{attribute} is not allowed")
    return
  end

  if !a_value.is_a?(SwifterEnum::Base) || a_value.instance_of?(SwifterEnum::Base)
    record.errors.add(attribute, "#{a_value.value} is not a valid subclass of SwifterEnum")
  end

  unless a_value.class.values.key?(a_value.value)
    record.errors.add(attribute, "#{a_value.value} is not a valid #{attribute} type")
  end
end