Skip to content

Commit

Permalink
Add validates_format_of :without => /regexp/ option [Elliot Winkler, …
Browse files Browse the repository at this point in the history
…Peer Allan]

[#430 state:resolved]

  Example :

    validates_format_of :subdomain, :without => /www|admin|mail/

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
mcmire authored and lifo committed Aug 10, 2009
1 parent 600a89f commit cccb0e6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
6 changes: 6 additions & 0 deletions activemodel/CHANGELOG
@@ -1,5 +1,11 @@
*Edge* *Edge*


* Add validates_format_of :without => /regexp/ option. #430 [Elliot Winkler, Peer Allan]

Example :

validates_format_of :subdomain, :without => /www|admin|mail/

* Introduce validates_with to encapsulate attribute validations in a class. #2630 [Jeff Dean] * Introduce validates_with to encapsulate attribute validations in a class. #2630 [Jeff Dean]


* Extracted from Active Record and Active Resource. * Extracted from Active Record and Active Resource.
37 changes: 29 additions & 8 deletions activemodel/lib/active_model/validations/format.rb
@@ -1,22 +1,30 @@
module ActiveModel module ActiveModel
module Validations module Validations
module ClassMethods module ClassMethods
# Validates whether the value of the specified attribute is of the correct form by matching it against the regular expression # Validates whether the value of the specified attribute is of the correct form, going by the regular expression provided.
# provided. # You can require that the attribute matches the regular expression:
# #
# class Person < ActiveRecord::Base # class Person < ActiveRecord::Base
# validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create # validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
# end # end
# #
# Alternatively, you can require that the specified attribute does _not_ match the regular expression:
#
# class Person < ActiveRecord::Base
# validates_format_of :email, :without => /NOSPAM/
# end
#
# Note: use <tt>\A</tt> and <tt>\Z</tt> to match the start and end of the string, <tt>^</tt> and <tt>$</tt> match the start/end of a line. # Note: use <tt>\A</tt> and <tt>\Z</tt> to match the start and end of the string, <tt>^</tt> and <tt>$</tt> match the start/end of a line.
# #
# A regular expression must be provided or else an exception will be raised. # You must pass either <tt>:with</tt> or <tt>:without</tt> as an option. In addition, both must be a regular expression,
# or else an exception will be raised.
# #
# Configuration options: # Configuration options:
# * <tt>:message</tt> - A custom error message (default is: "is invalid"). # * <tt>:message</tt> - A custom error message (default is: "is invalid").
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+). # * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+). # * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
# * <tt>:with</tt> - The regular expression used to validate the format with (note: must be supplied!). # * <tt>:with</tt> - Regular expression that if the attribute matches will result in a successful validation.
# * <tt>:without</tt> - Regular expression that if the attribute does not match will result in a successful validation.
# * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>). # * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).
# * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
# occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The # occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
Expand All @@ -25,13 +33,26 @@ module ClassMethods
# not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
# method, proc or string should return or evaluate to a true or false value. # method, proc or string should return or evaluate to a true or false value.
def validates_format_of(*attr_names) def validates_format_of(*attr_names)
configuration = { :with => nil } configuration = attr_names.extract_options!
configuration.update(attr_names.extract_options!)
unless configuration.include?(:with) ^ configuration.include?(:without) # ^ == xor, or "exclusive or"
raise ArgumentError, "Either :with or :without must be supplied (but not both)"
end


raise(ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash") unless configuration[:with].is_a?(Regexp) if configuration[:with] && !configuration[:with].is_a?(Regexp)
raise ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash"
end

if configuration[:without] && !configuration[:without].is_a?(Regexp)
raise ArgumentError, "A regular expression must be supplied as the :without option of the configuration hash"
end


validates_each(attr_names, configuration) do |record, attr_name, value| validates_each(attr_names, configuration) do |record, attr_name, value|
unless value.to_s =~ configuration[:with] if configuration[:with] && value.to_s !~ configuration[:with]
record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value)
end

if configuration[:without] && value.to_s =~ configuration[:without]
record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value) record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value)
end end
end end
Expand Down
29 changes: 29 additions & 0 deletions activemodel/test/cases/validations/format_validation_test.rb
Expand Up @@ -71,6 +71,35 @@ def test_validate_format_with_formatted_message
assert_equal ["can't be Invalid title"], t.errors[:title] assert_equal ["can't be Invalid title"], t.errors[:title]
end end


def test_validate_format_with_not_option
Topic.validates_format_of(:title, :without => /foo/, :message => "should not contain foo")
t = Topic.new

t.title = "foobar"
t.valid?
assert_equal ["should not contain foo"], t.errors[:title]

t.title = "something else"
t.valid?
assert_equal [], t.errors[:title]
end

def test_validate_format_of_without_any_regexp_should_raise_error
assert_raise(ArgumentError) { Topic.validates_format_of(:title) }
end

def test_validates_format_of_with_both_regexps_should_raise_error
assert_raise(ArgumentError) { Topic.validates_format_of(:title, :with => /this/, :without => /that/) }
end

def test_validates_format_of_when_with_isnt_a_regexp_should_raise_error
assert_raise(ArgumentError) { Topic.validates_format_of(:title, :with => "clearly not a regexp") }
end

def test_validates_format_of_when_not_isnt_a_regexp_should_raise_error
assert_raise(ArgumentError) { Topic.validates_format_of(:title, :without => "clearly not a regexp") }
end

def test_validates_format_of_with_custom_error_using_quotes def test_validates_format_of_with_custom_error_using_quotes
repair_validations(Developer) do repair_validations(Developer) do
Developer.validates_format_of :name, :with => /^(A-Z*)$/, :message=> "format 'single' and \"double\" quotes" Developer.validates_format_of :name, :with => /^(A-Z*)$/, :message=> "format 'single' and \"double\" quotes"
Expand Down

1 comment on commit cccb0e6

@booch
Copy link

@booch booch commented on cccb0e6 Aug 13, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs should mention that :with or :without cannot both be specified. (The ArgumentError in the code does a good job of this.) Also, the doc line "both must be a regular expression" is very confusing. It should probably be rewritten to something like:

You must pass either :with or :without as an option, but not both.

The argument to :with or :without must be a regular expression.

There's no real need to explicitly state that an exception will be raised, since "must" already implies that.

Please sign in to comment.