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

Fix regression in Numericality validator #29249

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
5 changes: 5 additions & 0 deletions activemodel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Fix regression in numericality validator when comparing Decimal and Float input
values with more scale than the schema.

*Bradley Priest*

* Fix methods `#keys`, `#values` in `ActiveModel::Errors`.

Change `#keys` to only return the keys that don't have empty messages.
Expand Down
4 changes: 3 additions & 1 deletion activemodel/lib/active_model/validations/numericality.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def validate_each(record, attr_name, value)
return
end

unless raw_value.is_a?(Numeric)
if raw_value.is_a?(Numeric)
value = raw_value
else
value = parse_raw_value_as_a_number(raw_value)
end

Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/validations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ def test_numericality_validation_with_mutation
assert topic.valid?
end

def test_numericality_validation_checks_against_raw_value
klass = Class.new(Topic) do
def self.model_name
ActiveModel::Name.new(self, nil, "Topic")
end
attribute :wibble, :decimal, scale: 2, precision: 9
validates_numericality_of :wibble, greater_than_or_equal_to: BigDecimal.new("97.18")
end

assert_not klass.new(wibble: "97.179").valid?
assert_not klass.new(wibble: 97.179).valid?
assert_not klass.new(wibble: BigDecimal.new("97.179")).valid?
end

def test_acceptance_validator_doesnt_require_db_connection
klass = Class.new(ActiveRecord::Base) do
self.table_name = "posts"
Expand Down