Sub topic 10
Reusing Schemas with `extend`, `pick`, `omit`, and `partial`
Assume you know basic TypeScript types, JavaScript objects, JSON, npm imports, and try/catch. This chapter focuses on composing Zod schemas instead of copying similar field definitions.
A schema often describes more than one related shape. For example, an application might need:
- A complete user returned by an API.
- A user creation payload.
- A partial update payload.
- A public user without private fields.
- An administrator user with extra permissions.
Copying each shape by hand makes them drift apart. Zod provides methods that build a new schema from an existing one.
| Method | Purpose |
|---|---|
| `extend` | Add fields or replace fields |
| `pick` | Keep selected fields |
| `omit` | Remove selected fields |
| `partial` | Make selected or all fields optional |
Start with a base schema
Define the fields shared by several shapes once:
import { z } from "zod";
const User = z.object({
id: z.number().int().positive(),
name: z.string().min(1),
email: z.string().email(),
nickname: z.string().min(1).optional(),
role: z.enum(["user", "admin"]),
});
type User = z.infer<typeof User>;User validates a complete object. Its inferred TypeScript type describes the data produced after successful parsing.
Add fields with `extend`
Use extend when a new object contains all the base fields plus additional fields:
const AdminUser = User.extend({
permissions: z.array(z.string()),
});
type AdminUser = z.infer<typeof AdminUser>;
const admin = AdminUser.parse({
id: 1,
name: "Mina",
email: "[email protected]",
role: "admin",
permissions: ["users:read", "users:write"],
});The original User schema remains unchanged. AdminUser has every User field and also requires permissions.
An extension can replace a field with another schema. Treat this as a deliberate decision because it can weaken or change the original contract:
const ImportedUser = User.extend({
id: z.string(),
});In most cases, use a distinct name when the new meaning is different.
Keep fields with `pick`
pick creates a schema containing only the selected keys. It is useful for request payloads:
const CreateUser = User.pick({
name: true,
email: true,
nickname: true,
});
const newUser = CreateUser.parse({
name: "Ravi",
email: "[email protected]",
});CreateUser does not require id or role. Those values might be generated or assigned by the server.
Remove fields with `omit`
Use omit when a shape contains most of the base fields but must hide selected ones:
const PublicUser = User.omit({
id: true,
role: true,
});
const publicUser = PublicUser.parse({
name: "Ravi",
email: "[email protected]",
});This is useful for response data that should not expose internal identifiers or authorization details. Schema composition does not automatically remove fields from an existing object. You must parse or construct the value through the new schema.
Make fields optional with `partial`
An update request usually allows the client to send only the fields that changed:
const UpdateUser = User.pick({
name: true,
email: true,
nickname: true,
}).partial();
const patch = UpdateUser.parse({
nickname: "R",
});After partial(), every selected field is optional. An empty object also passes this schema, so add a custom rule if an update must contain at least one field.
You can make only selected fields optional with partial and a key map:
const UserWithOptionalNickname = User.partial({
nickname: true,
});Here, nickname becomes optional while id, name, email, and role remain required.
Exercise
Which composition creates a schema for an update that accepts only name and email, with both fields optional?
Show answer
const UpdateUser = User.pick({ name: true, email: true }).partial();