We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Why Validate?

Zod's only one of many runtime validation libraries. Popular alternatives include:

The Problem

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

The Solution

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.

Do You Need It?

Runtime validation might not be necessary if you control all the data or work on a small team with insight into all changes.