123456789_123456789_123456789_123456789_123456789_

Class: ActiveModel::Validations::LengthValidator

Do not use. This class is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: ActiveModel::EachValidator
Defined in: activemodel/lib/active_model/validations/length.rb

Constant Summary

Class Method Summary

::ActiveModel::EachValidator - Inherited

.new

Returns a new validator instance.

::ActiveModel::Validator - Inherited

.kind

Returns the kind of the validator.

.new

Accepts options that will be made available through the options reader.

Instance Attribute Summary

Instance Method Summary

ResolveValue - Included

::ActiveModel::EachValidator - Inherited

#check_validity!

Hook method that gets called by the initializer allowing verification that the arguments supplied are valid.

#validate

Performs validation on the supplied record.

#validate_each

Override this method in subclasses with the validation logic, adding errors to the records #errors array where necessary.

#prepare_value_for_validation

::ActiveModel::Validator - Inherited

#kind

Returns the kind for this validator.

#validate

Override this method in subclasses with validation logic, adding errors to the records #errors array where necessary.

Constructor Details

.new(options) ⇒ LengthValidator

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations/length.rb', line 15

def initialize(options)
  if range = (options.delete(:in) || options.delete(:within))
    raise ArgumentError, ":in and :within must be a Range" unless range.is_a?(Range)
    options[:minimum] = range.min if range.begin
    options[:maximum] = (range.exclude_end? ? range.end - 1 : range.end) if range.end
  end

  if options[:allow_blank] == false && options[:minimum].nil? && options[:is].nil?
    options[:minimum] = 1
  end

  super
end

Instance Method Details

#check_validity!

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations/length.rb', line 29

def check_validity!
  keys = CHECKS.keys & options.keys

  if keys.empty?
    raise ArgumentError, "Range unspecified. Specify the :in, :within, :maximum, :minimum, or :is option."
  end

  keys.each do |key|
    value = options[key]

    unless (value.is_a?(Integer) && value >= 0) ||
            value == Float::INFINITY || value == -Float::INFINITY ||
            value.is_a?(Symbol) || value.is_a?(Proc)
      raise ArgumentError, ":#{key} must be a non-negative Integer, Infinity, Symbol, or Proc"
    end
  end
end

#skip_nil_check?(key) ⇒ Boolean (private)

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations/length.rb', line 69

def skip_nil_check?(key)
  key == :maximum && options[:allow_nil].nil? && options[:allow_blank].nil?
end

#validate_each(record, attribute, value)

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations/length.rb', line 47

def validate_each(record, attribute, value)
  value_length = value.respond_to?(:length) ? value.length : value.to_s.length
  errors_options = options.except(*RESERVED_OPTIONS)

  CHECKS.each do |key, validity_check|
    next unless check_value = options[key]

    if !value.nil? || skip_nil_check?(key)
      check_value = resolve_value(record, check_value)
      next if value_length.public_send(validity_check, check_value)
    end

    errors_options[:count] = check_value

    default_message = options[MESSAGES[key]]
    errors_options[:message] ||= default_message if default_message

    record.errors.add(attribute, MESSAGES[key], **errors_options)
  end
end