@anatine/zod-openapi

中文文档
2.2.8发布于 2025/04/04MITTypeScriptCommonJS近 30 天下载 280.9万

Zod to OpenAPI converter

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. Take any Zod schema and convert it to an OpenAPI JSON object
  4. Extend a Zod schema with additional OpenAPI schema via a fu…
  5. Credits
  6. express-zod-api
  7. zod-dto

@anatine/zod-openapi

Converts a Zod schema to an OpenAPI SchemaObject as defined by openapi3-ts


Installation

Both openapi3-ts and zod are peer dependencies instead of dependant packages. While zod is necessary for operation, openapi3-ts is for type-casting.

npm install openapi3-ts zod @anatine/zod-openapi

Usage

Take any Zod schema and convert it to an OpenAPI JSON object

import { generateSchema } from '@anatine/zod-openapi';
const aZodSchema = z.object({
  uid: z.string().nonempty(),
  firstName: z.string().min(2),
  lastName: z.string().optional(),
  email: z.string().email(),
  phoneNumber: z.string().min(10).optional(),
})
const myOpenApiSchema = generateSchema(aZodSchema);
// ...

This will generate an OpenAPI schema for myOpenApiSchema

{
  "type": "object",
  "properties": {
    "uid": {
      "type": "string",
      "minLength": 1
    },
    "firstName": {
      "type": "string",
      "minLength": 2
    },
    "lastName": {
      "type": "string"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "phoneNumber": {
      "type": "string",
      "minLength": 10
    }
  },
  "required": [
    "uid",
    "firstName",
    "email"
  ]
}

Extend a Zod schema with additional OpenAPI schema via a function wrapper

import { extendApi, generateSchema } from '@anatine/zod-openapi';
const aZodExtendedSchema = extendApi(
      z.object({
        uid: extendApi(z.string().nonempty(), {
          title: 'Unique ID',
          description: 'A UUID generated by the server',
        }),
        firstName: z.string().min(2),
        lastName: z.string().optional(),
        email: z.string().email(),
        phoneNumber: extendApi(z.string().min(10), {
          description: 'US Phone numbers only',
          example: '555-555-5555',
        }),
      }),
      {
        title: 'User',
        description: 'A user schema',
      }
    );
const myOpenApiSchema = generateSchema(aZodExtendedSchema);
// ...

... or via extension of the Zod schema:

import { extendApi, generateSchema, extendZodWithOpenApi } from '@anatine/zod-openapi';
import {z} from 'zod';

extendZodWithOpenApi(z);

const aZodExtendedSchema = 
      z.object({
        uid: z.string().nonempty().openapi({
          title: 'Unique ID',
          description: 'A UUID generated by the server',
        }),
        firstName: z.string().min(2),
        lastName: z.string().optional(),
        email: z.string().email(),
        phoneNumber: z.string().min(10).openapi({
          description: 'US Phone numbers only',
          example: '555-555-5555',
        }),
      }).openapi(
      {
        title: 'User',
        description: 'A user schema',
      }
    );
const myOpenApiSchema = generateSchema(aZodExtendedSchema);
// ...

This will generate an extended schema:

{
  "type": "object",
  "properties": {
    "uid": {
      "type": "string",
      "minLength": 1,
      "title": "Unique ID",
      "description": "A UUID generated by the server"
    },
    "firstName": {
      "type": "string",
      "minLength": 2
    },
    "lastName": {
      "type": "string"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "phoneNumber": {
      "type": "string",
      "minLength": 10,
      "description": "US Phone numbers only",
      "example": "555-555-5555"
    }
  },
  "required": [
    "uid",
    "firstName",
    "email",
    "phoneNumber"
  ],
  "title": "User",
  "description": "A user schema"
}

Credits

  • express-zod-api

    A great lib that provided some insights on dealing with various zod types.

  • zod-dto

    Lib providing insights into using Zod with NestJS


This library is part of a nx monorepo @anatine/zod-plugins.

版本与发布记录

@anatine/zod-openapi 共发布 47 个版本,首次发布于 2021/07/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.2.8latest2025/04/041.6 年前
2.2.72025/01/201.8 年前
2.2.62024/06/212 年前
2.2.52024/03/202 年前
2.2.42024/03/202 年前
2.2.32024/01/242 年前

关于 @anatine/zod-openapi 的常见问题

@anatine/zod-openapi 是什么?

Zod to OpenAPI converter

如何安装 @anatine/zod-openapi?

在项目目录运行 npm install @anatine/zod-openapi,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:openapi3-ts(^4.1.2)、zod(^3.20.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@anatine/zod-openapi 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@anatine/zod-openapi 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@anatine/zod-openapi 可以商用吗?

@anatine/zod-openapi 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@anatine/zod-openapi 的下载量有多少?

根据 npm 官方下载统计,@anatine/zod-openapi 在 2026/10/06 – 2026/11/04 这 30 天内被下载 2,809,002 次,日均约 93,633 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@anatine/zod-openapi 还在维护吗?

最新版本 2.2.8 发布于 2025/04/04(1.6 年前);至今共发布 47 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/07/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。