0Pricing
TypeScript Academy · Lesson

Zod Schema Basics

Define schemas for primitives, objects, and arrays.

Zod Schema Basics is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Runtime Validation?

TypeScript types vanish at runtime. Data from APIs, forms, and files is untyped at the boundary. Zod validates that data at runtime and gives you types too.

import { z } from "zod";
// Types check at compile time; Zod checks values at runtime.

Importing Zod

Bring in the z namespace from the zod package. Every schema builder lives on z.

import { z } from "zod";
const nameSchema = z.string();
// nameSchema validates that a value is a string.

Primitive Schemas

Zod offers builders for primitives: z.string(), z.number(), and z.boolean() each validate one kind of value.

import { z } from "zod";
const name = z.string();
const age = z.number();
const active = z.boolean();
// Use .parse(value) to validate at runtime.

Validating a Value

Call .parse on a schema to validate. It returns the value if valid and throws if not.

import { z } from "zod";
const schema = z.string();
const result = schema.parse("hello"); // "hello"
const broken = schema.parse(123); // throws ZodError

Object Schemas

z.object describes the shape of an object, mapping each key to a schema for its value.

import { z } from "zod";
const userSchema = z.object({
  name: z.string(),
  age: z.number()
});
// userSchema.parse({ name: "Ada", age: 36 }) succeeds.

Array Schemas

z.array validates that a value is an array whose elements all match a given schema.

import { z } from "zod";
const tags = z.array(z.string());
// tags.parse(["a", "b"]) succeeds; tags.parse([1]) throws.

Nesting Schemas

Schemas compose. An object can contain arrays and other objects, mirroring how your real data is shaped.

import { z } from "zod";
const post = z.object({
  title: z.string(),
  tags: z.array(z.string()),
  author: z.object({ name: z.string() })
});
// Validates nested structure in one call.

Boolean and Number Together

Combine multiple field types in one object schema to model realistic records.

import { z } from "zod";
const product = z.object({
  name: z.string(),
  price: z.number(),
  inStock: z.boolean()
});
// product.parse({ name: "Pen", price: 2, inStock: true });

Schemas Are Values

A Zod schema is an ordinary JavaScript value you can store, pass around, and reuse, unlike a TypeScript type which exists only at compile time.

import { z } from "zod";
const emailSchema = z.string();
function check(s: typeof emailSchema, v: unknown) {
  return s.parse(v);
}
// You can pass schemas as arguments.

Validating Unknown Input

Zod is ideal for unknown data, like a parsed JSON payload, because it both checks and narrows the value safely.

import { z } from "zod";
const schema = z.object({ id: z.number() });
const raw: unknown = JSON.parse("{ \"id\": 1 }");
const data = schema.parse(raw); // data.id is number

Building Blocks for the Course

With z.object, z.string, z.number, z.array, and z.boolean, you can describe most data. Next we will infer TypeScript types from these schemas.

import { z } from "zod";
const schema = z.object({
  name: z.string(),
  scores: z.array(z.number())
});
// One schema, ready to validate and to infer a type from.

Quick Check: Zod Basics

Test your understanding of Zod schema basics.

Recap: Zod Schema Basics

You learned to import z and build schemas with z.object, z.string, z.number, z.array, and z.boolean, then validate values at runtime with .parse.

import { z } from "zod";
const userSchema = z.object({ name: z.string(), age: z.number() });
// userSchema.parse({ name: "Ada", age: 36 });

Frequently asked questions

Is the “Zod Schema Basics” lesson free?

Yes — the full text of “Zod Schema Basics” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Zod Schema Basics”?

Define schemas for primitives, objects, and arrays. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Zod Schema Basics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Zod Schema Basics
  2. Inferring Types from Schemas
  3. parse vs safeParse
  4. Composing and Refining Schemas
← Back to TypeScript Academy