asteasolutions / zod-to-openapi

A library that generates OpenAPI (Swagger) docs from Zod schemas
MIT License
955 stars 60 forks source link

Drizzle Zod + lazy #255

Closed samducker closed 1 month ago

samducker commented 1 month ago

Hi in all of my schemas created with 'drizzle-zod'

They cause this library to throw this error

{
"message": "Unknown zod object type, please specify `type` and other OpenAPI props using `ZodSchema.openapi`.",
"data": {
"currentSchema": {
"typeName": "ZodLazy"
}
}
}

I have searched the issues here and not found a workaround for this apart from not using drizzle zod, which is not ideal

as the benefit of using it is that my database schema stays as the source of truth for my route definition schemas.

Do you have any workarounds or advice to overcome this issue?

AGalabov commented 1 month ago

@samducker sadly we do not support z.lazy as of right now and this has been the case for a while. https://github.com/asteasolutions/zod-to-openapi/discussions/191.

The main workaround would be to use the .openapi method and provide some manual documentation on your schema (assuming this is a one off scenario).

For example:

type Category = {
  name: string;
  subcategories: Category[];
};

const categorySchema: z.ZodType<Category> = z
  .object({
    name: z.string(),
    subcategories: z
      .lazy(() => categorySchema.array())
      .openapi({
        type: 'array',
        items: {
          $ref: '#/components/Category',
        },
      }),
  })
  .openapi('Category');

We are open to the idea of making it work. However time is scarce and I cannot give an estimate as to when we would be able to include such feature considering it would take some overhaul of our codebase

AGalabov commented 1 month ago

@samducker I will close this task since it is a duplicate with #247 and has more tracking