Sub topic 3
Describing Objects with `z.object`
Prerequisites and the object model
This chapter assumes basic TypeScript types, interfaces, and type aliases. You should also know JavaScript object and array syntax, JSON, npm packages, and ES module imports.
TypeScript describes the shape of values during compilation. Zod describes and checks the shape of values while the program runs. A Zod object schema is a JavaScript value that lists the expected fields and the schema for each field.
The simplest form is:
import { z } from "zod";
const User = z.object({
id: z.number(),
name: z.string(),
active: z.boolean(),
});User is a schema, not a TypeScript type. Its fields describe the values that a runtime object must contain.
| Field | Schema | Accepted value |
|---|---|---|
| `id` | `z.number()` | Any JavaScript number |
| `name` | `z.string()` | Any string |
| `active` | `z.boolean()` | `true` or `false` |
Adding constraints to fields
Primitive schemas can include checks. These checks run when data is validated.
import { z } from "zod";
const Product = z.object({
sku: z.string().min(3),
title: z.string().min(1),
price: z.number().nonnegative(),
quantity: z.number().int().min(0),
});
const productData = {
sku: "BK-1",
title: "TypeScript Handbook",
price: 29.99,
quantity: 4,
};
const product = Product.parse(productData);
console.log(product.title);Here, sku must contain at least three characters. price cannot be negative. quantity must be a whole number that is zero or greater.
A field is required when its schema does not include optional(). The object must contain that field with a value of the correct type.
Nesting object schemas
Objects can contain other objects. Define a separate schema when a nested shape has meaning or may be reused.
import { z } from "zod";
const Address = z.object({
street: z.string().min(1),
city: z.string().min(1),
postalCode: z.string().min(1),
});
const Customer = z.object({
id: z.number().int().positive(),
name: z.string().min(1),
address: Address,
});
const customer = Customer.parse({
id: 42,
name: "Mina",
address: {
street: "10 Oak Street",
city: "Leeds",
postalCode: "LS1 1AA",
},
});
console.log(customer.address.city);The address field accepts only an object that satisfies Address. Nested schemas make large payloads easier to read and reuse.
How object schemas handle extra fields
A JavaScript object can contain fields that the schema does not list. Zod object behavior depends on the object mode you choose. Use strict() when unexpected fields should cause validation to fail. Use passthrough() when unknown fields should remain in the result.
import { z } from "zod";
const StrictUser = z
.object({
name: z.string(),
})
.strict();
StrictUser.parse({ name: "Ari", role: "admin" });
// Throws because "role" is not part of the schemaChoose strict validation for security-sensitive payloads or fixed contracts. Choose passthrough behavior when another system adds metadata that your application does not need to interpret.
Seeing the validation flow
Once an object schema is defined, later code can parse data with it and inspect validation issues. The same schema can also become the source for a matching TypeScript type.
Exercise
Write a Zod schema for an Order object with a positive integer id, a nonempty customerName, and an array of nonempty item names.
Show answer
const Order = z.object({ id: z.number().int().positive(), customerName: z.string().min(1), items: z.array(z.string().min(1)) });