Messages and languages
Where the validation messages come from, and how to change them.
The generated schemas carry no messages. Valibot and Zod produce the message when they validate, so you configure messages in the library.
Valibot
Load a language from @valibot/i18n and select it.
import "@valibot/i18n/ja";
import * as v from "valibot";
v.setGlobalConfig({ lang: "ja" });Replace the message of one action with setSpecificMessage.
v.setSpecificMessage(v.email, "メールアドレスの形式で入力してください", "ja");Zod
Set a locale with z.config.
import * as z from "zod";
z.config(z.locales.ja());Replace messages with an error map in z.config({ customError }).
Switch the language at run time
Both settings are global, and the libraries read them when they validate. To switch the language, set both and validate the form again.
export function applyLang(lang: "en" | "ja"): void {
v.setGlobalConfig({ lang });
z.config(lang === "ja" ? z.locales.ja() : z.locales.en());
}With react-hook-form, call trigger() after the switch when the form already shows errors. The example app does this in src/forms/ReactHookForm.tsx.
The example keeps its labels in a typed object. In an application with an i18n library, call the two settings from its language-change hook.
Checks without a standard action
A rule that has no standard action uses v.check or z.refine. These report the generic message of the library, such as "Invalid input". Rule mapping lists them. repeated.unique is the common case.
To give such a field its own message, show a message of your own when the issue type is check in Valibot or the issue code is custom in Zod.