zod icon indicating copy to clipboard operation
zod copied to clipboard

[question] is it possible to stop parsing on the first error?

Open gerardolima opened this issue 1 year ago • 27 comments

I'm using Zod to validate HTTP payloads, some of which may be expensive to validate. Is it possible for schema.parse() and their friends to stop on the first error and return a single ZodIssue?

gerardolima avatar Sep 11 '22 14:09 gerardolima

Zod is designed to surface as many errors as possible, so not really. Some sort of abortEarly mode is certainly possible but I'm not convinced that it's worth the increase in complexity - it's not a very common use case.

colinhacks avatar Sep 12 '22 07:09 colinhacks

hey, @colinhacks, first of all, thank you for the great work 💪🏽! I understand that's not the intended original use case for Zod. I'm moving from Joi -- which I like, but lacks the type inference Zod does so well -- and my idea was to migrate all schemas. My problem is that I've been also using Joi to validate HTTP payloads on a Hapi service and some of those validations can be expensive.

Maybe my case is too specific, and it wouldn't worth the changes, but maybe HTTP payload validation could be a new use case that Zod might also address :) Otherwise, and obviously, please, close the issue -- I don't want to bother you with things out of the scope of the project.

gerardolima avatar Sep 12 '22 08:09 gerardolima

+1

Bailing out on very first error, should be as simple as throwing an exception the moment you stumble on one. No?

jayarjo avatar Oct 30 '22 16:10 jayarjo