Metadata and registries
It's often useful to associate a schema with some additional metadata for documentation, code generation, AI structured outputs, form validation, and other purposes.
Registries
Metadata in Zod is handled via registries. Registries are collections of schemas, each associated with some strongly-typed metadata. To create a simple registry:
To register, lookup, and remove schemas from this registry:
TypeScript enforces that the metadata for each schema matches the registry's metadata type.
.register()
Note — This method is special in that it does not return a new schema; instead, it returns the original schema. No other Zod method does this! That includes .meta()
and .describe()
(documented below) which return a new instance.
Schemas provide a .register()
method to more conveniently add it to a registry.
This lets you define metadata "inline" in your schemas.
If a registry is defined without a metadata type, you can use it as a generic "collection", no metadata required.
Metadata
z.globalRegistry
For convenience, Zod provides a global registry (z.globalRegistry
) that can be used to store metadata for JSON Schema generation or other purposes. It accepts the following metadata:
To register some metadata in z.globalRegistry
for a schema:
.meta()
For a more convenient approach, use the .meta()
method to register a schema in z.globalRegistry
.
Calling .meta()
without an argument will retrieve the metadata for a schema.
.describe()
The .describe()
method still exists for compatibility with Zod 3, but .meta()
is now the recommended approach.
The .describe()
method is a shorthand for registering a schema in z.globalRegistry
with just a description
field.
Custom registries
You've already seen a simple example of a custom registry:
Let's look at some more advanced patterns.
Referencing inferred types
It's often valuable for the metadata type to reference the inferred type of a schema. For instance, you may want an examples
field to contain examples of the schema's output.
The special symbol z.$output
is a reference to the schemas inferred output type (z.infer<typeof schema>
). Similarly you can use z.$input
to reference the input type.
Constraining schema types
Pass a second generic to z.registry()
to constrain the schema types that can be added to a registry. This registry only accepts string schemas.