z.ZodSchema Questions #794
-
Hello, I was looking for a way to define a human-readable type and then come up with the equivalent schema. This is what I ended up with:
Here are my two questions:
Any help would be appreciated. Thanks! NOTE: Using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
If you have a required field, then giving it a default value doesn't make any sense. Because, by definition, a required field always needs to be passed into
export const dashboards: z.ZodSchema<Dashboards> = z.object( {
// ^^^^^^^^^^
// The types of '_type.asdf' are incompatible between these types.
// Type 'number' is not assignable to type 'string | undefined'.
asdf: z.number(),
dashboards: z.array( dashboard ).default( [] ),
currentDashboardId: z.string().optional(),
} ) Hope this helps. :D |
Beta Was this translation helpful? Give feedback.
If you have a required field, then giving it a default value doesn't make any sense. Because, by definition, a required field always needs to be passed into
parse
or it will throw an error. So defining a default value would only ever make sense if that field was optional. So the docs could be updated to be more explicit about this, but I don't think it is entirely necessary.