How to validate types in API Requests and Responses?
Hello, I'm wondering how to validate and handle the types in my app's API. I'll use the example found in the docs as an example:
How to add a type validator for the object
req.json()
that throws an error when it doesn't match the User
interface and continues with the code if it does?
Thank you2 Replies
I've heard folks like to use Zod for that. No personal experience though. https://zod.dev/
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
Thanks for your answer, but my issue with Zod is that one must create a schema different from the already used interfaces like the
User
interface in the example.