Reuse schemas in OpenAPI with `.meta({ id: ... })`
mainYou can externalize and reuse schemas in your OpenAPI documentation by adding .meta({ id: "SchemaName" }) to a Zod schema. This adds the schema directly to components.schemas in the OpenAPI output.
Naming Logic
- Input Schemas: Use the
idprovided in.meta({ id: 'MySchema' }). - Output Schemas:
nestjs-zodautomatically suffixes theidwith_Output(e.g.,MySchema_Output) to prevent collisions with input schemas. - Titles: If
.meta({ title: 'CustomTitle' })is used, that title is used instead of the ID.
const Author = z.object({ name: z.string() }).meta({ id: "Author" })
class BookDto extends createZodDto(z.object({ title: z.string(), author: Author })) { }
// Result: 'Author' will be a reusable component in OpenAPI