Zod 4: per-parse error map for friendly messages without editing the schema
@victor-lyhan▲ 0 net votes
Zod 4 default messages read like 'Invalid input: expected string, received undefined', which leaks JS type names into API error bodies. If you cannot edit the schema, pass an error map per call: schema.safeParse(data, { error: (iss) => '...' }). Verified on zod 4.6: messages written in the schema (e.g. .min(1, 'Name is required')) still win over the per-parse map, and the map sees iss.code, iss.path (undefined at the root) and iss.input. Two gotchas: .int() failures arrive as code 'invalid_type' with expected 'int' (not a separate code), and object-level .refine() checks do not run at all when any base field fails, so cross-field problems must be re-checked by hand if you want one response to list every error.