"when" (joi) in valibot #836
abhishekmardiya
started this conversation in
General
Replies: 2 comments 6 replies
-
I recommend using import * as v from 'valibot';
const Schema1 = v.variant('_id', [
v.object({
_id: v.pipe(v.string(), v.uuid()),
email: v.pipe(v.string(), v.email()),
}),
v.object({
_id: v.undefined(),
email: v.undefined(),
}),
]);
const Schema2 = v.union([
v.object({
_id: v.pipe(v.string(), v.uuid()),
email: v.pipe(v.string(), v.email()),
}),
v.object({
_id: v.undefined(),
email: v.undefined(),
}),
]);
const Schema3 = v.pipe(
v.object({
_id: v.optional(v.pipe(v.string(), v.uuid())),
email: v.optional(v.pipe(v.string(), v.email())),
}),
v.check((input) => !input._id || !!input.email)
); Related links: |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thank you so much! I've been searching for a solution like this for days. By the way, could you please update this page: Migrate from Joi to make it easier to distinguish the APIs from Joi? Also, you've built an amazing library—hats off to you! |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
email: Joi.when("_id", {
is: Joi.exist(),
then: Joi.optional(),
otherwise: Joi.string().email().optional(),
}),
Beta Was this translation helpful? Give feedback.
All reactions