@alt-stack/zod-openapi

中文文档
1.8.2发布于 2026/10/20MITTypeScriptESM + CJS近 30 天下载 1.3万

Convert OpenAPI schemas to Zod schemas with TypeScript code generation

原始使用文档
文档目录5 节
  1. Install
  2. Generate
  3. Branded types
  4. Empty success responses
  5. Documentation

@alt-stack/zod-openapi

Generate TypeScript interfaces, Zod 4 schemas, and typed HTTP Request/Response maps from OpenAPI JSON.

Install

pnpm add zod
pnpm add -D @alt-stack/zod-openapi

Generate

zod-openapi ./openapi.json --output ./src/generated-api.ts

input may be a local JSON path or HTTP(S) URL. The CLI also supports --registry for executing custom schema registrations, --include for inserting required imports/definitions, and --help. It always emits route maps; programmatic generation opts in with includeRoutes.

import { openApiToZodTsCode } from "@alt-stack/zod-openapi";

const source = openApiToZodTsCode(document, undefined, {
  includeRoutes: true,
});

Generated files begin with a do-not-edit banner. Change the OpenAPI source, registry/include files, or generator options and regenerate the whole file.

Branded types

Add x-altstack-brand to an identity schema to give its output a nominal type:

{ "type": "string", "format": "uuid", "x-altstack-brand": "UserId" }

The generator emits z.string().uuid().brand<"UserId">() and exports one shared UserId type, using the package's concrete type-alias convention. References, parameters, optional and nullable properties, array items, and union members retain the brand. Parse a value with its generated schema before assigning it to UserId. The TypeScript HTTP clients require parsed branded identifiers for path parameters. Query parameters and request bodies retain their schema input types, including unbranded strings. Branding adds no runtime validation beyond the underlying schema.

Names must be ASCII identifiers ([A-Za-z_][A-Za-z0-9_]*) and must not be reserved words or collide with another component type. Reusing a name with incompatible base types produces a generation error. Each use keeps its own validation constraints.

Empty success responses

A declared 2xx response without content (or with an empty content map) emits z.undefined() in the generated Response map. For example, a content-free 204 on /v1/sessions/{id}/logout produces a success branch with code: "204" and body: undefined in both the Fetch and Ky clients. JSON null remains null; non-JSON media types are not treated as an empty response.

Documentation

版本与发布记录

@alt-stack/zod-openapi 共发布 21 个版本,首次发布于 2025/11/06;过去 12 个月发布了 18 个版本。

版本发布日期距今
1.8.2latest2026/10/2018 天前
1.8.12026/10/2019 天前
1.8.02026/10/2019 天前
1.6.22026/08/202 个月前
1.6.12026/07/283 个月前
1.6.02026/07/223 个月前

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

@alt-stack/zod-openapi 是什么?

Convert OpenAPI schemas to Zod schemas with TypeScript code generation

如何安装 @alt-stack/zod-openapi?

在项目目录运行 npm install @alt-stack/zod-openapi,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 zod-openapi,可以用 npx zod-openapi 直接运行。

@alt-stack/zod-openapi 支持 TypeScript 吗?

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

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

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@alt-stack/zod-openapi 可以商用吗?

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

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

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

@alt-stack/zod-openapi 还在维护吗?

最新版本 1.8.2 发布于 2026/10/20(18 天前);至今共发布 21 个版本,其中过去 12 个月发布了 18 个;首次发布于 2025/11/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。