Skip to content
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

allow_nil should work for casted value in NumericalityValidator #40765

Merged

Conversation

kamipo
Copy link
Member

@kamipo kamipo commented Dec 8, 2020

It is a regression for 4cc438a.

NumericalityValidator basically takes the value before typecasting,
but allow_nil should work for the typecasted value for the
compatibility.

Fixes #40750.

It is a regression for 4cc438a.

`NumericalityValidator` basically takes the value before typecasting,
but `allow_nil` should work for the typecasted value for the
compatibility.

Fixes rails#40750.
@@ -167,7 +168,9 @@ def check_validity!

private
def read_attribute_for_validation(record, attr_name)
record.read_attribute_for_validation(attr_name)
value = record.read_attribute_for_validation(attr_name)
throw(:allowed) if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
Copy link
Member

Choose a reason for hiding this comment

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

Moving the checks here actually make the API for custom validations worse. Users need to know that they need to deal with those options inside read_attribute_for_validation while before they didn't. I believe we should continue doing this check inside validate to keep users not needing to worry about checking for those options.

Can we split the job of read_attribute_for_validation in two? One which does record.read_attribute_for_validation(attr_name) and we use for the options checks and the other than calculate value that we will pass to validate_each? This will also avoid the throw/abort.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree that it is not ideal, the intent of the implementation was to avoid extra value argument for read_attribute_for_validation.
I've given up that finally, splited the job and add value argument for read_attribute_for_validation in 720a60e (perhaps read_attribute_for_validation no longer appropriate name now?).

Copy link
Member

Choose a reason for hiding this comment

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

Agree the name is not matching the behaviour anymore. I'd call it prepare_value_for_validation.

@rafaelfranca rafaelfranca merged commit 72dc9ac into rails:master Dec 9, 2020
rafaelfranca added a commit that referenced this pull request Dec 9, 2020
…d_value

`allow_nil` should work for casted value in `NumericalityValidator`
@kamipo kamipo deleted the allow_nil_should_work_for_casted_value branch December 9, 2020 22:48
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.

NumericalityValidator allow_nil option doesn't work anymore for params coming from a controller
2 participants