Replies: 1 comment
-
You can achieve this with a ternary, but beware the inferred type signature since the type changes at runtime. It's likely that what you want here is two different schemas or some kind of union: lastName: needLastName ? z.string() : z.string().optional(), |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I'd like to conditionally set some fields optional.
Right now, it does not seem we can specify a boolean value to
.optional()
, although that would be very handy:If
needLastName = true
: the field is indeed optional.If
needLastName = false
: the.optional()
is "dismissed" (field is in fact required).Such a change should be non-breaking as a default value could be set to true.
Or perhaps there's another way to achieve the same result?
Beta Was this translation helpful? Give feedback.
All reactions