Skip to content
Browse files

Merge pull request #4905 from herimedia/to-infinity-and-beyond

Infinity should be a valid validates_length_of maximum
  • Loading branch information...
2 parents 6342cdd + 60dad82 commit 393f797be868c46e9a25c6f07760e4cd69d150ea @tenderlove tenderlove committed Feb 6, 2012
View
4 activemodel/lib/active_model/validations/length.rb
@@ -29,8 +29,8 @@ def check_validity!
keys.each do |key|
value = options[key]
- unless value.is_a?(Integer) && value >= 0
- raise ArgumentError, ":#{key} must be a nonnegative Integer"
+ unless value.is_a?(Integer) && value >= 0 or value == Float::INFINITY
+ raise ArgumentError, ":#{key} must be a nonnegative Integer or Infinity"
end
end
end
View
18 activemodel/test/cases/validations/length_validation_test.rb
@@ -357,4 +357,22 @@ def test_validates_length_of_for_ruby_class
ensure
Person.reset_callbacks(:validate)
end
+
+ def test_validates_length_of_for_infinite_maxima
+ Topic.validates_length_of(:title, :within => 5..Float::INFINITY)
+
+ t = Topic.new("title" => "1234")
+ assert t.invalid?
+ assert t.errors[:title].any?
+
+ t.title = "12345"
+ assert t.valid?
+
+ Topic.validates_length_of(:author_name, :maximum => Float::INFINITY)
+
+ assert t.valid?
+
+ t.author_name = "A very long author name that should still be valid." * 100
+ assert t.valid?
+ end
end

0 comments on commit 393f797

Please sign in to comment.
Something went wrong with that request. Please try again.