Async Parsing
Zod supports asynchronous validation for schemas with async refinements or transforms.Basic Async Parsing
Use.parseAsync() or .safeParseAsync() for async validation:
Sync vs Async Parse
Async Refinements
Basic Async Refinement
Refinements can be async functions:Async Refinement with Promises
Value-Based Async Validation
Real-World Example: Database Validation
Async Error Behavior
Sync Parse Throws on Async Refinements
Multiple Async Errors
Async validation collects all errors, just like synchronous validation:Non-Empty String Validation
Async Refinement Execution
Early Termination
Async refinements stop executing after the first failure:Mixed Sync and Async Validation
Promise Schemas
Validate Promise values:Async Transforms
While not shown in the test files, async transforms work similarly to async refinements:All Schema Types Support Async
Async parsing works with all Zod schema types:Best Practices
- Always use async methods - Use
.parseAsync()or.safeParseAsync()with async refinements - Optimize async operations - Async refinements run sequentially; minimize API calls
- Handle errors gracefully - Use
.safeParseAsync()to avoid unhandled promise rejections - Avoid mixing parse types - Don’t call
.parse()on schemas with async refinements - Consider performance - Async validation is slower; use sync validation when possible
- Test both paths - Verify both success and failure cases in async validation
Async refinements are perfect for validating against external data sources like databases, APIs, or file systems.