自定义错误
在 Zod 中,验证错误以 z.core.$ZodError 类的实例形式显示。
🌐 In Zod, validation errors are surfaced as instances of the z.core.$ZodError class.
在 zod 包中的 ZodError 类是一个子类,它实现了一些额外的便利方法。
$ZodError 的实例包含一个 .issues 数组。每个问题包含一个可读的 message 以及关于该问题的额外结构化元数据。
🌐 Instances of $ZodError contain an .issues array. Each issue contains a human-readable message and additional structured metadata about the issue.
每个问题都包含一个带有人类可读错误消息的 message 属性。错误消息可以通过多种方式自定义。
🌐 Every issue contains a message property with a human-readable error message. Error messages can be customized in a number of ways.
error 参数
🌐 The error param
几乎所有 Zod API 都接受可选的错误消息。
🌐 Virtually every Zod API accepts an optional error message.
此自定义错误将显示为源自此模式的任何验证问题的 message 属性。
🌐 This custom error will show up as the message property of any validation issues that originate from this schema.
所有 z 函数和模式方法都接受自定义错误。
🌐 All z functions and schema methods accept custom errors.
如果你愿意,你也可以传递一个带有 error 参数的 params 对象。
🌐 If you prefer, you can pass a params object with an error parameter instead.
error 参数可选择接受一个函数。Zod 术语中,错误自定义函数被称为 错误映射。如果在解析时发生验证错误,错误映射将会运行。
🌐 The error param optionally accepts a function. An error customization function is known as an error map in Zod terminology. The error map will run at parse time if a validation error occurs.
注意 — 在 Zod v3 中,message(一个字符串)和 errorMap(一个函数)有单独的参数。在 Zod 4 中,这些已统一为 error。
错误映射接收一个上下文对象,你可以使用它根据验证问题自定义错误消息。
🌐 The error map receives a context object you can use to customize the error message based on the validation issue.
对于高级情况,iss 对象提供了可用于自定义错误的额外信息。
🌐 For advanced cases, the iss object provides additional information you can use to customize the error.
根据你使用的 API,可能会有额外的属性可用。使用 TypeScript 的自动补齐功能来探索可用的属性。
🌐 Depending on the API you are using, there may be additional properties available. Use TypeScript's autocomplete to explore the available properties.
返回 undefined 以避免自定义错误消息并回退到默认消息。(更具体来说,Zod 将将控制权交给 优先级链 中的下一个错误映射。)这对于有选择地自定义某些错误消息而不修改其他消息非常有用。
🌐 Return undefined to avoid customizing the error message and fall back to the default message. (More specifically, Zod will yield control to the next error map in the precedence chain.) This is useful for selectively customizing certain error messages but not others.
每次解析错误自定义
🌐 Per-parse error customization
要在每次解析时自定义错误,请将错误映射传入解析方法:
🌐 To customize errors on a per-parse basis, pass an error map into the parse method:
这比任何模式级自定义消息的优先级都低。
🌐 This has lower precedence than any schema-level custom messages.
iss 对象是所有可能问题类型的 判别联合。使用 code 属性来区分它们。
🌐 The iss object is a discriminated union of all possible issue types. Use the code property to discriminate between them.
有关所有 Zod 问题代码的详细说明,请参阅 zod/v4/core 文档。
在问题中包含输入
🌐 Include input in issues
默认情况下,Zod 不会在问题中包含输入数据。这是为了防止意外记录可能包含敏感信息的输入数据。要在每个问题中包含输入数据,请使用 reportInput 标志:
🌐 By default, Zod does not include input data in issues. This is to prevent unintentional logging of potentially sensitive input data. To include the input data in each issue, use the reportInput flag:
全局错误自定义
🌐 Global error customization
要指定全局错误映射,请使用 z.config() 设置 Zod 的 customError 配置项:
🌐 To specify a global error map, use z.config() to set Zod's customError configuration setting:
全局错误消息的优先级低于模式级或每次解析的错误消息。
🌐 Global error messages have lower precedence than schema-level or per-parse error messages.
iss 对象是所有可能问题类型的 判别联合。使用 code 属性来区分它们。
🌐 The iss object is a discriminated union of all possible issue types. Use the code property to discriminate between them.
有关所有 Zod 问题代码的详细说明,请参阅 zod/v4/core 文档。
国际化
🌐 Internationalization
为了支持错误信息的国际化,Zod 提供了几个内置的语言环境。它们从 zod/v4/core 包中导出。
🌐 To support internationalization of error message, Zod provides several built-in locales. These are exported from the zod/v4/core package.
注意 — 常规的 zod 库会自动加载 en 语言环境。Zod Mini 默认不加载任何语言环境;相反,所有错误信息默认使用 Invalid input。
要延迟加载语言环境,请考虑使用动态导入:
🌐 To lazily load a locale, consider dynamic imports:
为了方便,所有区域设置都从 "zod" 导出为 z.locales。在某些打包工具中,这可能无法进行 tree-shaking。
🌐 For convenience, all locales are exported as z.locales from "zod". In some bundlers, this may not be tree-shakable.
语言环境
🌐 Locales
以下语言环境可用:
🌐 The following locales are available:
ar— 阿拉伯语az— 阿塞拜疆语be— 白俄罗斯语bg— 保加利亚语ca— 加泰罗尼亚语cs— 捷克da— 丹麦语de— 德语en— 英语eo— 世界语es— 西班牙语fa— 波斯语fi— 芬兰语fr— 法语frCA— 加拿大法语he— 希伯来语hu— 匈牙利语hy— 亚美尼亚人id— 印度尼西亚语is— 冰岛语it— 意大利语ja— 日语ka— 格鲁吉亚语km— 柬埔寨语ko— 韩语lt— 立陶宛语mk— 马其顿语ms— 马来语nl— 荷兰语no— 挪威语ota— 土耳其语ps— 普什图语pl— 波兰语pt— 葡萄牙语ru— 俄语sl— 斯洛文尼亚语sv— 瑞典语ta— 泰米尔语th— 泰语tr— 土耳其语uk— 乌克兰语ur— 乌尔都语uz— 乌兹别克语vi— 越南语zhCN— 简体中文zhTW— 繁体中文yo— 约鲁巴语
错误优先级
🌐 Error precedence
以下是确定错误优先级的快速参考:如果定义了多个错误自定义,哪个优先?从最高到最低优先级:
🌐 Below is a quick reference for determining error precedence: if multiple error customizations have been defined, which one takes priority? From highest to lowest priority:
- 模式级错误 — 任何“硬编码”到模式定义中的错误信息。
- 每解析错误 — 一个传入
.parse()方法的自定义错误映射。
- 全局错误映射 — 一个传入
z.config()的自定义错误映射。
- 本地错误映射 — 一个传入
z.config()的自定义错误映射。

