Sub topic 2
Your First Schemas: Strings, Numbers, and Booleans
This chapter assumes you know basic TypeScript types, JavaScript values, JSON, npm packages, and try/catch. You will use Zod to check values at runtime, when your program is running.
Why runtime checks are needed
TypeScript checks your code before it runs. It does not check data that arrives later from an API, form, file, or environment variable.
type Config = {
port: number;
};
const config = JSON.parse('{"port":"3000"}') as Config;
console.log(config.port + 1); // "30001", not 3001The as Config expression changes what TypeScript believes. It does not convert or validate the value. The actual port value is still a string.
A Zod schema is an executable description of allowed data. It checks a value and reports whether the value matches the description.
flowchart TD
A[Unknown runtime value] --> B[Zod schema]
B -->|matches| C[Validated value]
B -->|does not match| D[Validation error]String schemas
Create a string schema with z.string().
import { z } from "zod";
const Username = z.string();
console.log(Username.parse("mina")); // "mina"
try {
Username.parse(42);
} catch (error) {
console.error("The value is not a string");
}parse returns the validated value when the check succeeds. When it fails, parse throws a ZodError.
Schemas can include common checks:
const UserName = z
.string()
.trim()
.min(3, "Use at least 3 characters")
.max(20, "Use at most 20 characters");
const Email = z.string().email();
const Version = z.string().regex(/^v\d+$/, "Expected a version such as v2");
console.log(UserName.parse(" samira ")); // "samira"
console.log(Email.parse("[email protected]"));
console.log(Version.parse("v2"));The checks run in the order that they appear. trim() removes surrounding whitespace before later checks run.
Number and boolean schemas
Use z.number() for JavaScript numbers. It rejects strings that look like numbers.
const Port = z.number().int().min(1).max(65535);
const Enabled = z.boolean();
console.log(Port.parse(8080)); // 8080
console.log(Enabled.parse(false)); // false
Port.parse("8080"); // throws: this is a stringint() requires an integer. min() and max() constrain the allowed range. A boolean schema accepts only true or false, not values such as "true", 1, or 0.
Choosing checks for a boundary
Use the smallest schema that expresses the real contract.
| Value | Schema | Example |
|---|---|---|
| Nonempty label | `z.string().min(1)` | `"draft"` |
| Port number | `z.number().int().min(1).max(65535)` | `3000` |
| Feature flag | `z.boolean()` | `true` |
For untrusted input, validate before using the value:
import { z } from "zod";
const Settings = {
name: z.string().trim().min(1),
port: z.number().int().min(1).max(65535),
debug: z.boolean(),
};
const settings = {
name: Settings.name.parse(" api "),
port: Settings.port.parse(3000),
debug: Settings.debug.parse(false),
};
console.log(settings);Later, you will combine primitive schemas into an object schema. For now, each schema gives one value a precise runtime contract.
Exercise
Why does z.number().parse("3000") fail even though the string contains a number?
Show answer
Zod checks the runtime type. The value is a string, and z.number() accepts only a JavaScript number.