You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Conditional validation is pretty common problem, that's why we have requiredIf and requiredUnless. The problem is that they only work for required validation. If we want other validators to be conditionally executed as well I have to write custom validator wrappers, which is a bit cumbersome. In my app, since I've done it so many times, I end-up writing a wrapper that I'm now sharing with you.
Solution
a when validator, that would work just like requiredIf but instead accepted any validator as an argument.
Note: The code could also be adapted to accept a validator function instead of a normalized validator as it is now.
EDIT: removed optional chaining for shorter output.
Usage
// inlineconstrules={x: 'something',y: when((_,siblings)=>siblings.x==='something')(myValidator),z: when((_,siblings)=>siblings.x==='something')(myOtherValidator),}// wrap it to DRY it upconstwhenXIsSomething=when((_,siblings)=>siblings.x==='something')construles={x: 'something',y: whenXIsSomething(myValidator),z: whenXIsSomething(myOtherValidator),}
Additional context
I understand that this can be achieved in userland pretty easily, but would be nice to have baked-in support.
I believe this is useful, otherwise requiredIf and requiredUnless wouldn't be a thing. In fact, these validators could be rewritten using the when validator under the hood.
construles={y: requiredIf((_,siblings)=>siblings.x==='something'),x: when((_,siblings)=>siblings.x==='something')(required)// same as above}
Since when returns a function and not a validator, maybe this could be categorised as a helper like helpers.withMessage to be more consistent with the current API
The text was updated successfully, but these errors were encountered:
The problem
Conditional validation is pretty common problem, that's why we have
requiredIf
andrequiredUnless
. The problem is that they only work forrequired
validation. If we want other validators to be conditionally executed as well I have to write custom validator wrappers, which is a bit cumbersome. In my app, since I've done it so many times, I end-up writing a wrapper that I'm now sharing with you.Solution
a when validator, that would work just like
requiredIf
but instead accepted anyvalidator
as an argument.The code
Note: The code could also be adapted to accept a validator function instead of a normalized validator as it is now.
EDIT: removed optional chaining for shorter output.
Usage
Additional context
I understand that this can be achieved in userland pretty easily, but would be nice to have baked-in support.
I believe this is useful, otherwise
requiredIf
andrequiredUnless
wouldn't be a thing. In fact, these validators could be rewritten using thewhen
validator under the hood.Since
when
returns a function and not a validator, maybe this could be categorised as a helper likehelpers.withMessage
to be more consistent with the current APIThe text was updated successfully, but these errors were encountered: