@frontmcp/lazy-zod

中文文档
1.12.2发布于 2026/11/15Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 1.3万

Zod 4's z with compound schemas built on first use, for faster start-up. Exports z (lazy), eagerZ (Zod's own) and lazyZ

原始使用文档
文档目录7 节
  1. Install
  2. Usage
  3. Performance
  4. Docs
  5. Related packages
  6. Contributing
  7. License

@frontmcp/lazy-zod

Zod 4's z, with compound schemas like z.object() built the first time they're used instead of when the module loads. A process that defines many schemas but uses few of them per run, like a CLI, a serverless function or an edge worker, starts faster. This is an internal package: @frontmcp/sdk re-exports all of it, so in a FrontMCP server import z, eagerZ and lazyZ from @frontmcp/sdk. It doesn't depend on the rest of FrontMCP, so you can also use it on its own.

npm License

Install

npm install @frontmcp/lazy-zod zod

zod ^4.0.0 is a peer dependency: the package wraps the Zod you install. Zod 3 isn't supported. It needs Node.js 24 or later.

Usage

Import z from here instead of from zod:

import { eagerZ, lazyZ, toJSONSchema, z } from '@frontmcp/lazy-zod';

const Ticket = z.object({
  title: z.string().min(5),
  customer: z.object({ email: z.string().email() }).optional(),
});

type Ticket = z.infer<typeof Ticket>;

const ticket: Ticket = Ticket.parse({ title: 'Cannot log in' });

const jsonSchema = toJSONSchema(Ticket);

const Built = eagerZ.object({ id: eagerZ.string() });

const Deferred = lazyZ(() => eagerZ.object({ id: eagerZ.string() }));

z.object(), z.strictObject(), z.looseObject(), z.union(), z.discriminatedUnion(), z.intersection(), z.record() and z.tuple() return a stand-in. The real Zod schema is built the first time the stand-in is used, by a parse or by reading a property like .shape, and methods you chain on it, like .optional(), return stand-ins too. After the first parse, parse, safeParse, parseAsync and safeParseAsync call Zod directly. The other factories, like z.string() and z.enum(), are Zod's own.

A stand-in behaves like the schema it stands for: it parses the same, instanceof ZodObject is true, and z.infer gives the same type.

ExportWhat it is
z (also the default export)Zod 4's z, with the factories above deferred.
eagerZZod's own z. Use it for a schema that something reads as soon as it's defined.
lazyZ(build)A schema that calls build() the first time it's used, and only then.
internalZWhat FrontMCP's own packages build their schemas with. Every factory, leaves included, returns a stand-in, and a stand-in builds its leaf children with it. Zod's cycle check isn't attached to them, so a recursive schema is built with z. It isn't part of the public API and can change in any release: use z.
isLazy(schema)true for a stand-in, from z or from lazyZ().
forceMaterialize(schema)The real Zod schema, with every stand-in inside it built.
hasTransform(schema)true when the schema runs a transform anywhere in its tree (.transform(), z.preprocess(), .pipe(), a codec), so what it parses to can differ in shape from its input.
toJSONSchema(schema)Zod's JSON Schema converter, run on forceMaterialize(schema). Use it instead of Zod's own z.toJSONSchema(), which throws TypeError: Cannot set properties of undefined (setting 'ref') when an object or union in the schema is .optional() or has a .default().
ZodObject, ZodError, …Zod's classes and types (ZodTypeAny, infer, input, output), so your code needs no import from zod.

Performance

The prototype this package grew from was measured on 1,515 generated schemas, bundled into one file, on Node.js 24 (medians of 30 runs):

MetricZod's zLazy z
Start-up387 ms7.1 ms
First parse of one schema0.45 ms1.47 ms
First parse of every schema235 ms616 ms
Second parse of every schema31.0 ms31.0 ms
Bundle size1.69 MB1.70 MB

The work isn't removed, it's moved: each schema costs more the first time it's parsed. Lazy schemas pay off when a process starts often and uses only some of its schemas. The benchmark, with its method and full results, is in apps/poc-lazy-zod.

Docs

  • @frontmcp/sdk: re-exports this package. Use it in a FrontMCP server.
  • zod: the schema library this package wraps.

Contributing

Source: libs/lazy-zod. Issues and pull requests are welcome; see CONTRIBUTING.md.

License

Apache-2.0. See LICENSE.

版本与发布记录

@frontmcp/lazy-zod 共发布 78 个版本,首次发布于 2026/04/19;过去 12 个月发布了 78 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.12.2latest2026/11/151 天前
1.12.12026/11/132 天前
1.12.02026/11/114 天前
1.12.0-rc.1rc2026/11/114 天前
1.11.32026/11/106 天前
1.11.22026/11/096 天前

其他发布通道:beta → 1.1.2-beta.1,rc → 1.12.0-rc.1。

关于 @frontmcp/lazy-zod 的常见问题

@frontmcp/lazy-zod 是什么?

Zod 4's z with compound schemas built on first use, for faster start-up. Exports z (lazy), eagerZ (Zod's own) and lazyZ

如何安装 @frontmcp/lazy-zod?

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

@frontmcp/lazy-zod 支持 TypeScript 吗?

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

@frontmcp/lazy-zod 支持 ESM 还是 CommonJS?

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

@frontmcp/lazy-zod 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@frontmcp/lazy-zod 可以商用吗?

@frontmcp/lazy-zod 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@frontmcp/lazy-zod 的下载量有多少?

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

@frontmcp/lazy-zod 还在维护吗?

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