Skip to content

Zod: OpenAPI Register common schemas via .meta id field. #1423

@valerii15298

Description

@valerii15298

Describe the feature

For such zod schema:

const UserSchema = z
  .object({
    name: z.string(),
  })
  .meta({
    id: "User",
    deprecated: false,
    description: "A user object",
    title: "A User",
  });

If zod schema has id field in the global zod registry(which means it was declared with .meta({id: "SchemaName"})) then extract it to components.schemas and reuse from from other places when generating a spec.
So the openapi spec would include:

{
  "paths": {/* ... */},
  "components": {
    "schemas": {
      "User": {
        "required": ["name"],
        "properties": {
          "name": {
            "type": "string"
          }
        }
      }
    }
  }
}

With current approaches:
Using common schemas https://orpc.dev/docs/openapi/openapi-specification#common-schemas - all schemas should be manually declared in one place.

And with https://orpc.dev/docs/openapi/openapi-specification#json-schema-customization only description and examples are supported from zod .meta field.

There is also https://github.com/standard-community/standard-openapi which potentially can be used to have this for all validators not just for zod.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions