causaly / zod-validation-error

Wrap zod validation errors in user-friendly readable messages
MIT License
813 stars 11 forks source link

[Feature request] Include info about which validator failed in the error message #307

Open callumgare opened 6 months ago

callumgare commented 6 months ago

Currently zod-validation-error does a great job of constructing a human readable error message to tell you that a validation error has occurred and where abouts it has occurred (ie which property/properties are the culprit), but for invalid data errors it doesn't give you much details about why it's invalid.

For example just now I was running into this error message: Validation error: Invalid at "request.source"; Invalid at "request.queryType". That doesn't really say anything about what is invalid about those properties. I had to modify the console.log statement to print the whole ValidationError object in order to find out that the error was due to a regex validation failure:

image

It would be great if zod-validation-error could include some info on what validator failed when applicable. And ideally it would be amazing if in a situation like this it could even include the validation regex itself in the error message along side the input data. I'm guessing that info isn't readily available in the error object that zod spits out but maybe the user could optionally supply the schema and input data to fromZodError which could be used to provide more info in the error message if supplied.

If that was the case then the above error message might look something like: Validation error: Invalid at "request.source" (input "foo" failed regex validator /\d+/); Invalid at "request.queryType" (input "bar" failed IP address validator); Which would very helpful for pinpointing the problem right away.

Thanks for your consideration and thanks for such a handy little library :)

jmike commented 6 months ago

Thank you for your feedback - it's always great to see the community pushing for new features. Please find my comments inline below.

It would be great if zod-validation-error could include some info on what validator failed when applicable.

Yes, this is a great suggestion. Doesn't seem too difficult either.

And ideally it would be amazing if in a situation like this it could even include the validation regex itself in the error message along side the input data.

Hm, this is tricky for various reasons.

  1. You typically don't want to expose the regex to your users. This is an implementation detail that will only confuse the final user. It will make the error message non user-friendly, thus going against the philosophy of zod-validation-error.
  2. As you mentioned in your comment above, this particular info isn't readily available as part of the ZodError object (disclaimer: will need to verify that). Therefore it requires a significant change to our API to be able to accept the original zod schema.

tl;dr I don't think this is a good idea atm

callumgare commented 6 months ago

Makes sense. Just the first part of info on what validator failed when applicable would still be very helpful I think.

I'd envision the second part as being used not so much for presenting to the end user but more for situations like writing to a log that would be read by the dev (devs deserve friendly error messages too 😆). You'd opt-in to including that more implementation related info in a particular error message by deliberately supplying the schema and/or passed object in the options arg of fromZodError. And that's something you'd only deliberately choose to do in those situations where you're constructing an error message that's designed more for being read by the dev than the enduser (like the backend logs of a web app for example).

But very understandable if this lib is explicitly only aiming at creating error messages for end users and human readable zod error messages for devs are out of scope.