Class: ActiveModel::Validations::TypeValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/active_model/validations/type_validator.rb

Overview

Adds type validation to ActiveModel::Model.

Usage:

Examples:

class FooBar
  include ActiveModel::Model

  attr_accessor :date

  validates :date, type: Date
end

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/active_model/validations/type_validator.rb', line 15

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