Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid converting integer as a string into float #28050

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions activemodel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Avoid converting integer as a string into float.

*namusyaka*

* Remove deprecated behavior that halts callbacks when the return is false.

*Rafael Mendonça França*
Expand Down
1 change: 1 addition & 0 deletions activemodel/lib/active_model/validations/numericality.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def is_number?(raw_value)
end

def parse_raw_value_as_a_number(raw_value)
return raw_value.to_i if is_integer?(raw_value)
Kernel.Float(raw_value) if raw_value !~ /\A0[xX]/
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ def test_validates_numericality_of_for_ruby_class
Person.clear_validators!
end

def test_validates_numericality_with_exponent_number
base = 10_000_000_000_000_000
Topic.validates_numericality_of :approved, less_than_or_equal_to: base
topic = Topic.new
topic.approved = (base + 1).to_s

assert topic.invalid?
end

def test_validates_numericality_with_invalid_args
assert_raise(ArgumentError) { Topic.validates_numericality_of :approved, greater_than_or_equal_to: "foo" }
assert_raise(ArgumentError) { Topic.validates_numericality_of :approved, less_than_or_equal_to: "foo" }
Expand Down