Parse only throws error for single field, if outer refine is present
See original GitHub issueConsider Following Code:
const User = z
.object({
email: z
.string()
.min(1, { message: 'Email is required.' })
.email({ message: 'Email is invalid.' }),
password: z.string().min(8, { message: 'Password must have minimum 8 characters.' }),
confirmPassword: z.string().min(1, { message: 'Confirm password required.' }),
})
.refine((data) => data.password === data.confirmPassword, {
message: "Passwords don't match.",
path: ['confirmPassword'], // path of error
});
const parseResult = User2.parse({
email: 'sds@',
password: 'Ak111@202902',
confirmPassword: 'Ak111@kj32l',
});
console.log({ parseResult });
Above code throws following error:
[
{
"validation": "email",
"code": "invalid_string",
"message": "Email is invalid.",
"path": [
"email"
]
}
]
Expected Output:
It should also return error for Passwords don't match.
Because of this issue all errors are not shown at once in react-hook-form
.
Versions Zod: 3.1.0
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Validation rule Error message not displaying in LWC ...
I have a record which I'm updating via the UpdateRecord method from LWC, using Toast events to display success or error messages, which...
Read more >Check whether a string is parsable into Long without try- ...
Exception should be thrown and handled properly. If you look inside parseLong code, you'll see that there are many different verifications and ...
Read more >Control flow and error handling - JavaScript - MDN Web Docs
The try...catch statement marks a block of statements to try, and specifies one or more responses should an exception be thrown. If an...
Read more >Database Engine events and errors - SQL Server
For a full list of all errors, query the sys.messages catalog view with ... to target a sparse column set instead of single...
Read more >Failed to parse field of type in document with id ''.
In a nutshell, this error just means that you provided a value which could not be parsed by Elasticsearch for the field's type...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, actually. Zod 3.10 just landed and now surfaces more errors simultaneously. Basically refinement issues (and a few other issue types) are considered “continuable”, in that parsing continues even after they occur. Previously Zod would “short circuit” as soon as any issue occurred.
The OPs example now returns two errors:
Keep in mind, this approach has limits. If base object type is missing a required field, for instance, that issue is considered “fatal”/non-continuable, so the outer refine won’t execute. (Though setting default values on the keys would solve this.)
Hello, it seems issue is now fixed (even with acceptTerms boolean & refine) in
3.10.3
, earlier I was checking with3.9.8
, Thanks.