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 beginless and endless ranges in validates_inclusion_of #1615

Merged
merged 1 commit into from
Feb 23, 2024
Merged

Allow beginless and endless ranges in validates_inclusion_of #1615

merged 1 commit into from
Feb 23, 2024

Conversation

pjpires
Copy link
Contributor

@pjpires pjpires commented Feb 20, 2024

Problem

Currently, the min and max range values are checked with range.first and range.max, that will fail with cannot get the first element of beginless range (RangeError) and cannot get the maximum of endless range (RangeError) respectively for beginless/endless ranges.

Proposed solution

We can rely on range.begin and range.end (by correctly checking if end should be considered by looking at exclude_end?) instead, that will both return nil instead of raising for beginless/endless ranges.

Also added the required early returns on minimum/maximum value checks when the begin/end of range is absent and updated the specs with two scenarios for beginless/endless ranges.

Fixes #1614.

Copy link
Member

@matsales28 matsales28 left a comment

Choose a reason for hiding this comment

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

LGTM! I left a single comment about changing the way we handle the guard clause otherwise, everything looks good to me. Thanks for fixing this bug!

Comment on lines 456 to 457
return true unless @minimum

Copy link
Member

Choose a reason for hiding this comment

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

I'd say we could change this by removing the guard clause to stay consistent with the other methods in this class.

Suggested change
return true unless @minimum
@minimum.nil? || allows_value_of(@minimum, @low_message)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes a lot of sense, thanks! I've updated the nil checks. 👍

Copy link
Collaborator

@vsppedro vsppedro left a comment

Choose a reason for hiding this comment

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

LGTM! Thank you for working on this!

@matsales28 matsales28 merged commit a078b46 into thoughtbot:main Feb 23, 2024
15 checks passed
@pjpires pjpires deleted the fix-validates-beginless-and-endless-ranges branch February 23, 2024 16:55
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

Successfully merging this pull request may close these issues.

RangeError: cannot get the first element of beginless range
3 participants