123456789_123456789_123456789_123456789_123456789_

Class: ActiveModel::Validations::FormatValidator

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

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

This class inherits a constructor from ActiveModel::EachValidator

Instance Method Details

#check_options_validity(name) (private)

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations/format.rb', line 34

def check_options_validity(name)
  if option = options[name]
    if option.is_a?(Regexp)
      if options[:multiline] != true && regexp_using_multiline_anchors?(option)
        raise ArgumentError, "The provided regular expression is using multiline anchors (^ or $), " \
        "which may present a security risk. Did you mean to use \\A and \\z, or forgot to add the " \
        ":multiline => true option?"
      end
    elsif !option.respond_to?(:call)
      raise ArgumentError, "A regular expression or a proc or lambda must be supplied as :#{name}"
    end
  end
end

#check_validity!

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations/format.rb', line 20

def check_validity!
  unless options.include?(:with) ^ options.include?(:without)  # ^ == xor, or "exclusive or"
    raise ArgumentError, "Either :with or :without must be supplied (but not both)"
  end

  check_options_validity :with
  check_options_validity :without
end

#record_error(record, attribute, name, value) (private)

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations/format.rb', line 30

def record_error(record, attribute, name, value)
  record.errors.add(attribute, :invalid, **options.except(name).merge!(value: value))
end

#regexp_using_multiline_anchors?(regexp) ⇒ Boolean (private)

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations/format.rb', line 48

def regexp_using_multiline_anchors?(regexp)
  source = regexp.source
  source.start_with?("^") || (source.end_with?("$") && !source.end_with?("\\$"))
end

#validate_each(record, attribute, value)

[ GitHub ]

  
# File 'activemodel/lib/active_model/validations/format.rb', line 10

def validate_each(record, attribute, value)
  if options[:with]
    regexp = resolve_value(record, options[:with])
    record_error(record, attribute, :with, value) unless regexp.match?(value.to_s)
  elsif options[:without]
    regexp = resolve_value(record, options[:without])
    record_error(record, attribute, :without, value) if regexp.match?(value.to_s)
  end
end