@duplojs/zod-accelerator

中文文档
2.6.2发布于 2025/02/26MITTypeScriptESM + CJS零依赖近 30 天下载 3020

NPM version

原始使用文档
文档目录10 节
  1. Installation
  2. Benchmark
  3. How to use it
  4. When should I use it
  5. How does it work
  6. Support for Zod types
  7. The differences
  8. How custom functions are created
  9. Caching system?
  10. Skipping eval ?

@duplojs/zod-accelerator

NPM version

Installation

npm i @duplojs/zod-accelerator

Benchmark

Benchmarck result

How to use it

import * as zod from "zod";
import {ZodAccelerator} from "@duplojs/zod-accelerator";

const zodSchema = zod.object({
    firstname: zod.string(),
    lastname: zod.string(),
    age: zod.number(),
    email: zod.string(),
    gender: zod.enum(["boy", "girl"]),
    connected: zod.boolean(),
    createdAt: zod.date(),
    addresse: zod.object({
        postCode: zod.string(),
        city: zod.string(),
        number: zod.number()
    }),
}).array();

const zodAccelerateSchema = ZodAccelerator.build(zodSchema);

const inputData = Array.from({length: 10}).fill({
    firstname: "  Mike ",
    lastname: "ee",
    age: 21,
    email: "[email protected]",
    gender: "girl",
    connected: true,
    createdAt: new Date(),
    addresse: {
        postCode: "22778",
        city: "Paris",
        number: 67
    },
});

const outputData = zodAccelerateSchema.parse(inputData);

When should I use it

ZodAccelerator is useful when the schema is used multiple times during the same session. The time to build the custom function is about twice as long as its execution. If you only need to call your schema once, it might not be the best solution.

How does it work

The ZodAccelerator.build function allows you to create a custom function based on the schema passed as an argument. The function will be used to create an instance of the ZodAcceleratorParser object. This makes its usage similar to Zod's.

zodSchema.parse(...)
zodAccelerateSchema.parse(...)

zodSchema.parseAsync(...)
zodAccelerateSchema.parseAsync(...)

zodSchema.safeParse(...)
zodAccelerateSchema.safeParse(...)

zodSchema.safeParseAsync(...)
zodAccelerateSchema.safeParseAsync(...)

Support for Zod types

To build the custom function, we have created a converter for each type. Not all types are supported. However, to avoid compatibility issues, when a type is not supported, ZodAccelerator directly uses the Zod schema. To find out which types are supported, go to the scripts/accelerators folder.

The differences

The functions created by ZodAccelerator have some key differences compared to Zod's basic functionality :

  • In case of an issue, the schema analysis does not continue, it stops immediately.
  • Errors are not as explicit.
  • All secondary parameters, such as custom messages, may not be implemented everywhere (can be done on request).
  • The Zod contexts of zodEffect are not as complete.

How custom functions are created

For each type that Zod provides, we have an algorithm that translates the information from the Zod schema into a string of code. At the end, the different relevant parts are assembled and interpreted using the eval function (no injection possible).

Caching system?

Would it be possible to build the schemas in a file and then call them instead of rebuilding them? Unfortunately, no. The construction is mandatory because, in addition to the content, we also build its context (this). In the case of a ZodEffect type, for example, the associated function is stored in the context. Therefore, skipping the construction phase is not possible.

Skipping eval ?

As mentioned earlier, ZodAccelerator uses the eval function to interpret the code after a Node program is launched. Currently, no other method seems viable.

版本与发布记录

@duplojs/zod-accelerator 共发布 33 个版本,首次发布于 2024/03/08;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.6.2latest2025/02/261.7 年前
2.6.12025/02/251.7 年前
2.6.02025/02/251.7 年前
2.5.32024/12/091.9 年前
2.5.3-betabeta2024/12/091.9 年前
2.5.22024/11/292 年前

关于 @duplojs/zod-accelerator 的常见问题

如何安装 @duplojs/zod-accelerator?

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

@duplojs/zod-accelerator 支持 TypeScript 吗?

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

@duplojs/zod-accelerator 支持 ESM 还是 CommonJS?

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

@duplojs/zod-accelerator 可以商用吗?

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

@duplojs/zod-accelerator 的下载量有多少?

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

@duplojs/zod-accelerator 还在维护吗?

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