自定义错误
在 Zod 中,验证错误会显示为 z.core.$ZodError
类的实例。
¥In Zod, validation errors are surfaced as instances of the z.core.$ZodError
class.
zod
包中的 ZodError
类是实现一些额外便捷方法的子类。
¥The ZodError
class in the zod
package is a subclass that implements some additional convenience methods.
$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.
此自定义错误将显示为源自此 Schema 的任何验证问题的 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
。
¥Note — In Zod v3, there were separate params for message
(a string) and errorMap
(a function). These have been unified in Zod 4 as 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
文档。
¥For a breakdown of all Zod issue codes, see the zod/v4/core
documentation.
在问题中包含输入
¥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
文档。
¥For a breakdown of all Zod issue codes, see the zod/v4/core
documentation.
国际化
¥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
。
¥— The regular zod
library automatically loads the en
locale automatically. Zod Mini does not load any locale by default; instead all error messages default to Invalid input
.
要延迟加载语言环境,请考虑使用动态导入:
¥To lazily load a locale, consider dynamic imports:
为了方便起见,所有语言环境都从 "zod"
导出为 z.locales
。在某些 bundler 中,这可能无法进行 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
— 阿拉伯语¥
ar
— Arabic -
az
— 阿塞拜疆语¥
az
— Azerbaijani -
be
— 白俄罗斯语¥
be
— Belarusian -
ca
— 加泰罗尼亚语¥
ca
— Catalan -
cs
— 捷克语¥
cs
— Czech -
de
— 德语¥
de
— German -
en
— 英语¥
en
— English -
eo
— 世界语¥
eo
— Esperanto -
es
— 西班牙语¥
es
— Spanish -
fa
— 波斯语¥
fa
— Farsi -
fi
— 芬兰语¥
fi
— Finnish -
fr
— 法语¥
fr
— French -
frCA
— 加拿大法语¥
frCA
— Canadian French -
he
— 希伯来语¥
he
— Hebrew -
hu
— 匈牙利语¥
hu
— Hungarian -
id
— 印尼语¥
id
— Indonesian -
it
— 意大利语¥
it
— Italian -
ja
— 日语¥
ja
— Japanese -
kh
— 高棉语¥
kh
— Khmer -
ko
— 韩语¥
ko
— Korean -
mk
— 马其顿语¥
mk
— Macedonian -
ms
— 马来语¥
ms
— Malay -
nl
— 荷兰语¥
nl
— Dutch -
no
— 挪威语¥
no
— Norwegian -
ota
— 土耳其语¥
ota
— Türkî -
ps
— 普什图语¥
ps
— Pashto -
pl
— 波兰语¥
pl
— Polish -
pt
—葡萄牙语¥
pt
— Portuguese -
ru
— 俄语¥
ru
— Russian -
sl
— 斯洛文尼亚语¥
sl
— Slovenian -
sv
— 瑞典语¥
sv
— Swedish -
ta
— 泰米尔语¥
ta
— Tamil -
th
— 泰语¥
th
— Thai -
tr
— 土耳其语¥
tr
— Türkçe -
ua
— 乌克兰语¥
ua
— Ukrainian -
ur
— 乌尔都语¥
ur
— Urdu -
vi
— 越南语¥
vi
— Tiếng Việt -
zhCN
— 简体中文¥
zhCN
— Simplified Chinese -
zhTW
— 繁体中文¥
zhTW
— Traditional Chinese
错误优先级
¥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:
-
模式级错误 - 任何错误消息 "硬编码" 都会进入模式定义。
¥Schema-level error — Any error message "hard coded" into a schema definition.
-
每次解析错误 - 传递给
.parse()
方法的自定义错误映射。¥Per-parse error — A custom error map passed into the
.parse()
method.
-
全局错误映射 - 传递给
z.config()
的自定义错误映射。¥Global error map — A custom error map passed into
z.config()
.
-
区域设置错误映射 - 传入
z.config()
的自定义错误映射。¥Locale error map — A custom error map passed into
z.config()
.