-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
In Zod v4, the global configuration method z.config allows customizing error messages via the customError and localeError function, which currently only receives the issue parameter. The requirement is for customError localeError to support a second parameter, which provides the actual schema instance responsible for the validation error. This enables direct access to the meta information (such as title, label, etc.) attached to that schema instance and allows developers to implement consistent localized error handling logic.
Key Points:
customError(issue, schemaInstance): the second parameter is the schema instance where the error occurred, localeError accepts the same parameters.
Works for both base and chained schemas (e.g., min/max/refine), always providing the schema with its associated meta.
Compatible with localization and meta-driven error formatting, enabling easier access to meta fields like title or label for custom error messages.
z.config({
localeError,
customError(issue, schemaInstance) {
const title = schemaInstance.meta()?.title;
const message = localeError(issue, schemaInstance);
// ...
return title ? `${title} - ${message}` : message;
}
})Expected Benefits:
Simplifies multilingual and meta-driven error handling.
Resolves the problem of losing meta information in chained validations.
Makes Zod’s error handling API more consistent and easier to use for localization and customization scenarios.
Using issue.inst is not safe, as it may not necessarily point to schemaInstance.