Skip to content
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

Return all schema errors #68

Closed
ozimos opened this issue Jun 14, 2020 · 9 comments
Closed

Return all schema errors #68

ozimos opened this issue Jun 14, 2020 · 9 comments

Comments

@ozimos
Copy link

ozimos commented Jun 14, 2020

How do I get Zod to return all the errors. Currently schema.parse throws on the first error. Something like yup's {abortEarly: false} would be nice

@colinhacks
Copy link
Owner

@ozimos Currently Zod returns all the errors it can. For instance if you're validating an object, it will give you all the errors detected and a path to where in the object the error was thrown. Check out the Error Handling section of the docs if you haven't already.

If there's a specific instance where Zod is throwing an error too early, please provide some details and a code sample, and I'll look into it.

@ozimos
Copy link
Author

ozimos commented Jun 14, 2020

@ozimos
Copy link
Author

ozimos commented Jun 14, 2020

@vriad I know that Zod does return all the errors https://codesandbox.io/s/kind-curie-ilf05?file=/src/index.ts
Perhaps something in the my setup of the react-hook-form is suppressing the normal Zod behavior (useCallback ?)

@colinhacks
Copy link
Owner

colinhacks commented Jun 14, 2020

@ozimos Looking into your repo now. I've never used React hook form so I'm having some trouble figuring out what's going on. Would it be possible for you to send me the data being validated and the error(s) being returned?

Seems unlikely that the library could impact Zod's bahavior.

@ozimos
Copy link
Author

ozimos commented Jun 14, 2020

I'm working off these examples in the react-hook-form docs
https://react-hook-form.com/advanced-usage#CustomHookwithValidationResolver
https://react-hook-form.com/get-started#Quickstart

some output from the console

Array(1)
index.js:27 input data 
{firstName: "zod", lastName: "", email: "theba@z", password: "thetetathea", confirmPassword: "thethtbet"}
App.tsx? [sm]:33 
Array(1)
index.js:27 input data 
{firstName: "zod", lastName: "", email: "theba@zo", password: "thetetathea", confirmPassword: "thethtbet"}
App.tsx? [sm]:33 
Array(1)
index.js:27 input data 
{firstName: "zod", lastName: "", email: "theba@zod", password: "thetetathea", confirmPassword: "thethtbet"}
App.tsx? [sm]:33 
Array(1)
index.js:27 input data 
{firstName: "zod", lastName: "", email: "theba@zod.", password: "thetetathea", confirmPassword: "thethtbet"}
App.tsx? [sm]:33 
Array(1)
0: {path: Array(1), message: "Invalid email address."}
length: 1
__proto__: Array(0)
index.js:27 input data 
{firstName: "zod", lastName: "", email: "theba@zod.c", password: "thetetathea", confirmPassword: "thethtbet"}
App.tsx? [sm]:33 
Array(1)
index.js:27 input data 
{firstName: "zod", lastName: "", email: "theba@zod.co", password: "thetetathea", confirmPassword: "thethtbet"}
App.tsx? [sm]:33 
Array(1)
index.js:27 input data 
{firstName: "zod", lastName: "", email: "theba@zod.com", password: "thetetathea", confirmPassword: "thethtbet"}
App.tsx? [sm]:33 
Array(1)
0: {path: Array(0), message: "Both password and confirmation must match"}
length: 1
__proto__: Array(0)
index.js:27 input data 
{firstName: "zod", lastName: "", email: "theba@zod.com", password: "thetetathea", confirmPassword: "thethtbet"}
App.tsx? [sm]:33 
Array(1)
0: {path: Array(0), message: "Both password and confirmation must match"}
length: 1
__proto__: Array(0)

@ozimos
Copy link
Author

ozimos commented Jun 14, 2020

@vriad In the dump above, I am typing into the form inputs and logging to console at lines 24 and 33 of App.tsx. In the console dump above, there is only one error from zod even when both email and confirmPassword fields are wrong. As soon as I fix the email field the sole error message switches from "Invalid email address." to "Both password and confirmation must match"

@colinhacks
Copy link
Owner

colinhacks commented Jun 14, 2020

Gotcha. Just looked into the code and Zod doesn't run the custom refinements on an object schema if there was an error earlier when parsing its values. I'll fix this and get back to you.

@colinhacks
Copy link
Owner

I just published zod@1.8 which has far more consistent parsing behavior.

Specifically, parsing in executed in two phases:

  1. First all types are validated. If there is an inconsistency (e.g. a string is passed to a number validator), a ZodError is thrown. In other words, a type error "short circuits" the parsing flow and an error is thrown immediately.
  2. If the type is correct, then all other checks — unrecognized properties, array/string lengths, number min/maxes, and all custom refinement checks — are run in parallel. All of the errors encountered in this phase are returned in a single object.

@ozimos Give this a shot with zod@1.8+ and let me know if you're still encountering issues.

@brabeji
Copy link
Contributor

brabeji commented Aug 2, 2020

@vriad I've just encountered some missing errors as seen in #107

I'm building zod+mobx typesafe form state library. Previously I had some success with joi + typesafe-joi but some nasty hacks were needed. So I'm really happy to see zod to take off. Thank you! <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants