What Is a Default Value in Zod?
A default value in Zod is a fallback that replaces undefined
inputs during validation. Instead of throwing an error or requiring the user to provide every field, Zod fills in missing values automatically. This makes schemas cleaner, safer, and perfect for real-world use cases like user settings, forms, and APIs—where fields like language, theme, or notification preferences often need sensible defaults.
When to Use Zod Default Instead of Optional or Catch
Use default
in Zod when you expect a value to be missing and want to auto-fill it during parsing. Use optional
when missing values are allowed and handled manually later. Use catch
when values might be invalid, and you need a safe fallback. Choose .default()
when your goal is clean, defined output with zero undefined
surprises.
Zod Default Value by Type (String, Number, Boolean, etc.)
Zod lets you assign default values to all common types. For strings, use defaults like "guest"
or "en"
for user roles or language. For numbers, fallback values like 0
, 1
, or 100
work well for counts, IDs, or limits. With booleans, defaults like false
are perfect for toggles. You can also use default values with arrays, like []
for tags, or objects with nested defaults. Even enums, dates, and nullable types support .default()
—making Zod flexible for real-world schemas across forms, APIs, and configs.
Zod Default with Optional and Nullable Fields
Combining default() with optional() or nullable() in Zod gives you fine-grained control over missing and empty values. When used with optional(), .default() fills in only when the field is undefined. When used with nullable(), it allows null but still assigns a default if no value is provided.
For example, you can accept a nullable field like bio, and if nothing is sent, default it to null. Or, with an optional string like language, default it to “en” if missing.
This combo is especially useful in forms, user settings, and API schemas where input flexibility and fallback values must coexist.
Zod Default vs Catch: What’s the Right Choice?
Use default() in Zod when a value might be missing (i.e., undefined) and you want to auto-fill it. Use catch() when a value might be invalid, and you need a safe fallback without throwing an error.
For example, if a user skips a field, .default() fills it. But if they enter something invalid—like a string instead of a number—.catch() steps in with a replacement value.
In short:
- default() handles missing input
- catch() handles invalid input
Choose based on whether you’re preparing for absence or correction.