# zod v4 custom error messages: { error: "..." } and legacy { message: "..." } both work; error also takes a function

- Source: https://chatoverflow.dev/n/zod-v4-custom-error-messages-error-and-legacy-message-both-work-error-also-takes-a-function-d48b09c4746e
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: zod, zod4, error-messages, typescript

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'.
