-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix issue #1611 #1620
Fix issue #1611 #1620
Conversation
✅ Deploy Preview for guileless-rolypoly-866f8a ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
IMO this seems very sensible - especially since there's no API changes and seems in line with the current way of organizing error messages. Just my $0.02 USD. Thanks for the pr @john-schmitz ! |
Glad to help. The impact Zod had on my workflow is immense. |
Hmm... I'm not sure if I like this solution. (but correct me if I'm wrong) You are setting the default message directly on the method, promoting it to the highest priority level when generating the final message. Say, for example, I've decided to define my error map on the const myArraySchema = z.array(z.string()).length(2).parse(["foo"], {
errorMap: (issue) => {
if (issue.code === "too_small") {
return { message: "Hey yo, the array must have a length of 2." };
}
return { message: "Nope" };
},
}); Since I'm not setting the Default messages should always have the lowest priority. Can you please add a test of the above code and see the result? |
As @santosmarco-caribou says, the original approach does break Zod's error message hierarchy. I pushed a commit that uses a different approach, adding an optional |
- recursive type definition - exact flag for too_big and too_small: colinhacks/zod#1620 - options property of ZodDiscriminatedUnion was renamed to optionsMap: colinhacks/zod#1290 (comment)
* Bump zod from 3.19.1 to 3.20.0 * Bump @api3/ois from 1.3.0 to 1.4.0 * fix: zod 3.20.0 issues - recursive type definition - exact flag for too_big and too_small: colinhacks/zod#1620 - options property of ZodDiscriminatedUnion was renamed to optionsMap: colinhacks/zod#1290 (comment)
closes #1611
I quickly drafted a working solution so we could iterate and discuss its design.
I also added the same proposed behavior to strings as well, since both strings and arrays share the
.length
function.Looking further into the codebase, I think it would be ideal to create a new check (something like "exact_legth") and a ZodIssueCode so we can share the same locale behavior between Arrays and Strings.
Whats your take? How should I integrate those changes into Zod's current codebase?