You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Zod is a fantastic validation library, but many developers, myself included, face performance challenges when validating data with multiple rules, especially for expensive operations like database queries or HTTP calls. Currently, Zod validates all rules and surfaces as many errors as possible, which is excellent for debugging but not ideal for all use cases.
Problem:
In some scenarios, such as form validation or validating HTTP payloads, it’s more efficient to stop validation as soon as the first error is encountered. For example:
Validating an email and password where only the first error per field is needed for the user.
Avoiding unnecessary, expensive operations (e.g., server requests) after detecting an initial failure.
Proposed Solution:
Introduce an optional abortEarly mode to Zod, similar to how Joi or Yup handle this. When enabled, Zod should stop validation at the first error and return a single issue. This could be implemented as an option in methods like .parse(), .safeParse(), and their async counterparts.
Many developers have expressed a need for this feature, and alternative solutions like superRefine often result in less readable and more verbose code.
For example:
Form validation where only the first error message for each field is displayed.
Avoiding redundant checks when basic rules like required fail.
Please reconsider adding this feature to improve developer experience and efficiency.
Thank you for considering this request!
The text was updated successfully, but these errors were encountered:
This feature would be really good to have.
Sometimes it's useful to insert some database operations in refine or superrefine because it creates a single source of errors which can be more easily handled and formatted along the way.
But when there is both input validation and database logic on the same zod validation chain, it would make a lot of sense to make zod stop at the first part (if inputs are not valid) before querying the database needlessly.
However at the moment this is only doable if the inputs are validated with superRefine one by one and adding the fatal flag and early return to each one of them, which obviously defeats the purpose of it.
Ideally, there would be two methods to stop validation early:
A single "abortEarly" flag, as suggested by OP and others.
A method like .checkPoint() where you can divide the validation chain in several "checkpoints" where you can handle the errors based on which portion of the validation chain they occurred.
Zod is a fantastic validation library, but many developers, myself included, face performance challenges when validating data with multiple rules, especially for expensive operations like database queries or HTTP calls. Currently, Zod validates all rules and surfaces as many errors as possible, which is excellent for debugging but not ideal for all use cases.
Problem:
Proposed Solution:
Introduce an optional
abortEarly
mode to Zod, similar to how Joi or Yup handle this. When enabled, Zod should stop validation at the first error and return a single issue. This could be implemented as an option in methods like.parse()
,.safeParse()
, and their async counterparts.Example:
Benefits:
Additional Notes:
This issue has been discussed extensively in Issue #1403, Issue #1606, and abort on signal Issue #1915.
Many developers have expressed a need for this feature, and alternative solutions like
superRefine
often result in less readable and more verbose code.For example:
required
fail.Please reconsider adding this feature to improve developer experience and efficiency.
Thank you for considering this request!
The text was updated successfully, but these errors were encountered: