Repository navigation
Conversation
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.
|
I don't think this is the right approach. Rather than adding edge case after edge case to what is essentially duplicating the type casting logic, let's change the validator to use the existing method only to validate that the string was numeric, and then read the after type cast version for comparisons such as greater and less than. |
|
I had to read that a couple of times... I'll have a look tonight. |
|
Actually @sgrif if you could expand on that a little or explain what you mean a bit differently that would be great as I'm really not sure I understand... be kind it's my first PR :) |
|
So |
|
Closing in favor of #19851 |
This fixes the issue where you may be comparing (using a numeric
validator such as
greater_than) numbers of a specific Numeric typesuch as
BigDecimal. See #12134Previous 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:
If the
:sub_totalvalueBigDecimal.new('97.18')was validated againstthe above, the following would be valid since
:sub_totalis convertedto 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.