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
-
CHECKS =
# File 'activemodel/lib/active_model/validations/length.rb', line 11{ is: :==, minimum: :>=, maximum: :<= }.freeze -
MESSAGES =
# File 'activemodel/lib/active_model/validations/length.rb', line 10{ is: :wrong_length, minimum: :too_short, maximum: :too_long }.freeze -
RESERVED_OPTIONS =
# File 'activemodel/lib/active_model/validations/length.rb', line 13[:minimum, :maximum, :within, :is, :too_short, :too_long]
Class Method Summary
- .new(options) ⇒ LengthValidator constructor
::ActiveModel::EachValidator - Inherited
| .new | Returns a new validator instance. |
::ActiveModel::Validator - Inherited
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. |
| #allow_blank?, #allow_nil?, #prepare_value_for_validation | |
::ActiveModel::Validator - Inherited
Constructor Details
.new(options) ⇒ LengthValidator
# File 'activemodel/lib/active_model/validations/length.rb', line 15
def initialize() if range = (.delete(:in) || .delete(:within)) raise ArgumentError, ":in and :within must be a Range" unless range.is_a?(Range) [:minimum] = range.min if range.begin [:maximum] = (range.exclude_end? ? range.end - 1 : range.end) if range.end end if [:allow_blank] == false && [:minimum].nil? && [:is].nil? [: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 & .keys if keys.empty? raise ArgumentError, "Range unspecified. Specify the :in, :within, :maximum, :minimum, or :is option." end keys.each do |key| value = [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)
# File 'activemodel/lib/active_model/validations/length.rb', line 69
def skip_nil_check?(key) key == :maximum && [:allow_nil].nil? && [: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 = .except(*RESERVED_OPTIONS) CHECKS.each do |key, validity_check| next unless check_value = [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 [:count] = check_value = [MESSAGES[key]] [:] ||= if record.errors.add(attribute, MESSAGES[key], **) end end