zod
zod copied to clipboard
Error message based on input value
Issue: #3048
Generate error message based on the input value
z
.string({ invalid_type_error: value => 'Expected a string, received: ' + String(value) })
.min(2, { message: value => 'Expected a string with at least 2 characters, received: ' + String(value) })
Deploy Preview for guileless-rolypoly-866f8a ready!
Built without sensitive environment variables
Name | Link |
---|---|
Latest commit | 2d74e1f5338f83c1f0761578c2b55da919145809 |
Latest deploy log | https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/65a923421b86950008d0c18c |
Deploy Preview | https://deploy-preview-3152--guileless-rolypoly-866f8a.netlify.app |
Preview on mobile | Toggle QR Code...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify site configuration.
Thanks Gabriel. I think if we were going to support a function here, it should conform to the ZodErrorMap
interface. Essentially you'd be providing an error map that would be used whenever this is error happens.
export type ZodErrorMap = (
issue: ZodIssueOptionalMessage,
_ctx: ErrorMapCtx
) => { message: string };
This is a pretty major project, since it should be supported everywhere consistently, including in all the schema constructors, not just z.string()
.
Also, the ZodErrorMap
type definition will be changing (slightly) in Zod 4 😅 I think this is a great API and I'm putting this on my list of Zod 4 features, but it's probably better that I implement it. I really appreciate your work here though, and it's a great PR!