Skip to content
This repository
Newer
Older
100644 115 lines (106 sloc) 5.225 kb
87535bd3 » dhh
2008-04-01 Splitting them up first
1 module ActiveModel
3004bc24 » frodsan
2012-05-16 fixing removed empty lines and examples
2
87535bd3 » dhh
2008-04-01 Splitting them up first
3 module Validations
9ac095fe » frodsan
2012-10-25 minor edits and remove mixed titles in AM::Validations docs [ci skip]
4 class FormatValidator < EachValidator # :nodoc:
f1085f41 » josevalim
2009-12-23 Move validations in ActiveModel to validators, however all validatity…
5 def validate_each(record, attribute, value)
58594be6 » sikachu
2011-03-11 Add support for proc or lambda as an option for InclusionValidator, E…
6 if options[:with]
7379114c » josevalim
2011-04-11 Remove code duplication.
7 regexp = option_call(record, :with)
8 record_error(record, attribute, :with, value) if value.to_s !~ regexp
58594be6 » sikachu
2011-03-11 Add support for proc or lambda as an option for InclusionValidator, E…
9 elsif options[:without]
7379114c » josevalim
2011-04-11 Remove code duplication.
10 regexp = option_call(record, :without)
11 record_error(record, attribute, :without, value) if value.to_s =~ regexp
f1085f41 » josevalim
2009-12-23 Move validations in ActiveModel to validators, however all validatity…
12 end
13 end
26392c4a » jeroenvandijk
2010-05-19 Make ActiveModel::Errors#add_on_blank and #add_on_empty accept an opt…
14
0a79eb78 » jamie
2010-01-07 Add validates method as shortcut to setup validators for a given set …
15 def check_validity!
16 unless options.include?(:with) ^ options.include?(:without) # ^ == xor, or "exclusive or"
17 raise ArgumentError, "Either :with or :without must be supplied (but not both)"
18 end
19
7379114c » josevalim
2011-04-11 Remove code duplication.
20 check_options_validity(options, :with)
21 check_options_validity(options, :without)
22 end
23
24 private
25
26 def option_call(record, name)
27 option = options[name]
28 option.respond_to?(:call) ? option.call(record) : option
29 end
30
31 def record_error(record, attribute, name, value)
32 record.errors.add(attribute, :invalid, options.except(name).merge!(:value => value))
33 end
65362af7 » frodsan
2012-06-25 add :nodoc: to internal implementations [ci skip]
34
bc7c0b5c » mrbrdo
2012-06-08 prevent users from unknowingly using bad regexps that can compromise …
35 def regexp_using_multiline_anchors?(regexp)
36 regexp.source.start_with?("^") ||
37 (regexp.source.end_with?("$") && !regexp.source.end_with?("\\$"))
38 end
65362af7 » frodsan
2012-06-25 add :nodoc: to internal implementations [ci skip]
39
7379114c » josevalim
2011-04-11 Remove code duplication.
40 def check_options_validity(options, name)
41 option = options[name]
42 if option && !option.is_a?(Regexp) && !option.respond_to?(:call)
43 raise ArgumentError, "A regular expression or a proc or lambda must be supplied as :#{name}"
bc7c0b5c » mrbrdo
2012-06-08 prevent users from unknowingly using bad regexps that can compromise …
44 elsif option && option.is_a?(Regexp) &&
45 regexp_using_multiline_anchors?(option) && options[:multiline] != true
46 raise ArgumentError, "The provided regular expression is using multiline anchors (^ or $), " \
47 "which may present a security risk. Did you mean to use \\A and \\z, or forgot to add the " \
48 ":multiline => true option?"
0a79eb78 » jamie
2010-01-07 Add validates method as shortcut to setup validators for a given set …
49 end
50 end
f1085f41 » josevalim
2009-12-23 Move validations in ActiveModel to validators, however all validatity…
51 end
52
9131a88b » joshk
2010-05-11 validation macros can now be used within an instance
53 module HelperMethods
95e14d15 » frodsan
2012-05-16 better format and fixes to validators docs
54 # Validates whether the value of the specified attribute is of the correct
55 # form, going by the regular expression provided.You can require that the
56 # attribute matches the regular expression:
87535bd3 » dhh
2008-04-01 Splitting them up first
57 #
58 # class Person < ActiveRecord::Base
68df2302 » frodsan
2012-07-05 update ActiveModel::Validations::HelperMethods documentation [ci skip]
59 # validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create
87535bd3 » dhh
2008-04-01 Splitting them up first
60 # end
61 #
95e14d15 » frodsan
2012-05-16 better format and fixes to validators docs
62 # Alternatively, you can require that the specified attribute does _not_
63 # match the regular expression:
cccb0e6b » mcmire
2009-08-09 Add validates_format_of :without => /regexp/ option [Elliot Winkler, …
64 #
65 # class Person < ActiveRecord::Base
68df2302 » frodsan
2012-07-05 update ActiveModel::Validations::HelperMethods documentation [ci skip]
66 # validates_format_of :email, without: /NOSPAM/
cccb0e6b » mcmire
2009-08-09 Add validates_format_of :without => /regexp/ option [Elliot Winkler, …
67 # end
68 #
95e14d15 » frodsan
2012-05-16 better format and fixes to validators docs
69 # You can also provide a proc or lambda which will determine the regular
70 # expression that will be used to validate the attribute.
58594be6 » sikachu
2011-03-11 Add support for proc or lambda as an option for InclusionValidator, E…
71 #
72 # class Person < ActiveRecord::Base
73 # # Admin can have number as a first letter in their screen name
95e14d15 » frodsan
2012-05-16 better format and fixes to validators docs
74 # validates_format_of :screen_name,
70c4676d » frodsan
2012-07-05 fix syntax of AM::Validations::HelperMethods examples [ci skip]
75 # with: ->(person) { person.admin? ? /\A[a-z0-9][a-z0-9_\-]*\z/i : /\A[a-z][a-z0-9_\-]*\z/i }
58594be6 » sikachu
2011-03-11 Add support for proc or lambda as an option for InclusionValidator, E…
76 # end
77 #
95e14d15 » frodsan
2012-05-16 better format and fixes to validators docs
78 # Note: use <tt>\A</tt> and <tt>\Z</tt> to match the start and end of the
79 # string, <tt>^</tt> and <tt>$</tt> match the start/end of a line.
87535bd3 » dhh
2008-04-01 Splitting them up first
80 #
68df2302 » frodsan
2012-07-05 update ActiveModel::Validations::HelperMethods documentation [ci skip]
81 # Due to frequent misuse of <tt>^</tt> and <tt>$</tt>, you need to pass
82 # the <tt>multiline: true</tt> option in case you use any of these two
83 # anchors in the provided regular expression. In most cases, you should be
84 # using <tt>\A</tt> and <tt>\z</tt>.
bc7c0b5c » mrbrdo
2012-06-08 prevent users from unknowingly using bad regexps that can compromise …
85 #
95e14d15 » frodsan
2012-05-16 better format and fixes to validators docs
86 # You must pass either <tt>:with</tt> or <tt>:without</tt> as an option.
87 # In addition, both must be a regular expression or a proc or lambda, or
88 # else an exception will be raised.
87535bd3 » dhh
2008-04-01 Splitting them up first
89 #
90 # Configuration options:
8828b2ca » lifo
2009-03-19 Move all the Active Record validations to Active Model
91 # * <tt>:message</tt> - A custom error message (default is: "is invalid").
68df2302 » frodsan
2012-07-05 update ActiveModel::Validations::HelperMethods documentation [ci skip]
92 # * <tt>:allow_nil</tt> - If set to true, skips this validation if the
93 # attribute is +nil+ (default is +false+).
95e14d15 » frodsan
2012-05-16 better format and fixes to validators docs
94 # * <tt>:allow_blank</tt> - If set to true, skips this validation if the
95 # attribute is blank (default is +false+).
96 # * <tt>:with</tt> - Regular expression that if the attribute matches will
68df2302 » frodsan
2012-07-05 update ActiveModel::Validations::HelperMethods documentation [ci skip]
97 # result in a successful validation. This can be provided as a proc or
95e14d15 » frodsan
2012-05-16 better format and fixes to validators docs
98 # lambda returning regular expression which will be called at runtime.
68df2302 » frodsan
2012-07-05 update ActiveModel::Validations::HelperMethods documentation [ci skip]
99 # * <tt>:without</tt> - Regular expression that if the attribute does not
100 # match will result in a successful validation. This can be provided as
101 # a proc or lambda returning regular expression which will be called at
102 # runtime.
bc7c0b5c » mrbrdo
2012-06-08 prevent users from unknowingly using bad regexps that can compromise …
103 # * <tt>:multiline</tt> - Set to true if your regular expression contains
104 # anchors that match the beginning or end of lines as opposed to the
105 # beginning or end of the string. These anchors are <tt>^</tt> and <tt>$</tt>.
c175563b » bogdan
2012-07-10 AM::Validations: remove documentation duplicates
106 #
107 # There is also a list of default options supported by every validator:
108 # +:if+, +:unless+, +:on+ and +:strict+.
109 # See <tt>ActiveModel::Validation#validates</tt> for more information
87535bd3 » dhh
2008-04-01 Splitting them up first
110 def validates_format_of(*attr_names)
0a79eb78 » jamie
2010-01-07 Add validates method as shortcut to setup validators for a given set …
111 validates_with FormatValidator, _merge_attributes(attr_names)
87535bd3 » dhh
2008-04-01 Splitting them up first
112 end
113 end
114 end
28f36279 » josh
2009-06-08 Properly require ActiveModel validation dependencies
115 end
Something went wrong with that request. Please try again.