Sub topic 7
Optional, Nullable, and Default Fields
Assume you know basic TypeScript types, JavaScript objects and JSON, npm imports, and try/catch. This chapter focuses on how Zod represents missing values and null.
A field can be missing, present with the value null, or present with an ordinary value. These cases are different:
- Optional means the field may be missing or
undefined. - Nullable means the field must exist, but its value may be
null. - Nullish means the field may be missing,
undefined, ornull. - Defaulted means Zod supplies a value when the input is
undefined.
Optional fields
Use .optional() when a property is not required.
import { z } from "zod";
const Profile = z.object({
username: z.string(),
nickname: z.string().optional(),
});
console.log(Profile.parse({ username: "maya" }));
console.log(Profile.parse({ username: "maya", nickname: "May" }));The inferred type allows the property to be absent:
type Profile = z.infer<typeof Profile>;
// {
// username: string;
// nickname?: string | undefined;
// }An optional field does not automatically accept null:
import { z } from "zod";
const Profile = z.object({
nickname: z.string().optional(),
});
Profile.parse({}); // Valid
Profile.parse({ nickname: undefined }); // Valid
Profile.parse({ nickname: null }); // Throws a ZodErrorNullable fields
Use .nullable() when null is a meaningful value. The property itself is still required.
import { z } from "zod";
const Account = z.object({
displayName: z.string().nullable(),
});
Account.parse({ displayName: "Maya" }); // Valid
Account.parse({ displayName: null }); // Valid
Account.parse({}); // Throws a ZodErrorThis produces a required property whose value has two possibilities:
type Account = z.infer<typeof Account>;
// { displayName: string | null }Use nullable values when the data source deliberately sends null, such as a database column or an API response. Do not use null and omission interchangeably unless the API contract says they mean the same thing.
Nullish fields
Use .nullish() when both omission and null are valid.
import { z } from "zod";
const Preferences = z.object({
colorScheme: z.enum(["light", "dark"]).nullish(),
});
console.log(Preferences.parse({}));
console.log(Preferences.parse({ colorScheme: null }));
console.log(Preferences.parse({ colorScheme: "dark" }));It is equivalent in meaning to combining .optional() and .nullable():
import { z } from "zod";
const A = z.string().nullish();
const B = z.string().nullable().optional();
console.log(A.parse(null));
console.log(B.parse(undefined));Default values
Use .default(value) when a missing or undefined value should become a concrete value.
import { z } from "zod";
const Settings = z.object({
language: z.string().default("en"),
notifications: z.boolean().default(true),
});
console.log(Settings.parse({}));
// { language: "en", notifications: true }
console.log(Settings.parse({ language: "fr" }));
// { language: "fr", notifications: true }A default applies to undefined, not null:
import { z } from "zod";
const Theme = z.object({
name: z.string().default("light"),
});
Theme.parse({}); // { name: "light" }
Theme.parse({ name: undefined }); // { name: "light" }
Theme.parse({ name: null }); // Throws a ZodErrorA defaulted field is optional in the input but required in the parsed output. This makes defaults useful for configuration: callers may omit a setting, while the rest of the program receives a complete value.
Choosing the right schema
| Requirement | Schema | Missing key | `null` |
|---|---|---|---|
| Value may be omitted | `z.string().optional()` | Valid | Invalid |
| Value may be null | `z.string().nullable()` | Invalid | Valid |
| Both are valid | `z.string().nullish()` | Valid | Valid |
| Missing value is replaced | `z.string().default("x")` | Valid, becomes `"x"` | Invalid |
Exercise
Write a schema for an object with a required email string, an optional phone string, and a nullable avatarUrl string.
Show answer
z.object({ email: z.string(), phone: z.string().optional(), avatarUrl: z.string().nullable() })