Skip to content

Conditionally convert the raw_value received by the numeric validator. - #18617

Closed
trev wants to merge 1 commit into
rails:masterfrom
trev:numericality_numeric_comparison
Closed

trev wants to merge 1 commit into
rails:masterfrom
trev:numericality_numeric_comparison

Conversation

@trev

@trev trev commented Jan 20, 2015

Copy link
Copy Markdown

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. See #12134

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.

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.
@sgrif

sgrif commented Jan 20, 2015

Copy link
Copy Markdown
Contributor

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.

@trev

trev commented Jan 20, 2015

Copy link
Copy Markdown
Author

I had to read that a couple of times... I'll have a look tonight.

@trev

trev commented Jan 21, 2015

Copy link
Copy Markdown
Author

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 :)

@sgrif

sgrif commented Jan 23, 2015

Copy link
Copy Markdown
Contributor

So value should already be the post-type-cast version of the attribute, at the beginning of the method. If we remove the assignments from the if statements, we'll be using that instead. This lets the types handle the conversion themselves (rather than the validator forcing a float) and the existing casting logic within the validator can remain, in order to do the validation it was intended for (ensuring that a given string is numeric)

@sgrif

sgrif commented Oct 20, 2015

Copy link
Copy Markdown
Contributor

Closing in favor of #19851

@sgrif sgrif closed this Oct 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants