TypeScript: validating external data
BRANK

Data validation means ensuring that data has the desired structure and content.With TypeScript, validation becomes relevant when we receive external data such as:Data parsed from JSON filesData received from web servicesIn these cases, we expect the data to fit static types we have, but we can’t be sure. Contrast that with data we create ourselves, where TypeScript continuously checks that everything is correct.This blog post explains how to validate external data in TypeScript.Table of contents:JSON schema  Approaches for data validation in TypeScript  Approaches not using JSON schema  Approaches using JSON schema  Picking a library  Example: validating data via the library Zod  Defining a “schema” via Zod’s builder API  Validating data  Type guards  Deriving a static type from a Zod schema  External vs. internal representation of data  Conclusion  JSON schema  Before we can explore approaches for data validation in TypeScript, we need to take a look at JSON schema because severa…

2ality.com
Related Topics: TypeScript
1 comments