Allow beginless and endless ranges in validates_inclusion_of#1615
Merged
matsales28 merged 1 commit intothoughtbot:mainfrom Feb 23, 2024
pjpires:fix-validates-beginless-and-endless-ranges
Merged
Allow beginless and endless ranges in validates_inclusion_of#1615matsales28 merged 1 commit intothoughtbot:mainfrom pjpires:fix-validates-beginless-and-endless-ranges
matsales28 merged 1 commit intothoughtbot:mainfrom
pjpires:fix-validates-beginless-and-endless-ranges
Conversation
matsales28
approved these changes
Feb 20, 2024
Member
matsales28
left a comment
There was a problem hiding this comment.
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 | ||
|
|
Member
There was a problem hiding this comment.
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) |
Contributor
Author
There was a problem hiding this comment.
That makes a lot of sense, thanks! I've updated the nil checks. 👍
vsppedro
approved these changes
Feb 20, 2024
Collaborator
vsppedro
left a comment
There was a problem hiding this comment.
LGTM! Thank you for working on this!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Currently, the min and max range values are checked with
range.firstandrange.max, that will fail withcannot get the first element of beginless range (RangeError)andcannot get the maximum of endless range (RangeError)respectively for beginless/endless ranges.Proposed solution
We can rely on
range.beginandrange.end(by correctly checking ifendshould be considered by looking atexclude_end?) instead, that will both returnnilinstead 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.