Repository navigation
Reading default values from a schema #3843
Replies: 1 comment
|
For the schema you posted, the public parsing API gives you the defaults without inspecting const defaults = RAW_SEACH_QUERY.parse({});
const qDefault = RAW_SEACH_QUERY.shape.q.parse(undefined);For example, with { q: '', o: 0, l: 20, e: false, v: 'list' }Use the untransformed Default values and catch values have different triggers: const field = z.string().default('default value').catch('fallback value');
field.parse(undefined); // 'default value'
field.parse(123); // 'fallback value'If the form needs the catch values as data, define those values separately and pass them into I checked these examples and your field chains with Zod 3.24.2, 3.25.76, and 4.6.5. The docs describe the |
Uh oh!
There was an error while loading. Please reload this page.
I'm building a search form, and the schema for it is defined as the following:
This is further processed with
.transform()step, but that's not relevant to my question. I'd really like access to the default values and the catch fallback values too. I tried the following, but it doesn't work:I would expect something like
.defaultValue()(and.catchFallback()) to be available. I asked ChatGPT. It suggestedRAW_SEACH_QUERY.shape.q._def.defaultValue(), but.defaultValue()isn't defined. Worse I assume_defisn't part of the public API, so using it is likely not supported.Is there a (supported) way to access the default values that I'm just missing?
All reactions