Basic Usage
The .catch() modifier provides a fallback value when validation fails for any reason.
Unlike .default() which only handles undefined, .catch() handles all validation failures.
Type Inference
The .catch() modifier does not change the input/output types:
This is because from a type perspective, you’re still working with the base type - the catch is a runtime safety mechanism.
Dynamic Fallbacks
You can provide a function to generate the fallback value and access error context:
Catch Context
The catch function receives a context object with:
ctx.input: The original input value that failed validation
ctx.issues: Array of validation issues that occurred
With Optional Fields
In Object Schemas
When using .catch() in objects, validation errors in fields without catch will still cause the entire parse to fail.
The catch applies after the transform, so invalid inputs that can’t be transformed will use the fallback.
Nested Catches
Chained Catches
When catches are chained, the innermost catch handles the error first.
With Enums
Complex Example
Direction-Aware Behavior
Catches only apply during parsing (forward direction), not during encoding:
Unwrapping
You can remove the catch wrapper:
Catch vs Default
Use Cases
Fault-Tolerant Parsing
API Response Sanitization
Gradual Migration
While .catch() is useful for fault tolerance, overuse can hide data quality issues. Use it thoughtfully and log when fallbacks are triggered.