跳转到内容
Host your specs. Generate from anywhere.

Zodv4

v4

此内容尚不支持你的语言。

Zod is a TypeScript-first schema validation library with static type inference.

The Zod plugin for Hey API generates schemas from your OpenAPI spec, fully compatible with validators, transformers, and all core features.

  • Zod v4 support
  • seamless integration with @hey-api/openapi-ts ecosystem
  • Zod schemas for requests, responses, and reusable definitions
  • minimal learning curve thanks to extending the underlying technology

In your configuration, add zod to your plugins and you’ll be ready to generate Zod artifacts. 🎉

openapi-ts.config.ts
export default {
input: 'hey-api/backend', // sign up at app.heyapi.dev
output: 'src/client',
plugins: [
// ...other plugins
'zod',
],
};

To add data validators to your SDKs, set sdk.validator to true.

openapi-ts.config.ts
export default {
input: 'hey-api/backend', // sign up at app.heyapi.dev
output: 'src/client',
plugins: [
// ...other plugins
'zod',
{
name: '@hey-api/sdk',
validator: true,
},
],
};

Learn more about data validators in your SDKs on the SDKs page.

The Zod plugin will generate the following artifacts, depending on the input specification.

A Zod schema is generated for every request layer of each endpoint.

zod.gen.ts
const zDeletePetHeaders = z.object({
api_key: z.string().optional(),
});
const zDeletePetPath = z.object({
petId: z.number(),
});
const zDeletePetQuery = z.object({
additionalMetadata: z.string(),
});

You can customize the naming and casing pattern for requests schemas using the .name and .case options.

A single Zod schema is generated for all endpoint’s responses. If the endpoint describes multiple responses, the generated schema is a union of all possible response shapes.

zod.gen.ts
const zResponse = z.union([
z.object({
foo: z.optional(z.string()),
}),
z.object({
bar: z.optional(z.number()),
}),
]);

You can customize the naming and casing pattern for responses schemas using the .name and .case options.

A Zod schema is generated for every reusable definition from your input.

zod.gen.ts
const zFoo = z.int();
const zBar = z.object({
bar: z.optional(z.array(z.int())),
});

You can customize the naming and casing pattern for definitions schemas using the .name and .case options.

By default, values without a timezone or with a timezone offset are not allowed in the z.iso.datetime() method.

You can allow values with timezone offsets by setting dates.offset to true.

zod.gen.ts
export const zFoo = z.iso.datetime({ offset: true });

You can allow values without a timezone by setting dates.local to true.

zod.gen.ts
export const zFoo = z.iso.datetime({ local: true });

It’s often useful to associate a schema with some additional metadata for documentation, code generation, AI structured outputs, form validation, and other purposes. You can set metadata to true to attach descriptions to schemas when available.

zod.gen.ts
export const zFoo = z.string().register(z.globalRegistry, {
description: 'Additional metadata',
});

For more control over metadata, you can provide your own function. It receives the source schema, the target node object, and the $ builder for populating metadata.

zod.gen.ts
export const zFoo = z.string().register(z.globalRegistry, {
hasTitle: true,
createdAt: 1735732800000,
});

In addition to Zod schemas, you can generate schema-specific types. These can be generated for all schemas or for specific resources.

zod.gen.ts
export type ResponseZodType = z.infer<typeof zResponse>;

You can customize the naming and casing pattern for schema-specific types using the .name and .case options.

You can further customize this plugin’s behavior using resolvers.

You can view the complete list of options in the UserConfig interface.

Examples

You can view live examples on StackBlitz or on GitHub.