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

Validation Libraries

Manual validation is quite error-prone. Fortunately, many agree and thus someone wrote the Zod library:

What Is Zod?

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.

Creating Basic Schemas

Zod provides validators for all JavaScript primitives:

import { z } from "zod";

const stringSchema = z.string();
const numberSchema = z.number();
const booleanSchema = z.boolean();

Object Schemas

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(),
});

Schema Refinements

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
});

Assignment

Complete the Zod schema for validating issue data from the Jello API. The tests will use your schema to validate different data inputs.