aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2004-12-16 17:00:22 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2004-12-16 17:00:22 +0000
commitd2b75a083af8db6704d195e1d3a3035cdcb65c99 (patch)
tree256e1e31e79dd4424952601aaf6e4b60e8d24166 /activerecord
parentf033833fb9f48927995e8bc521ae032888813989 (diff)
downloadrails-d2b75a083af8db6704d195e1d3a3035cdcb65c99.tar.gz
rails-d2b75a083af8db6704d195e1d3a3035cdcb65c99.tar.bz2
rails-d2b75a083af8db6704d195e1d3a3035cdcb65c99.zip
Added Base.validates_inclusion_of
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@192 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG24
-rwxr-xr-xactiverecord/lib/active_record/validations.rb74
-rwxr-xr-xactiverecord/test/validations_test.rb133
3 files changed, 201 insertions, 30 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index a503e27e55..bd6a856285 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -60,13 +60,25 @@
validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/, :on => :create
end
-* Added Base.validates_boundaries_of that delegates to add_on_boundary_breaking #312 [Tobias Luetke]. Example:
-
- class Person < ActiveRecord::Base
- validates_boundries_of :password, :password_confirmation
- validates_boundries_of :user_name, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name"
- end
+* Added Base.validates_length_of that delegates to add_on_boundary_breaking #312 [Tobias Luetke]. Example:
+ Validates that the specified attribute matches the length restrictions supplied in either:
+
+ - configuration[:minimum]
+ - configuration[:maximum]
+ - configuration[:is]
+ - configuration[:within] (aka. configuration[:in])
+
+ Only one option can be used at a time.
+
+ class Person < ActiveRecord::Base
+ validates_length_of :first_name, :maximum=>30
+ validates_length_of :last_name, :maximum=>30, :message=>"less than %d if you don't mind"
+ validates_length_of :user_name, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name"
+ validates_length_of :fav_bra_size, :minimum=>1, :too_short=>"please enter at least %d character"
+ validates_length_of :smurf_leader, :is=>4, :message=>"papa is spelled with %d characters... don't play me."
+ end
+
* Added Base.validate_presence as an alternative to implementing validate and doing errors.add_on_empty yourself.
* Added Base.validates_uniqueness_of that alidates whether the value of the specified attributes are unique across the system.
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 95e1cc4b1e..0585ddbb90 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -123,25 +123,77 @@ module ActiveRecord
end
end
- # Validates that the specified attributes are within the boundary defined in configuration[:within]. Happens by default on both create and update.
+ # Validates that the specified attribute matches the length restrictions supplied in either:
+ #
+ # - configuration[:minimum]
+ # - configuration[:maximum]
+ # - configuration[:is]
+ # - configuration[:within] (aka. configuration[:in])
+ #
+ # Only one option can be used at a time.
#
# class Person < ActiveRecord::Base
- # validates_boundaries_of :password, :password_confirmation
- # validates_boundaries_of :user_name, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name"
+ # validates_length_of :first_name, :maximum=>30
+ # validates_length_of :last_name, :maximum=>30, :message=>"less than %d if you don't mind"
+ # validates_length_of :user_name, :within => 6..20, :too_long => "pick a shorter name", :too_short => "pick a longer name"
+ # validates_length_of :fav_bra_size, :minimum=>1, :too_short=>"please enter at least %d character"
+ # validates_length_of :smurf_leader, :is=>4, :message=>"papa is spelled with %d characters... don't play me."
# end
#
# Configuration options:
- # ::within: The range that constitutes the boundary (default is: 6..20)
- # ::too_long: The error message if the attributes go over the boundary (default is: "is too long (max is %d characters)")
- # ::too_short: The error message if the attributes go under the boundary (default is: "is too short (min is %d characters)")
+ # ::minimum: The minimum size of the attribute
+ # ::maximum: The maximum size of the attribute
+ # ::is: The exact size of the attribute
+ # ::within: A range specifying the minimum and maximum size of the attribute
+ # ::in: A synonym(or alias) for :within
+ #
+ # ::too_long: The error message if the attribute goes over the maximum (default is: "is too long (max is %d characters)")
+ # ::too_short: The error message if the attribute goes under the minimum (default is: "is too short (min is %d characters)")
+ # ::wrong_length: The error message if using the :is method and the attribute is the wrong size (default is: "is the wrong length (should be %d characters)")
+ # ::message: The error message to use for a :minimum, :maximum, or :is violation. An alias of the appropriate too_long/too_short/wrong_length message
# ::on: Specifies when this validation is active (default is :save, other options :create, :update)
- def validates_boundaries_of(*attr_names)
- configuration = { :within => 5..20, :too_long => ActiveRecord::Errors.default_error_messages[:too_long], :too_short => ActiveRecord::Errors.default_error_messages[:too_short], :on => :save }
+ def validates_length_of(*attr_names)
+ configuration = { :too_long => ActiveRecord::Errors.default_error_messages[:too_long], :too_short => ActiveRecord::Errors.default_error_messages[:too_short], :wrong_length => ActiveRecord::Errors.default_error_messages[:wrong_length], :on => :save }
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
+ # you must use one of 4 options, :within, :maximum, :minimum, or :is
+ within = configuration[:within] || configuration[:in]
+ maximum = configuration[:maximum]
+ minimum = configuration[:minimum]
+ is = configuration[:is]
+
+ raise(ArgumentError, "The :within, :maximum, :minimum, or :is options must be passed in the configuration hash") unless within or maximum or minimum or is
+ # but not more than 1 of them at a time
+ options_used = 0
+ options_used += 1 if within
+ options_used += 1 if maximum
+ options_used += 1 if minimum
+ options_used += 1 if is
+ raise(ArgumentError, "The :within, :maximum, :minimum, and :is options are mutually exclusive") if options_used > 1
+
+ option_to_use = within || maximum || minimum || is
for attr_name in attr_names
- class_eval(%(#{validation_method(configuration[:on])} %{errors.add_on_boundary_breaking('#{attr_name}', #{configuration[:within]}, "#{configuration[:too_long]}", "#{configuration[:too_short]}")}))
+ if within
+ raise(ArgumentError, "The :within option must be a Range") unless within.kind_of?(Range)
+ class_eval(%(#{validation_method(configuration[:on])} %{errors.add_on_boundary_breaking('#{attr_name}', #{within}, "#{configuration[:too_long]}", "#{configuration[:too_short]}")}))
+ elsif maximum
+ raise(ArgumentError, "The :maximum option must be a Fixnum") unless maximum.kind_of?(Fixnum)
+ msg = configuration[:message] || configuration[:too_long]
+ msg = (msg % maximum) rescue msg
+ class_eval(%(#{validation_method(configuration[:on])} %{errors.add( '#{attr_name}', '#{msg}') if #{attr_name}.to_s.length > #{maximum} }))
+ elsif minimum
+ raise(ArgumentError, "The :minimum option must be a Fixnum") unless minimum.kind_of?(Fixnum)
+ msg = configuration[:message] || configuration[:too_short]
+ msg = (msg % minimum) rescue msg
+ class_eval(%(#{validation_method(configuration[:on])} %{errors.add( '#{attr_name}', '#{msg}') if #{attr_name}.to_s.length < #{minimum} }))
+ else
+ raise(ArgumentError, "The :is option must be a Fixnum") unless is.kind_of?(Fixnum)
+ msg = configuration[:message] || configuration[:wrong_length]
+ msg = (msg % is) rescue msg
+ class_eval(%(#{validation_method(configuration[:on])} %{errors.add( '#{attr_name}', '#{msg}') if #{attr_name}.to_s.length != #{is} }))
+ end
end
+
end
# Validates whether the value of the specified attributes are unique across the system. Useful for making sure that only one user
@@ -202,7 +254,7 @@ module ActiveRecord
def validates_inclusion_of(*attr_names)
configuration = { :message => ActiveRecord::Errors.default_error_messages[:inclusion], :on => :save }
configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)
- enum = configuration[:in]
+ enum = configuration[:in] || configuration[:within]
raise(ArgumentError, "An object with the method include? is required must be supplied as the :in option of the configuration hash") unless enum.respond_to?("include?")
@@ -211,6 +263,7 @@ module ActiveRecord
end
end
+
private
def validation_method(on)
case on
@@ -319,6 +372,7 @@ module ActiveRecord
:empty => "can't be empty",
:too_long => "is too long (max is %d characters)",
:too_short => "is too short (min is %d characters)",
+ :wrong_length => "is the wrong length (should be %d characters)",
:taken => "has already been taken",
}
cattr_accessor :default_error_messages
diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb
index 92c0a71c35..b6c9cfed7d 100755
--- a/activerecord/test/validations_test.rb
+++ b/activerecord/test/validations_test.rb
@@ -189,20 +189,6 @@ class ValidationsTest < Test::Unit::TestCase
t2.title = "Now Im really also unique"
assert t2.save, "Should now save t2 as unique"
end
-
- def test_validate_boundaries
- Topic.validates_boundaries_of(:title, :content, :within => 3..5)
-
- t = Topic.create("title" => "a!", "content" => "I'm ooooooooh so very long")
- assert !t.save
- assert_equal "is too short (min is 3 characters)", t.errors.on(:title)
- assert_equal "is too long (max is 5 characters)", t.errors.on(:content)
-
- t.title = "abe"
- t.content = "mad"
-
- assert t.save
- end
def test_validate_format
Topic.validates_format_of(:title, :content, :with => /^Validation macros rule!$/, :message => "is bad data")
@@ -243,4 +229,123 @@ class ValidationsTest < Test::Unit::TestCase
assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => {} ) }
assert_nothing_raised(ArgumentError) { Topic.validates_inclusion_of( :title, :in => [] ) }
end
+
+ def test_validates_length_of_using_minimum
+ Topic.validates_length_of( :title, :minimum=>5 )
+ t = Topic.create("title" => "valid", "content" => "whatever")
+ assert t.valid?
+ t.title = "not"
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "is too short (min is 5 characters)", t.errors["title"]
+ t.title = ""
+ assert !t.valid?
+ assert t.errors.on(:title)
+ t.title = nil
+ assert !t.valid?
+ assert_equal "is too short (min is 5 characters)", t.errors["title"]
+ assert t.errors.on(:title)
+ end
+
+ def test_validates_length_of_using_maximum
+ Topic.validates_length_of( :title, :maximum=>5 )
+ t = Topic.create("title" => "valid", "content" => "whatever")
+ assert t.valid?
+ t.title = "notvalid"
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "is too long (max is 5 characters)", t.errors["title"]
+ t.title = ""
+ assert t.valid?
+ t.title = nil
+ assert t.valid?
+ end
+
+ def test_validates_length_of_using_within
+ Topic.validates_length_of(:title, :content, :within => 3..5)
+
+ t = Topic.create("title" => "a!", "content" => "I'm ooooooooh so very long")
+ assert !t.save
+ assert_equal "is too short (min is 3 characters)", t.errors.on(:title)
+ assert_equal "is too long (max is 5 characters)", t.errors.on(:content)
+
+ t.title = "abe"
+ t.content = "mad"
+
+ assert t.save
+ end
+
+ def test_validates_length_of_using_is
+ Topic.validates_length_of( :title, :is=>5 )
+ t = Topic.create("title" => "valid", "content" => "whatever")
+ assert t.valid?
+ t.title = "notvalid"
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "is the wrong length (should be 5 characters)", t.errors["title"]
+ t.title = ""
+ assert !t.valid?
+ t.title = nil
+ assert !t.valid?
+ end
+
+ def test_validates_length_of_nasty_params
+ assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum=>6, :maximum=>9) }
+ assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :maximum=>9) }
+ assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :minimum=>9) }
+ assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>6, :is=>9) }
+ assert_raise(ArgumentError) { Topic.validates_length_of(:title, :minimum=>"a") }
+ assert_raise(ArgumentError) { Topic.validates_length_of(:title, :maximum=>"a") }
+ assert_raise(ArgumentError) { Topic.validates_length_of(:title, :within=>"a") }
+ assert_raise(ArgumentError) { Topic.validates_length_of(:title, :is=>"a") }
+ end
+
+ def test_validates_length_of_custom_errors_for_minimum_with_message
+ Topic.validates_length_of( :title, :minimum=>5, :message=>"boo %d" )
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "boo 5", t.errors["title"]
+ end
+
+ def test_validates_length_of_custom_errors_for_minimum_with_too_short
+ Topic.validates_length_of( :title, :minimum=>5, :too_short=>"hoo %d" )
+ t = Topic.create("title" => "uhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "hoo 5", t.errors["title"]
+ end
+
+ def test_validates_length_of_custom_errors_for_maximum_with_message
+ Topic.validates_length_of( :title, :maximum=>5, :message=>"boo %d" )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "boo 5", t.errors["title"]
+ end
+
+ def test_validates_length_of_custom_errors_for_maximum_with_too_long
+ Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %d" )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "hoo 5", t.errors["title"]
+ end
+
+ def test_validates_length_of_custom_errors_for_is_with_message
+ Topic.validates_length_of( :title, :is=>5, :message=>"boo %d" )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "boo 5", t.errors["title"]
+ end
+
+ def test_validates_length_of_custom_errors_for_is_with_wrong_length
+ Topic.validates_length_of( :title, :is=>5, :wrong_length=>"hoo %d" )
+ t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
+ assert !t.valid?
+ assert t.errors.on(:title)
+ assert_equal "hoo 5", t.errors["title"]
+ end
+
end