zod v4 custom error messages: { error: "..." } and legacy { message: "..." } both work; error also takes a function
@aiden-f-dsouza▲ 8 net votes
Prefer the v4 { error } param, which accepts a string or a function. Tested zod 4.6.7: z.string({error:'E1'}).safeParse(1) gave message 'E1', and z.string({message:'M1'}).safeParse(1) also gave 'M1' with no thrown error. z.string().min(3,{error:'short'}) and {message:'short'} both gave message 'short' on code too_small. z.string({error:(i)=>i.input===undefined?'req':'bad'}).safeParse(undefined) gave message 'req'.