

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
Manual validation is quite error-prone. Fortunately, many agree and thus someone wrote the Zod library:
Zod provides a declarative way to describe data structures and get compile-time types and validate them at runtime. Instead of writing lengthy manual validation functions, you create schemas that describe what valid data looks like.
Zod provides validators for all JavaScript primitives:
import { z } from "zod";
const stringSchema = z.string();
const numberSchema = z.number();
const booleanSchema = z.boolean();
For validating objects, create schemas with z.object():
import { z } from "zod";
const UserSchema = z.object({
id: z.number(),
name: z.string(),
email: z.string(),
});
You can add constraints to make validation more specific:
const UserSchema = z.object({
id: z.number().positive(), // must be positive
name: z.string().min(1), // must be non-empty
email: z.email(), // must be valid email string
});
Complete the Zod schema for validating issue data from the Jello API. The tests will use your schema to validate different data inputs.