Sha256: c496bad194365cc75dfa3714ad267fc1bf07bc86a4917f157b7ca06726417955

Contents?: true

Size: 696 Bytes

Versions: 4

Compression:

Stored size: 696 Bytes

Contents

require "active_model"

module SwifterEnum
  class SwifterEnumValidator < ActiveModel::EachValidator
    def validate_each(record, attribute, a_value)
      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

      if a_value.value.nil?
        return if options[:allow_nil]

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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
swifter_enum-0.9.5 lib/swifter_enum/swifter_enum_validator.rb
swifter_enum-0.9.4 lib/swifter_enum/swifter_enum_validator.rb
swifter_enum-0.9.3 lib/swifter_enum/swifter_enum_validator.rb
swifter_enum-0.9.2 lib/swifter_enum/swifter_enum_validator.rb