

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: The Runtime Problem
incomplete
2: Validation Libraries
incomplete
3: Schemas and Type Inference
incomplete
4: Why Validate?
incomplete
This lesson's interactive features are locked, please to keep using them
Zod's only one of many runtime validation libraries. Popular alternatives include:
TypeScript provides compile-time type safety, but can't validate external data at runtime. Data from APIs, user input, or databases arrives as any and TypeScript can't guarantee its shape.
External APIs are particularly problematic - you don't control them, and documentation is often outdated.
const userData = await response.json(); // 'any'
console.log(userData.profile.name.toUpperCase()); // Crashes if profile is null
Runtime validation creates a boundary between untrusted external data and your typed code:
const userData = UserSchema.parse(await response.json());
console.log(userData.profile.name.toUpperCase()); // Guaranteed to work
When validation fails, you can handle it gracefully instead of crashing.
Runtime validation might not be necessary if you control all the data or work on a small team with insight into all changes.