Feature: Add In, InStrict, and DateTime validation rules#10
Feature: Add In, InStrict, and DateTime validation rules#10JasonTheAdams merged 3 commits intodevelopfrom
Conversation
In RuleFor a validation library, it seems smart to provide the ability to be strict with the types. I can see the value in the flexibility of non-strict checking, though. For that reason, I'd lean toward the non variadic approach or add a For the non-strict approach, I imagine we would want to cast as Additionally, it'd be good to get a couple of tests centered around the use case you laid out in your PR description:
DateTime RuleI'm a fan of the use of |
|
Thanks @borkweb!
Yeah, I was leaning towards a
Yeah, I think that should work. Honestly, I'm not entirely sure what that's accomplishing since "2" == 2 — whichever side the values are on. |
|
Hey @borkweb! I added the |
Resolves #1
This adds the
InandDateTimevalidation rules.In Rule
The
Inrule 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,3wouldn'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 theInconstructor should not be variadic and have a second,$strictparameter:new In([1, 2, 3], true)— which is false by default. Open to thoughts!DateTime Rule
The
DateTimerule 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
nowor+1 daywill not pass validation.If the value passed is a
DateTimeInterfaceinstance, 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
DateTimeImmutableover aDateTime. I'm open to feedback here as well.