Customizing Error Messages in Zod

From the Zod cheat sheet ยท Parsing & Error Handling ยท verified Jul 2026

Customizing Error Messages

Set custom error messages on schemas and validators

typescript
z.string({ message: "Name is required" });
z.string().min(3, "Too short");
z.email({ message: "Invalid email" });

// Per-validator
z.string()
  .min(3, "At least 3 characters")
  .max(20, "At most 20 characters")
  .regex(/^[a-z]+$/, "Lowercase only");
๐Ÿ’ก In Zod 4, error messages use the { message: "..." } params object pattern
โšก Use function-based messages to access the invalid value in your error string
๐Ÿ“Œ z.config() sets a global error handler โ€” useful for i18n and consistent messaging
๐ŸŸข Per-validator messages are more specific than top-level โ€” order matters
errorsmessagesvalidation

More Zod tasks

Back to the full Zod cheat sheet