Error Maps
Error maps allow you to customize error messages based on the validation issue type and context.Basic Error Map
An error map is a function that receives an issue and returns a custom message:Error Map Return Values
Error maps can return:{ message: string }- Custom error messagestring- Shorthand for{ message: string }undefinedornull- Fall back to default message
Contextual Error Maps
Apply error maps to specific parse operations:Refinements with Error Maps
Schema-Bound Error Maps
Bind error maps directly to schemas:Bound vs Contextual Precedence
Schema-bound error maps take precedence over contextual ones:Global Error Configuration
Set a global custom error map usingz.config():
Error Map Issue Types
Invalid Type Issues
Size Constraint Issues
Invalid Format Issues
Custom Issues with Params
Advanced Error Map Patterns
Path-Aware Error Messages
Unrecognized Keys
Invalid Union
Combining Error Messages
Hard-Coded Message with Error Map
Error and Message Conflict
You cannot use both
message and error options together. The message option is a shorthand that takes precedence.Empty String Messages
You can explicitly set empty error messages:Best Practices
- Return undefined for defaults - Let Zod generate standard messages when appropriate
- Use params for context - Pass metadata through
paramsfor dynamic messages - Prefer schema-bound for reusable schemas - Bind error maps to schemas you’ll reuse
- Use contextual for one-off customization - Apply custom error maps at parse time for specific cases
- Avoid global error maps in libraries - They affect all Zod usage in the application
- Consider internationalization - Error maps are perfect for translating messages