Zod logo

Zod Mini

zod/v4-mini 变体随 Zod 4 的发布而引入。要尝试:

¥The zod/v4-mini variant was introduced with the release of Zod 4. To try it:

npm install zod@^3.25.0

它实现了与 zod 完全相同的功能,但使用了函数式的、可摇树优化的 API。如果你来自 zod,这意味着你通常会使用函数代替方法。

¥It implements the exact same functionality as zod, but using a functional, tree-shakable API. If you're coming from zod, this means you generally will use functions in place of methods.

import { z } from "zod/v4-mini"
 
const mySchema = z.nullable(z.optional(z.string()));

摇树优化 - 在 zod 中,模式提供了一系列便捷的方法来执行一些常见操作(例如,.min() 中对字符串模式的操作)。即使未使用 ("树抖动") 方法实现,打包器也很难从打包中移除它们。删除未使用的顶层函数要容易得多,这就是 zod/v4-mini 的 API 使用函数多于方法的原因。

¥Tree-shaking — In zod, schemas provide a range of convenience methods to perform some common operations (e.g. .min() on string schemas). Bundlers have a hard time removing ("tree shaking") method implementations from your bundle, even if they aren't used. It's much easier to remove an unused top-level function, which is why the API of zod/v4-mini uses more functions than methods.

ZodMiniType

所有 zod/v4-mini 模式都扩展了 z.ZodMiniType 基类,而 z.ZodMiniType 基类又从 zod/v4/core 扩展了 z.core.$ZodType。虽然此类实现的方法比 zod 中的 ZodType 少得多,但仍保留了一些特别有用的方法。

¥All zod/v4-mini schemas extend the z.ZodMiniType base class, which in turn extends z.core.$ZodType from zod/v4/core. While this class implements far fewer methods than ZodType in zod, some particularly useful methods remain.

.parse

这是一个显而易见的目标。所有 zod/v4-mini 模式都实现了与 zod 相同的解析方法。

¥This is an obvious one. All zod/v4-mini schemas implement the same parsing methods as zod.

import { z } from "zod/v4-mini"
 
const mySchema = z.string();
 
mySchema.parse('asdf')
await mySchema.parseAsync('asdf')
mySchema.safeParse('asdf')
await mySchema.safeParseAsync('asdf')

.check()

zod/v4 中,架构子类中有专用方法用于执行常见检查:

¥In zod/v4 there are dedicated methods on schema subclasses for performing common checks:

import { z } from "zod/v4";
 
z.string()
  .min(5)
  .max(10)
  .refine(val => val.includes("@"))
  .trim()

zod/v4-mini 中,此类方法尚未实现。相反,你可以使用 .check() 方法将这些检查传递到模式中:

¥In zod/v4-mini such methods aren't implemented. Instead you pass these checks into schemas using the .check() method:

import { z } from "zod/v4-mini"
 
z.string().check(
  z.minLength(5), 
  z.maxLength(10),
  z.refine(val => val.includes("@")),
  z.trim()
);

已实现以下检查。其中一些检查仅适用于某些类型的模式(例如字符串或数字)。所有 API 均为类型安全;TypeScript 不允许你向架构添加不受支持的检查。

¥The following checks are implemented. Some of these checks only apply to schemas of certain types (e.g. strings or numbers). The APIs are all type-safe; TypeScript won't let you add an unsupported check to your schema.

z.lt(value);
z.lte(value); // alias: z.maximum()
z.gt(value);
z.gte(value); // alias: z.minimum()
z.positive();
z.negative();
z.nonpositive();
z.nonnegative();
z.multipleOf(value);
z.maxSize(value);
z.minSize(value);
z.size(value);
z.maxLength(value);
z.minLength(value);
z.length(value);
z.regex(regex);
z.lowercase();
z.uppercase();
z.includes(value);
z.startsWith(value);
z.endsWith(value);
z.property(key, schema);
z.mime(value);
 
// custom checks
z.refine()
z.check()   // replaces .superRefine()
 
// mutations (these do not change the inferred types)
z.overwrite(value => newValue);
z.normalize();
z.trim();
z.toLowerCase();
z.toUpperCase();

.register()

要在 registry 中注册模式。

¥For registering a schema in a registry.

const myReg = z.registry<{title: string}>();
 
z.string().register(myReg, { title: "My cool string schema" });

.brand()

用于为 Schema 打上品牌烙印。有关核心子库内容的更多信息,请参阅 品牌类型 文档。

¥For branding a schema. Refer to the Branded types docs for more information.

import { z } from "zod/v4-mini"
 
const USD = z.string().brand("USD");

.clone(def)

使用提供的 def 返回当前模式的相同克隆。

¥Returns an identical clone of the current schema using the provided def.

const mySchema = z.string()
 
mySchema.clone(mySchema._zod.def);

无默认语言环境

¥No default locale

虽然 zod/v4 会自动加载英语(en)语言环境,但 zod/v4-mini 不会。在错误消息不必要、本地化为非英语语言或其他自定义的情况下,这可以减少包的大小。

¥While zod/v4 automatically loads the English (en) locale, zod/v4-mini does not. This reduces the bundle size in scenarios where error messages are unnecessary, localized to a non-English language, or otherwise customized.

这意味着,默认情况下,所有问题的 message 属性都将直接读取 "Invalid input"。要加载英语语言环境:

¥This means, by default the message property of all issues will simply read "Invalid input". To load the English locale:

import { z } from "zod/v4-mini"
 
z.config(z.locales.en());

有关更多信息,请参阅 语言环境 文档。

¥Refer to the Locales docs for more on localization.

On this page