Schema
Schema validation with static type inference
Introduction
Zod is a TypeScript-first validation library.
In web applications, it is common to work with complex data that must be transmitted between different applications.
Using Zod, you can define schemas you can use to validate data, from a simple string to a complex nested object.
Json
When deserializing a JSON string, you can define its structure using a type to provide better autocompletion, error checking, and readability.
Modify the main.ts file:
name: string,
age: number
}
person.nameIf you run this code, it works without problems because the deserialized object has the name property:
EvaHowever, this is only informative, as JSON.parse does not verify that the string contains an object compatible with the variable type:
name: string,
age: number
}
person.nameIf you run the code, the result is undefined because the deserialized object does not have the name property:
You're reading a preview.
Sign in to read the full article. Any account opens 10 free articles a month; students and teachers read their course pages without limit.
Sign in