zod icon indicating copy to clipboard operation
zod copied to clipboard

Add documentation for `.lazy`

Open ryami333 opened this issue 2 years ago • 2 comments

This method is referenced twice in code-snippets in the README without explanation, and doesn't appear to be documented itself.

ryami333 avatar Jul 01 '22 05:07 ryami333

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.

ElasticBottle avatar Jul 12 '22 21:07 ElasticBottle

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
}));

UNDERCOVERj avatar Jul 19 '22 03:07 UNDERCOVERj