Introduction
on this page
What is the problem?
If you have ever fetched an endpoint, then you might have run into this problem. You want to validate the response-body’s structure, and its contents such that they are consistent with your requirements. You could either use a custom solution for that or use a third-party schema validation library like Joi, or Yup. The problem arises when you are using typescript and you also want to type the response-body structure, so you have to define the type separately, and cast the object as that type. You must already see the redundancy, and duplication here. The data validation schema we defined is essentially representing the same thing as the type we defined. It would be nice if we could infer the type from the schema. This is exactly what Zod does.
What is Zod?
Zod is a schema-driven data validation library which also provides static type inference. Taken right from the documentation:
The goal is to eliminate duplicative type declarations. With Zod, you declare a validator once and Zod will automatically infer the static TypeScript type…
What will you learn?
- Data validation
- Automatic type inference
- Type transformations
- Object manipulation methods
- Advanced validation logic
Prerequisites
A basic understanding of typescript is all you need to be able to understand this tutorial.
Reference
This course heavily references the documentation for Zod which can be found here.