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

missing_presence_validation incorrectly marks numericaility with allow_nil: false as missing a presence validator #187

Open
vanharen opened this issue Nov 5, 2024 · 2 comments

Comments

@vanharen
Copy link

vanharen commented Nov 5, 2024

first off, thanks for the gem! very handy!

so, to be honest, i'm not 100% convinced this is really a bug, but figured i'd raise it, just in case it is. please excuse me if this has been discussed previously -- i was unable to find any mention of it in issues.

so -- if you have a non-null integer field that has a validation like:

  validates :rank, numericality: {greater_than_or_equal_to: 0}, allow_nil: false

then it is my belief that this should suffice as a presence validator even though presence: true is not explicitly listed in the validator. one could include this but it is redundant, imho, based on the following (from https://guides.rubyonrails.org/active_record_validations.html#numericality)

By default, numericality doesn't allow nil values. You can use allow_nil: true option to permit it. For Integer and Float columns empty strings are converted to nil.

so in the example above allow_nil: false should not really even be necessary as it is the default. so the existence of the numericality: validator -- in the absence of allow_nil: true should suffice to be a presence validator for non-null integer columns, yes?

if my assertion is correct, i'd consider adding a PR to address this, once i get my fingers into the code. i welcome discussion. thanks for reading!

@fatkodima
Copy link
Contributor

yes, looks reasonable to me. Please do open a PR.

@gregnavis
Copy link
Owner

@vanharen, thank you for your comment. Discussion is always much appreciated!

I don't think treating allow_nil: false (which is the default as you mentioned) is a good idea. Technically, it does report a validation error, but we want to help application developers deliver the best possible user experience.

In this case, if you leave rank empty you'll get an error of type :not_a_number with a message "Rank is not a number". I think this is a very confusing error from a user's perspective, especially that it'll additionally be shown when the field contains an invalid value.

If you do:

validates :rank, presence: true
validates :rank, numericality: { greater_than_or_equal_to: 0 }

then leaving rank blank will result in two messages ("Rank is blank", "Rank is not a number"), which is confusing. In this case, better UX can be achieved by adding allow_nil to numericality validator (and I'm working on a detector to do just that).

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

No branches or pull requests

3 participants