Skip to content

Use clusivity for numericality with :in or :within - #44467

Open
skipkayhil wants to merge 1 commit into
rails:mainfrom
skipkayhil:numerically-inclusivity
Open

skipkayhil wants to merge 1 commit into
rails:mainfrom
skipkayhil:numerically-inclusivity

Conversation

@skipkayhil

Copy link
Copy Markdown
Member

Summary

Previously, only ranges were allowed, but this change uses the existing
Clusivity module to validate against anything with an include? method.

Other Information

Fixes #44465

@jonathanhefner

Copy link
Copy Markdown
Member

Echoing Rafael's question from #44465 (comment), what would be the benefit of using this instead of inclusion:?

@skipkayhil

Copy link
Copy Markdown
Member Author

I agree it probably makes (more?) sense to use inclusion instead of numericality. That being said, I think the api for numericality should be consistent either way: I don't think it makes sense to offer in: <range> and not in: <array/set> when inclusion allows for either.

@skipkayhil
skipkayhil force-pushed the numerically-inclusivity branch from a8a25c1 to 8b906b7 Compare May 24, 2022 04:18
Comment on lines 240 to 241

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Theoretically, this should work with an Enumerable too, yes?

Suggested change
def test_validates_numericality_with_in_array
Topic.validates_numericality_of :approved, in: [1, 3]
def test_validates_numericality_with_in_enumerable
Topic.validates_numericality_of :approved, in: [1, 3].each

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure about removing this. I understand why -- String responds to include? -- but I wonder if we should specifically check for String and raise an ArgumentError. Or perhaps check value.respond_to?(:include?) && !value.is_a?(Enumerable). (This would negate my previous suggestion involving CLUSIVITY_CHECKS.any? { ... }.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Allowing an arbitrary Enumerable also impacts error messages. For example, with validates_numericality_of :foo, in: Set[1, 3], the error message is "Foo must be in #<Set: {1, 3}>".

This message comes from

in: "must be in %{count}"

Whereas the error message for InclusionValidator is simply

inclusion: "is not included in the list"

I'm not sure of the best way to deal with that. One possibility is to change the 2nd argument of errors.add conditional on the type of option_value. e.g.:

Suggested change
record.errors.add(attr_name, option, **filtered_options(value).merge!(count: option_value))
error_type = option_value.is_a?(Range) ? option : :inclusion
record.errors.add(attr_name, error_type, **filtered_options(value).merge!(count: option_value))

Or:

Suggested change
record.errors.add(attr_name, option, **filtered_options(value).merge!(count: option_value))
if option_value.is_a?(Range)
record.errors.add(attr_name, option, **filtered_options(value).merge!(count: option_value))
else
record.errors.add(attr_name, :inclusion, **filtered_options(value))
end

Previously, only ranges were allowed, but this change uses the existing
Clusivity module to validate against anything with an include? method.
@skipkayhil
skipkayhil force-pushed the numerically-inclusivity branch from 8b906b7 to c97d414 Compare June 3, 2022 00:09
@skipkayhil

skipkayhil commented Jun 3, 2022 •

Copy link
Copy Markdown
Member Author

Pushed an update with the trivial changes. On thinking about this more, I'm now leaning more towards deprecating the in option and pointing people towards inclusion. I think it makes more sense to keep the basic validators as simple as possible, and if someone really wanted both numericality and clusivity they could either define both validations or they could eventually use something like #42907

@jonathanhefner

Copy link
Copy Markdown
Member

On thinking about this more, I'm now leaning more towards deprecating the in option and pointing people towards inclusion. I think it makes more sense to keep the basic validators as simple as possible, and if someone really wanted both numericality and clusivity they could either define both validations or they could eventually use something like #42907

I agree that using Clusivity for numericality seems to be more trouble than it's worth, but what would be the benefit of deprecating the in option? That seems like it would cause unnecessary work for app maintainers. And it is much more convenient to write e.g. in: 1..100 instead of greater_than_or_equal_to: 1, less_than_or_equal_to: 100.

I would be more inclined to improve the in option, especially its error message. i.e. Stick with only allowing a Range, but find a (non-developer) user-friendly way to represent it in the error message (without breaking existing apps that have customized that message). Also, possibly convert boundless ranges to an equivalent :*_than condition (similar to what we do in LengthValidator).

This branch has not been deployed

No deployments
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.

[activerecord] numericality: {in: ...} should accept an Array or Set of explicit values

2 participants