Sha256: cade2b41829ea100a642e3510b66a424f601356210aa9655f0c74c41183c5958

Contents?: true

Size: 778 Bytes

Versions: 6

Compression:

Stored size: 778 Bytes

Contents

module ActiveModel
  module Validations
    # Adds type validation to ActiveModel::Model.
    #
    # Usage:
    # @example
    #   class FooBar
    #     include ActiveModel::Model
    #
    #     attr_accessor :date
    #
    #     validates :date, type: Date
    #   end
    class TypeValidator < ActiveModel::EachValidator
      def validate_each(record, attribute, value)
        classes = Array(options[:with] || options[:in])
        unless classes.any? { |klass| value.is_a? klass }
          allowed_classes = classes.to_sentence two_words_connector: " or ",
                                                last_word_connector: ", or "

          record.errors[attribute] << (options[:message] || "must be a #{allowed_classes}")
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ph_model-1.1.1 lib/active_model/validations/type_validator.rb
ph_model-1.1.0 lib/active_model/validations/type_validator.rb
ph_model-1.0.2 lib/active_model/validations/type_validator.rb
ph_model-1.0.1 lib/active_model/validations/type_validator.rb
ph_model-1.0.0 lib/active_model/validations/type_validator.rb
ph_model-0.0.1 lib/active_model/validations/type_validator.rb