Handling Missing Values in Zod
From the Zod cheat sheet ยท Optional, Nullable, Default & Catch ยท verified Jul 2026
Handling Missing Values
Optional, nullable, default values, and catch fallbacks
typescript
z.string().optional(); // string | undefined
z.string().nullable(); // string | null
z.string().nullish(); // string | null | undefined
z.string().default("hello");
z.string().catch("fallback");๐ก .default() only kicks in when the value is undefined, not null
โก .catch() is like a try/catch โ it swallows ALL errors and returns the fallback
๐ Use .nullish() for fields that may be null OR undefined (common in databases)
๐ข .default() with a function generates a fresh value on every parse โ ideal for IDs
optionalnullabledefault