Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Don't hang when a length range's maximum is Infinity
Browse files Browse the repository at this point in the history
Raise with a meaningful error message instead. It's
not feasible to allow infinitely large persisted
data structures since there's always some form of
constraint in reality, depending on the datastore
or even the physical amount of memory available.

[#1383 state:resolved]
  • Loading branch information
snusnu committed Oct 25, 2010
1 parent a2ff4b2 commit 69b4b9a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/dm-validations/auto_validate.rb
Expand Up @@ -132,9 +132,14 @@ def infer_presence_validation_for(property, options)
def infer_length_validation_for(property, options)
return unless [ DataMapper::Property::String, DataMapper::Property::Text ].any? { |klass| property.kind_of?(klass) }

case length = property.options.fetch(:length, DataMapper::Property::String::DEFAULT_LENGTH)
when Range then options[:within] = length
else options[:maximum] = length
length = property.options.fetch(:length, DataMapper::Property::String::DEFAULT_LENGTH)


if length.is_a?(Range)
raise ArgumentError, "Infinity is no valid upper bound for a length range" if length.last == Infinity
options[:within] = length
else
options[:maximum] = length
end

validates_length_of property.name, options_with_message(options, property, :length)
Expand Down
Expand Up @@ -122,4 +122,13 @@
@model.errors.on(:body).should == [ 'Body must be between 1 and 500 characters long' ]
end
end

describe 'with an infinitely long note' do
it "should raise when trying to set the upper bound of a property length range to Infinity" do
expected_msg = "Infinity is no valid upper bound for a length range"
lambda {
@model.class.property :body, String, :length => (1..1.0/0)
}.should raise_error(ArgumentError, expected_msg)
end
end
end

0 comments on commit 69b4b9a

Please sign in to comment.