adxvcasas - Hello everyone, How can I make the ...
Hello everyone, How can I make the
dryRunDate
,dryRunStart
and dryRunEnd
dynamic?
so I have a type
who have a value of yes and no. Is it possible to make dryRunDate
,dryRunStart
and dryRunEnd
required if the user select yes
from type
?
this is my zod
8 Replies
You need to use a union (a discriminated one if possible). Alternatively you need to use refine/superRefine and do the check manually there
Hello @Svish , I tried doing it like this.
I don't know if i'm doing it correctly or not, Thanks
You're sort of on the right track if you want to use
refine
, but that's not how refine
works. It should just return true
or false
. To set the error message, you there's a second parameter of refine
with options.
In this case, since you're checking multiple fields, you should probably use superRefine
or transform
instead.
Both get a ctx
parameter where you can check things and call ctx.addIssue
for whatever you need
Check the documentation for those functions on https://zod.devwhat if I change the
type.enum
to true or false? is this still applicable?
Thank youIf I were you I would try the discriminated union
Then you'd use
type
as the discriminator, and z.literal(true)
and z.literal(false)
for the two "branches" in the unionHello, I tried experemnting it, is this thing worth?
Have you read the docs?
https://zod.dev/?id=refine
What you're doing there is not correct, no. Look at the examples in the documentation
Hello, Svish, I did this thing,
It works on 1 pair only but if I add the other two, the warning didn't show.
This one works fine, Thank you