Skip to main content

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

If a schema contains async refinements, calling .parse() or .safeParse() will throw an error. You must use .parseAsync() or .safeParseAsync().

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

  1. Always use async methods - Use .parseAsync() or .safeParseAsync() with async refinements
  2. Optimize async operations - Async refinements run sequentially; minimize API calls
  3. Handle errors gracefully - Use .safeParseAsync() to avoid unhandled promise rejections
  4. Avoid mixing parse types - Don’t call .parse() on schemas with async refinements
  5. Consider performance - Async validation is slower; use sync validation when possible
  6. 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.