skip to content

In Zod 4, z.string("Username is required").min(3) reports "Username is required" for "ab", and the error map passed to safeParse is ignored. Why?

level: seniorimportance: should knowfreq 30%

answer

  1. most specific map wins
  2. check, schema, parse, global, locale
  3. schema text covers its checks
  4. per-parse lost rank in Zod 4

basics

~20 s

Zod 4 picks the most specific message: check-level, then schema-level, then the per-parse map, then the global customError, then the locale. A schema-level string covers its own checks that lack a message, and outranks the per-parse map.

solid answer

~50 s

Zod 4 resolves a message by walking a fixed chain from most to least specific, and the first map that returns text wins: the failed check's own `error`, then the owning schema's `error`, then the error map passed to `parse`/`safeParse`, then `customError` from `z.config`, then the locale's map, and finally the literal "Invalid input". Here `.min(3)` defines no message, so the next candidate is the schema-level string "Username is required", which also covers issues raised by that schema's own checks. The per-parse map sits below both, so it never runs. In Zod 3 a per-parse map outranked schema-level maps; Zod 4 reversed that. The fix is to put the rule's text on the check, `.min(3, "At least 3 characters")`, or make the schema-level error a function that returns its text only when `iss.input === undefined` and `undefined` otherwise.

code

typescript · 15 lines
typescript
import * as z from "zod";

const onParse = { error: () => "From the parse call" };

const Trap = z.string("Username is required").min(3);
console.log(Trap.safeParse("ab", onParse).error?.issues[0].message);
// Username is required

const Fixed = z
  .string({ error: (iss) => (iss.input === undefined ? "Username is required" : undefined) })
  .min(3, "At least 3 characters");
console.log(Fixed.safeParse("ab", onParse).error?.issues[0].message);
// At least 3 characters
console.log(Fixed.safeParse(42, onParse).error?.issues[0].message);
// From the parse call

go deeper

for a junior

Recall that a message on a check like min beats a message on the schema, and that both beat Zod's default English text.

for a middle

Explain the full chain from check to locale and why returning undefined from an error function lets the next level answer.

for a senior

Diagnose a wrong message by walking the chain from the failing check outward, and catch Zod 3 code that expected the per-parse map to win.

for a principal

Set a team convention for where wording lives, because schema-level strings outrank every caller-supplied map and quietly block later translation or caller-specific text.

## The symptom A sign-up schema declares `username: z.string("Username is required").min(3)`. A user types "ab", and the form shows **"Username is required"**, which is false: they typed something. Someone adds a per-parse error map to fix the wording, and nothing changes. Both effects come from the same rule: **error precedence**. ## The precedence chain in Zod 4 When an issue arises, Zod 4 asks a series of error maps for a message, from most specific to least, and takes the first one that returns text (a function returning `undefined` passes its turn). 1. **Check-level error**: the `error` on the check that failed, such as `.min(3, "Too short")`. 2. **Schema-level error**: the `error` on the schema, such as `z.string("Invalid name")`. It covers issues raised by the schema itself *and* by its own checks when those checks define no error. 3. **Per-parse error**: a map passed as `schema.safeParse(input, { error })`. 4. **Global error map**: `customError` set through `z.config({ customError })`. 5. **Locale error map**: the locale set through `z.config(z.locales.xx())`; the `zod` package registers English by default. 6. If nothing returns text, the message is the literal **"Invalid input"**. An issue that was raised with an explicit message already set skips this chain entirely; that is how custom checks attach their own wording. ## Walking through the example | Step | Candidate | Result | |---|---|---| | 1 | `.min(3)` check error | none defined | | 2 | schema error `"Username is required"` | returns text, wins | | 3 | per-parse map | never asked | | 4-5 | global, locale | never asked | The schema-level string was meant for the missing-value case, but a string cannot know which issue it is answering. It answers all of them for that schema, including `too_small` from its own check. ## Fixes - **Put rule text on the rule.** `z.string("Username is required").min(3, "At least 3 characters")`. The check's own message outranks the schema's. - **Make the schema-level error selective.** A function can answer only the case it is about and return `undefined` for the rest, so lower maps, including the per-parse map, get their turn: ```ts z.string({ error: (iss) => (iss.input === undefined ? "Username is required" : undefined) }).min(3); ``` - **Keep schemas free of text** if wording should come from the call site or the locale, and let the per-parse map or `customError` decide it. ## Why the per-parse map lost In Zod 3, an error map passed to `.parse()` took precedence over a schema-level map. Zod 4 changed this so that precedence runs strictly by specificity: something written on the schema beats something supplied by a caller. Code migrated from Zod 3 that relied on a per-parse map to override schema text (often for translation) silently stops overriding it. ## Diagnosing a wrong message A short procedure when a message is not the one you expect: 1. Find the issue's `code` and `path`; they tell you which schema and which check raised it. 2. Look at that check for an `error`. If present, it wins. 3. Look at the owning schema for an `error`, including the shorthand string argument. 4. Only then look at the parse call, `z.config({ customError })` and the configured locale. 5. Remember that a function in the chain returning `undefined` is skipped, and one returning a string for every code shadows everything below it. ## Global maps and metadata A global `customError` can word messages from schema metadata, because the raw issue carries `iss.schema`, the schema that owns it. Zod's documentation shows reading a `title` registered with `.meta()` through `z.globalRegistry` and returning "Password is invalid." for a field titled Password. Two cautions apply: - a global map that returns text for **every** code shadows the locale completely, so return `undefined` for codes it does not handle; - anything written on a schema or check still beats it, so it only fills gaps. ## Design guidance - Schema-level strings are fine for single-rule schemas, or for messages true of every failure ("Must be a valid email"). - When a schema has several checks, give each check its own message or use a function keyed on `iss.code`. - If translation or caller-specific wording matters, keep hard-coded strings off shared schemas, because they outrank every map a caller can pass. The behaviour looks like a bug the first time, but it is the documented precedence: specificity first, and the parse call is less specific than the schema.

  • In Zod 4, where does the global customError sit relative to a locale set with z.config?
    Above it. Both live in the same global config, but `customError` is consulted first and the locale's map is the lowest-priority source before the literal "Invalid input". A `customError` that returns text for every code therefore hides the locale completely; returning `undefined` for codes it does not handle keeps the locale's wording for them.
  • What breaks when a Zod 3 codebase relied on a per-parse error map for translation?
    Any schema that carries its own `error` text now keeps that text, because Zod 4 ranks schema-level errors above the per-parse map. Translations appear only on schemas without hard-coded messages. The fix is to remove literal strings from shared schemas or turn them into functions that call the translation helper.

  • An error map passed to parse overrides every message on the schema.
  • A schema-level error string applies only to type failures, never to its checks.
  • The global customError beats messages written on the schema.
  • The locale is consulted before any custom error map.
  • Precedence goes to whichever customisation was set most recently.

Assumes Zod 4; Zod 4 made schema-level errors outrank an error map passed to parse, which took precedence in Zod 3.