New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Numericality validation regression in 5.2.1 #33651
Comments
Upgrading to 5.2.1 broke my specs as well. I test PS. The CHANGELOG for |
… Active Record The purpose of fe9547b is to work type casting to value from database. But that was caused not to use the value before type cast even except Active Record. There we never guarantees that the value before type cast was going to the used in this validation, but we should not change the behavior unless there is some particular reason. To restore original behavior, still use the value before type cast if `came_from_user?` is undefined (i.e. except Active Record). Fixes rails#33651. Fixes rails#33686.
This also affects code which wraps ActiveRecord objects with form presenters (e.g., to support manipulation of related AR objects as a unit). It can be worked around there by having the presenter delegate the _came_from_user? stuff for the relevant attributes (and, oddly, read_attribute_before_type_cast), but it would be nice if those workarounds don't have to turn permanent... |
Steps to reproduce
https://gist.github.com/zarqman/075179677c21c7aa58d7a7ba3376bac9
Expected behavior
validates :quantity, numericality: {greater_than: 0}
should return/not a number/
when given a value of "invalid".This is how it worked up through 5.2.0.
Actual behavior
In 5.2.1, non-numeric user input, such as "invalid" is silently morphed to 0 (numeric zero) and then validated as such, incorrectly returning
/must be greater than 0/
instead.System configuration
Rails version: 5.2.1
Ruby version: 2.5.1
Mongoid version: 7.0.1
Other notes
Because mongoid relies on
before_type_cast
and does not implementcame_from_user?
, the change to eliminate use ofbefore_type_cast
in Rails 5.2.1 is a breaking change.A post-5.2.1 commit (#33603) addresses a different regression from the change in 5.2.1, but does not resolve this one. Additionally, that commit might be incorrect in that it uses
read_attribute(attr_name)
as a fallback, preempting use ofvalue
which was produced usingread_attribute_for_validation(attr_name)
(which would seem preferable sinceraw_value
is being used for validation). It's possibleread_attribute_for_validation
itself needs to fallback toread_attribute
instead ofsend
(whether in ActiveModel or ActiveRecord, I don't know).It's possible this is a mongoid issue (although since it broke in the Rails 5.2.1 patch-level release, it's arguably still a Rails issue, at least until 6.0).
Also filed Mongoid bug https://jira.mongodb.org/browse/MONGOID-4604
The text was updated successfully, but these errors were encountered: