question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Zod exception should include the input data

See original GitHub issue

Hello.

When I run validator on incorrect data I get ZodError that does not contain the original data, only Zod info about what went wrong and on what path etc. I think it would be beneficial to have the original data also included in the ZodError, since it would make the debugging much easier.

My use case:

I am communicating with API that is not well documented. The APIs responses are big and complicated (union types) and the structure of the responses is not documented at all, so I have to guess little bit. When there is an exception, the Zod error itself does not have enough information for me to fix it. So I had to wrap the Zod library, and in cases when the validation does not pass, I log the original data, along with the ZodError.

So my line of thinking was that adding the original data into the ZodError could make the errors much easier to understand and it would remove the need for wrapping the Zod library for me and probably other people.

Proposal

Add input data into the ZodError

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
colinhackscommented, Aug 17, 2021

@scotttrinh Should be safe to add an inputData property on ZodError as long as it doesn’t get logged by the .message getter. Then users who want to inspect/print the input can do so, but it’s unlikely to get logged accidentally.

0reactions
stale[bot]commented, Mar 2, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Validating user input with Zod - YouTube
In this video we use Zod to validate incoming user data sent to an API route in Next.js.---------- CourseWant to go deeper with...
Read more >
Validating Input with Zod in TypeScript | by Dries Augustyns
Let's create a Zod schema to validate an entire object. For example, we expect an API call from our frontend framework to add...
Read more >
Schema validation in TypeScript with Zod
In this article, you will learn about schema design and validation in Zod and how to run it in a TypeScript codebase at...
Read more >
TypeScript schema validation with Zod - Iskander Samatov
You can use it to ensure the validity of input data, ... The exception will contain an array of ZodError objects with a...
Read more >
ZOD - TypeScript-first schema data validation - fun4code.com
With this one straight line, we check that the incoming input is a string of the correct length. Additionally, we can provide our...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found