-
-
Notifications
You must be signed in to change notification settings - Fork 220
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
Proposal: Ternary operator #1009
Comments
Thank you for the great issue description. const Schema = v.pipe(
v.string(),
v.ternary(
v.url(),
// positive actions:
[
v.startsWith('http', 'Only http based URLs are accepted'),
v.check(hasPort, 'URL should explicitly provide port to be used'),
],
// negative actions:
[
v.check(isPathLike, 'Should be path or URL'),
v.check(isAbsolutePath, 'Only absolute path is allowed'),
]
)
); Another approach to the const Schema = v.pipe(
v.string(),
v.strain(
[v.email(), [v.endsWith('@org.com'), v.check(isEmailAvailable)]],
[v.check(isPhoneNumber), [v.check(isPhoneAvailable)]],
'Fallback error message'
)
); |
Yep, Yet, I don't think that we can really scale v.strain with arrays as arguments, because it'll quickly go out of hand with hundreds of const Schema = v.pipe(
v.string(),
v.strain(
v.strainBranch(
v.email(),
v.endsWith('@org.com'),
v.check(isEmailAvailable)
),
v.strainBranch(
v.check(isPhoneNumber),
v.check(isPhoneAvailable)
),
'Fallback error'
)
) |
I will probably focus on our v1 release before coming back to this issue. Feel free to explore the API design further (including completely different ideas) and share your findings. In the meantime, since Valibot is modular, you can always write your own schemas, actions and methods to support such a feature in your own codebase. |
The ternary and strain features are exactly what I was looking for. |
Thanks for your feedback! Which API do you prefer? Do you have alternative solutions we should consider? |
This function signature is consistent and looks very good! |
As discussed in #994 would be great to have a ternary validation operator that enables branching of validation.
It declaration should look something like this:
When applied operator follows algorithm:
positive
::=v.is(selectorSchema, input)
positive
, thenpositiveSchema
positiveSchema
positive
, thennegativeSchema
negativeSchema
would be used something like this:
The text was updated successfully, but these errors were encountered: