diff options
author | Roque Pinel <repinel@gmail.com> | 2015-07-11 14:40:14 -0400 |
---|---|---|
committer | Roque Pinel <repinel@gmail.com> | 2015-07-11 14:40:14 -0400 |
commit | 7500daec69499e4f2da2fc06cd816c754cf59504 (patch) | |
tree | b4d0dcd9a5174c457994c5d69382ea0666731b47 /activemodel/test | |
parent | 14354f195540954a1dfc5c954d06389c9f71e986 (diff) | |
download | rails-7500daec69499e4f2da2fc06cd816c754cf59504.tar.gz rails-7500daec69499e4f2da2fc06cd816c754cf59504.tar.bz2 rails-7500daec69499e4f2da2fc06cd816c754cf59504.zip |
Conditionally convert the raw_value received by the numeric validator.
This fixes the issue where you may be comparing (using a numeric
validator such as `greater_than`) numbers of a specific Numeric type
such as `BigDecimal`.
Previous behavior took the numeric value to be validated and
unconditionally converted to Float. For example, due to floating point
precision, this can cause issues when comparing a Float to a BigDecimal.
Consider the following:
```
validates :sub_total, numericality: {
greater_than: BigDecimal('97.18')
}
```
If the `:sub_total` value BigDecimal.new('97.18') was validated against
the above, the following would be valid since `:sub_total` is converted
to a Float regardless of its original type. The result therefore becomes
Kernel.Float(97.18) > BigDecimal.new('97.18')
The above illustrated behavior is corrected with this patch by
conditionally converting the value to validate to float.
Use the post-type-cast version of the attribute to validate numericality
[Roque Pinel & Trevor Wistaff]
Diffstat (limited to 'activemodel/test')
-rw-r--r-- | activemodel/test/cases/validations/numericality_validation_test.rb | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/activemodel/test/cases/validations/numericality_validation_test.rb b/activemodel/test/cases/validations/numericality_validation_test.rb index 05432abaff..4db6c828f8 100644 --- a/activemodel/test/cases/validations/numericality_validation_test.rb +++ b/activemodel/test/cases/validations/numericality_validation_test.rb @@ -71,6 +71,13 @@ class NumericalityValidationTest < ActiveModel::TestCase valid!([11]) end + def test_validates_numericality_with_greater_than_using_differing_numeric_types + Topic.validates_numericality_of :approved, greater_than: BigDecimal.new('97.18') + + invalid!([-97.18, BigDecimal.new('97.18'), BigDecimal('-97.18')], 'must be greater than 97.18') + valid!([97.18, 98, BigDecimal.new('98')]) # Notice the 97.18 as a float is greater than 97.18 as a BigDecimal due to floating point precision + end + def test_validates_numericality_with_greater_than_or_equal Topic.validates_numericality_of :approved, greater_than_or_equal_to: 10 @@ -78,6 +85,13 @@ class NumericalityValidationTest < ActiveModel::TestCase valid!([10]) end + def test_validates_numericality_with_greater_than_or_equal_using_differing_numeric_types + Topic.validates_numericality_of :approved, greater_than_or_equal_to: BigDecimal.new('97.18') + + invalid!([-97.18, 97.17, 97, BigDecimal.new('97.17'), BigDecimal.new('-97.18')], 'must be greater than or equal to 97.18') + valid!([97.18, 98, BigDecimal.new('97.19')]) + end + def test_validates_numericality_with_equal_to Topic.validates_numericality_of :approved, equal_to: 10 @@ -85,6 +99,13 @@ class NumericalityValidationTest < ActiveModel::TestCase valid!([10]) end + def test_validates_numericality_with_equal_to_using_differing_numeric_types + Topic.validates_numericality_of :approved, equal_to: BigDecimal.new('97.18') + + invalid!([-97.18, 97.18], 'must be equal to 97.18') + valid!([BigDecimal.new('97.18')]) + end + def test_validates_numericality_with_less_than Topic.validates_numericality_of :approved, less_than: 10 @@ -92,6 +113,13 @@ class NumericalityValidationTest < ActiveModel::TestCase valid!([-9, 9]) end + def test_validates_numericality_with_less_than_using_differing_numeric_types + Topic.validates_numericality_of :approved, less_than: BigDecimal.new('97.18') + + invalid!([97.18, BigDecimal.new('97.18')], 'must be less than 97.18') + valid!([-97.0, 97.0, -97, 97, BigDecimal.new('-97'), BigDecimal.new('97')]) + end + def test_validates_numericality_with_less_than_or_equal_to Topic.validates_numericality_of :approved, less_than_or_equal_to: 10 @@ -99,6 +127,13 @@ class NumericalityValidationTest < ActiveModel::TestCase valid!([-10, 10]) end + def test_validates_numericality_with_less_than_or_equal_to_using_differing_numeric_types + Topic.validates_numericality_of :approved, less_than_or_equal_to: BigDecimal.new('97.18') + + invalid!([97.18, 98], 'must be less than or equal to 97.18') + valid!([-97.18, BigDecimal.new('-97.18'), BigDecimal.new('97.18')]) + end + def test_validates_numericality_with_odd Topic.validates_numericality_of :approved, odd: true @@ -196,7 +231,7 @@ class NumericalityValidationTest < ActiveModel::TestCase def valid!(values) with_each_topic_approved_value(values) do |topic, value| - assert topic.valid?, "#{value.inspect} not accepted as a number" + assert topic.valid?, "#{value.inspect} not accepted as a number with validation error: #{topic.errors[:approved].first}" end end |