Skip to content

Use the post-type-cast version of the attribute to validate numericality - #19851

Merged
sgrif merged 1 commit into
rails:masterfrom
repinel:numericality-validation2
Oct 20, 2015
Merged

sgrif merged 1 commit into
rails:masterfrom
repinel:numericality-validation2

Conversation

@repinel

@repinel repinel commented Apr 22, 2015

Copy link
Copy Markdown
Member

This should fix the issue #12134 as started by @trev in #18617 and with the changed indicated by @sgrif.

When validating numeric attributes, the actual value was not being used since the validator was parsing it internally to float.

@repinel
repinel force-pushed the numericality-validation2 branch from 199bf3d to 0c33566 Compare April 22, 2015 15:40

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_i will always be present? (whether 0 or otherwise) so this can be simplified to

/\A[+-]?\d+\z/ === raw_value.to_s

without presence check since === returns true or false

@repinel

repinel commented Apr 22, 2015

Copy link
Copy Markdown
Member Author

@egilburg good point. I'll make the appropriate changes.

cc @sgrif

@trev

trev commented Apr 22, 2015

Copy link
Copy Markdown

👍 It may be of value to add the tests that this is fixing.

@repinel
repinel force-pushed the numericality-validation2 branch from 0c33566 to b8e3f8b Compare April 22, 2015 22:07
@egilburg

Copy link
Copy Markdown
Contributor

@trev =~ will return nil/Integer. If you want predicate, use ===

@trev

trev commented Apr 22, 2015

Copy link
Copy Markdown

@egilburg I mean the whole commit, not your suggestion :)

@trev

trev commented Apr 22, 2015

Copy link
Copy Markdown

Nevermind! It's too early, I completely overlooked the tests. Carry on.

@repinel
repinel force-pushed the numericality-validation2 branch from b8e3f8b to cacb418 Compare April 23, 2015 03:47
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]
@repinel
repinel force-pushed the numericality-validation2 branch from cacb418 to 7500dae Compare July 11, 2015 18:46
sgrif added a commit that referenced this pull request Oct 20, 2015
Use the post-type-cast version of the attribute to validate numericality
@sgrif
sgrif merged commit 328ec26 into rails:master Oct 20, 2015
sgrif added a commit that referenced this pull request Oct 20, 2015
The error message when asserting `greater_than: BigDecimal.new` will
give an error message based on how BigDecimal displays itself. Big
decimal appears to always use scientific notation. This might not be the
best error message for the general case, but the general case wouldn't
use big decimal for the validation. And if they do, they likely need
this level of precision.
sgrif added a commit that referenced this pull request Oct 21, 2015
Ok, this explains why the branch showed as green. We don't run files in
isolation for PRs, only for master. Active Support monkeypatches
`BigDecimal#to_s`, so the generated error message was different
depending on if the file was run in isolation
@repinel
repinel deleted the numericality-validation2 branch October 21, 2015 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants