zod
zod copied to clipboard
Add documentation for `.lazy`
This method is referenced twice in code-snippets in the README without explanation, and doesn't appear to be documented itself.
Definitely agreed, trying to learn the zod
api here and am very curious why z.lazy()
was used and what the usecase for it is for.
Inferring from the docs, it seems like z.lazy(()=> {})
is used when there's self referential in the schema definition.
Looking forward to learning more.
for example:
// ERROR: ReferenceError: Cannot access 'IndustryDTOZodSchema' before initialization
export const IndustryDTOZodSchema = z.object({
name: z.string(),
id: z.number().int(),
subs: IndustryDTOZodSchema
});
// SUCCESS
export const IndustryDTOZodSchema = z.lazy(() => z.object({
name: z.string(),
id: z.number().int(),
subs: IndustryDTOZodSchema
}));