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
Instance Attribute Summary
Instance Method Summary
- #check_validity!
- #validate_each(record, attribute, value)
- #check_options_validity(name) private
- #record_error(record, attribute, name, value) private
- #regexp_using_multiline_anchors?(regexp) ⇒ Boolean private
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
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 (name) if option = [name] if option.is_a?(Regexp) if [: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 .include?(:with) ^ .include?(:without) # ^ == xor, or "exclusive or" raise ArgumentError, "Either :with or :without must be supplied (but not both)" end :with :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, ** .except(name).merge!(value: value)) end
#regexp_using_multiline_anchors?(regexp) ⇒ Boolean
(private)
# 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 [:with] regexp = resolve_value(record, [:with]) record_error(record, attribute, :with, value) unless regexp.match?(value.to_s) elsif [:without] regexp = resolve_value(record, [:without]) record_error(record, attribute, :without, value) if regexp.match?(value.to_s) end end