Sha256: a5af3a874be95dd9bad96651fe4cc9c83b8b98d8ee208d526525374a0b67d1b6

Contents?: true

Size: 383 Bytes

Versions: 1

Compression:

Stored size: 383 Bytes

Contents

require 'active_model'

class TypeValidator < ActiveModel::EachValidator
  def options
    super.merge(allow_nil: true)
  end

  def klasses
    options[:in] || [options[:with]]
  end

  def validate_each(record, attribute, value)
    unless klasses.any? { |klass| value.is_a?(klass) }
      record.errors.add(attribute, "must be a #{klasses}, not #{value.class}")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
minarai-0.0.3 lib/validator/type_validator.rb