Replies: 1 comment 4 replies
-
|
You're running into a common point of confusion with Zod's internals. When you use If you're seeing Here's how you can verify: const isOptional = schema.shape['firstName'].safeParse(undefined).success; // should be true
const type = schema.shape['firstName']._zod.def.type; // should be "optional"If The most reliable way to check if a schema is optional is to use If your question is answered, please close the issue! To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Please consider this schema:
Or like this, same problem:
Why is
firstNamenot optional?This will be
false. Why? The documentation says either of the above are the way to make something optional. It also says optional means it acceptsundefined, which is what I'm testing for. And the deprecation warning clearly sayssafeParse(undefined)is the way to do it correctly.And yet if I check:
That will be
"nonoptional". How does it arrive at there?So what is going on? The documentation is so crystal clear on this, and yet, somehow, I'm doing something wrong apparently. What is actually the way to make something optional please?
Beta Was this translation helpful? Give feedback.
All reactions