Repository navigation
Use the post-type-cast version of the attribute to validate numericality - #19851
Merged
Merged
Conversation
repinel
force-pushed
the
numericality-validation2
branch
from
April 22, 2015 15:40
199bf3d to
0c33566
Compare
Contributor
There was a problem hiding this comment.
to_i will always be present? (whether 0 or otherwise) so this can be simplified to
/\A[+-]?\d+\z/ === raw_value.to_swithout presence check since === returns true or false
Member
Author
|
👍 It may be of value to add the tests that this is fixing. |
repinel
force-pushed
the
numericality-validation2
branch
from
April 22, 2015 22:07
0c33566 to
b8e3f8b
Compare
Contributor
|
@trev |
|
@egilburg I mean the whole commit, not your suggestion :) |
|
Nevermind! It's too early, I completely overlooked the tests. Carry on. |
repinel
force-pushed
the
numericality-validation2
branch
from
April 23, 2015 03:47
b8e3f8b to
cacb418
Compare
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
force-pushed
the
numericality-validation2
branch
from
July 11, 2015 18:46
cacb418 to
7500dae
Compare
sgrif
added a commit
that referenced
this pull request
Oct 20, 2015
Use the post-type-cast version of the attribute to validate numericality
This was referenced 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.