REST API Validation in a Typescript front-end by Matteo Orlandi and Lorenzo Pieri

Discover how to implement robust REST API validation in your TypeScript frontend using zod, yup, and iots, and learn best practices for defining schemas, handling errors, and ensuring data integrity at both client and server sides.

Key takeaways
  • Validation is essential in API development to ensure data correctness and prevent errors.
  • zod is a TypeScript-first schema builder that generates types and should be used when starting a new project.
  • yup is another schema builder that can be used for validation, but it has limitations and requires extra flags to make fields optional.
  • API designs should follow a strict protocol, like OpenAPI, to ensure consistency and avoid errors.
  • iots is better suited for functional programming style and has support for TypeScript.
  • Validation should be applied at the client-side and server-side to ensure data integrity.
  • Branded types can be used to add extra validation rules.
  • zod and iots provide types at compile-time and validation at runtime, while yup provides validation at runtime only.
  • Validation rules should be defined for each field, including optional and required fields.
  • A schema should be defined for the response shape, including the data type and constraints.
  • Error handling is required to provide meaningful feedback to the end-user in case of validation errors.
  • Validating data at the client-side can prevent unexpected behaviors in the API.