new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Zod · all subjects

parsing

10 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Parsing data with .parse()

Use the .parse() method to validate input against a schema. If validation succeeds, .parse() returns a strongly-typed deep clone of the input. If validation fails, it throws a ZodError instance.

Async parsing with .parseAsync()

When a schema uses asynchronous APIs like async refinements or async transforms, use the .parseAsync() method instead of .parse(). This is an awaitable method that handles async operations.

Safe parsing with .safeParse()

Use .safeParse() to avoid try/catch blocks. It returns a discriminated union result object with either a success case containing result.data (the parsed value) or a failure case containing result.error (the ZodError). Check result.success to determine which case occurred.

Async safe parsing with .safeParseAsync()

When a schema uses asynchronous APIs like async refinements or async transforms, use the .safeParseAsync() method instead of .safeParse(). This is an awaitable method that handles async operations and returns the same discriminated union result object.

Three methods for processing schemas: parse, decode, encode

All Zod schemas can process inputs in both forward and backward directions using three methods: .parse() (Input to Output, unknown input type), .decode() (Input to Output, strongly-typed input), and .encode() (Output to Input, strongly-typed input).

Codec variants: async and safe

Codecs support async transforms and have safe variants: .decode(), .decodeAsync(), .safeDecode(), and .safeDecodeAsync(). The safe variants return { success: true, data } or { success: false, error: ZodError } instead of throwing.

Codec type signature example

stringToDate.decode("2024-01-15T10:30:00.000Z") returns Date; stringToDate.decodeAsync("2024-01-15T10:30:00.000Z") returns Promise<Date>; stringToDate.safeDecode("2024-01-15T10:30:00.000Z") returns { success: true, data: Date } | { success: false, error: ZodError }.

Codec use case: network boundary transformation

Codecs are particularly useful when parsing data at a network boundary. A single Zod schema can be shared between client and server to convert between network-friendly formats (like JSON) and richer JavaScript representations.

Top-level parsing functions in zod/v4/core

zod/v4/core provides top-level parsing functions since core schema classes have no methods: z.parse(schema, value), z.safeParse(schema, value), z.parseAsync(schema, value), and z.safeParseAsync(schema, value).

Parsing methods: parse and safeParse

The parse() method is used for parsing data against a schema. The safeParse() method is the safe variant. Both have async versions: parseAsync() and safeParseAsync().

Give your agent this brain