Skip to main content

Overview

Parsing is the process of validating data against a schema. Zod provides multiple methods for parsing, each suited to different use cases:
  • parse() - Throws on validation failure
  • safeParse() - Returns result object
  • parseAsync() - Async version that throws
  • safeParseAsync() - Async version that returns result

parse()

The parse() method validates data and returns it if valid, otherwise throws a ZodError:
From packages/zod/src/v4/classic/schemas.ts:195, parse() is defined as:

When to Use parse()

safeParse()

The safeParse() method never throws. Instead, it returns a result object:

Result Type

From packages/zod/src/v4/classic/parse.ts:4-6:

When to Use safeParse()

parseAsync()

Use parseAsync() when schemas contain asynchronous validations (like .refine() with async functions):
Calling parse() on a schema with async refinements will throw an error. Always use parseAsync() for async validation.

Async Transformations

safeParseAsync()

Combines the safety of safeParse() with async support:

Real-World Example

Parse Context

All parse methods accept an optional context parameter for customization:
From packages/zod/src/v4/core/schemas.ts:16-25, the ParseContext interface:

Error Handling Patterns

Pattern 1: Try-Catch with parse()

Pattern 2: Conditional with safeParse()

Pattern 3: Early Return

Practical Examples

Form Validation

Environment Variables

API Request Validation

Performance Considerations

Zod uses JIT (Just-In-Time) compilation for performance. The first parse is slower as it compiles the validator, but subsequent parses are much faster.
Disable JIT for debugging:

Best Practices

  1. Use safeParse() for user input - Never trust external data
  2. Use parse() for internal assertions - When data should always be valid
  3. Use async methods only when needed - Synchronous parsing is faster
  4. Handle errors gracefully - Provide clear error messages to users
  5. Validate early - Check data at system boundaries

Next Steps