Feature: Add In, InStrict, and DateTime validation rules #10
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.
Resolves #1
This adds the
In
andDateTime
validation rules.In Rule
The
In
rule limits a value to a given set of values:The only thing I'm going back and forth on is whether value checking should be strict or not. If it's not strict, then
in:1,2,3
wouldn't match integer values of 1, 2, 3. That seems counterintuitive, but is only an issue when converting from string values. Still, that's encouraged behavior. Looking at the Laravel In rule, it's not strict. If not strict, then perhaps theIn
constructor should not be variadic and have a second,$strict
parameter:new In([1, 2, 3], true)
— which is false by default. Open to thoughts!DateTime Rule
The
DateTime
rule validates that a given value is a valid date and/or time, and then sanitizes it as aDateTimeImmutable
:Note that relative date formats never pass. So values like
now
or+1 day
will not pass validation.If the value passed is a
DateTimeInterface
instance, then it passes and is directly passed back. Otherwise, the value is parsed to aDateTimeImmutable
(using the optional format if provided), and the new instance is returned.It may be too opinionated to return a
DateTimeImmutable
over aDateTime
. I'm open to feedback here as well.