zod icon indicating copy to clipboard operation
zod copied to clipboard

Error message based on input value

Open joaoGabriel55 opened this issue 5 months ago • 2 comments

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) })

joaoGabriel55 avatar Jan 17 '24 14:01 joaoGabriel55

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

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Jan 17 '24 14:01 netlify[bot]

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!

colinhacks avatar Feb 15 '24 01:02 colinhacks