### Setup Zod repository for local benchmarks Source: https://zod.dev/v4?id=an-extensible-foundation-zodv4core Commands to clone the Zod repository, switch to the v4 branch, and install dependencies for benchmarking. ```bash $ git clone git@github.com:colinhacks/zod.git $ cd zod $ git switch v4 $ pnpm install ``` -------------------------------- ### Zod Mini Bundle Size Example Source: https://zod.dev/v4?id=65x-faster-object-parsing Minimal example demonstrating Zod Mini's tree-shakable API, resulting in 1.88kb gzipped bundle (85% reduction vs Zod 3). ```typescript import * as z from "zod/mini"; const schema = z.boolean(); schema.parse(false); ``` -------------------------------- ### Set Up Zod 4 Benchmarks Repository Source: https://zod.dev/v4?id=65x-faster-object-parsing Clone the Zod repository, switch to the v4 branch, and install dependencies to run performance benchmarks locally. ```bash $ git clone git@github.com:colinhacks/zod.git $ cd zod $ git switch v4 $ pnpm install ``` -------------------------------- ### Setup and Run Zod 4 Benchmarks Source: https://zod.dev/v4?id=custom-email-regex Commands to clone the Zod repository, switch to the v4 branch, and execute specific benchmarks. ```bash $ git clone git@github.com:colinhacks/zod.git $ cd zod $ git switch v4 $ pnpm install ``` ```bash $ pnpm bench ``` -------------------------------- ### Zod Mini Bundle Size Example Source: https://zod.dev/v4?id=an-extensible-foundation-zodv4core Demonstrates the significant bundle size reduction when using Zod Mini. This example produces a 1.88kb gzipped bundle, an 85% reduction compared to Zod 3. ```typescript import * as z from "zod/mini"; const schema = z.boolean(); schema.parse(false); ``` -------------------------------- ### Install Zod 4 Source: https://zod.dev/v4/changelog?id=deprecates-message-parameter Install the stable Zod 4 version using npm. ```bash npm install zod@^4.0.0 ``` -------------------------------- ### Clone and set up Zod repository for benchmarks Source: https://zod.dev/v4 Commands to clone the Zod repository, switch to the v4 branch, and install dependencies using pnpm, preparing for benchmark execution. ```bash $ git clone git@github.com:colinhacks/zod.git $ cd zod $ git switch v4 $ pnpm install ``` -------------------------------- ### Basic Zod Mini Schema Usage Source: https://zod.dev/v4 A simple example demonstrating schema definition and parsing using Zod Mini, highlighting its smaller bundle size. ```typescript import * as z from "zod/mini"; const schema = z.boolean(); schema.parse(false); ``` -------------------------------- ### Example of Pretty-Printed Zod Error Output Source: https://zod.dev/v4?id=benchmarks Illustrates the multi-line string output generated by `z.prettifyError()` for the example `ZodError` provided. ```text ✖ Unrecognized key: "extraField" ✖ Invalid input: expected string, received number → at username ✖ Invalid input: expected number, received string → at favoriteNumbers[1] ``` -------------------------------- ### Zod v4 `tsc` Instantiation Reduction Example Source: https://zod.dev/v4?id=65x-faster-object-parsing This example demonstrates a simple Zod schema definition using `z.object` and `extend`, which shows significant `tsc` instantiation reduction in Zod v4 compared to v3. ```typescript import * as z from "zod"; export const A = z.object({ a: z.string(), b: z.string(), c: z.string(), d: z.string(), e: z.string() }); export const B = A.extend({ f: z.string(), g: z.string(), h: z.string() }); ``` -------------------------------- ### Equivalent .meta() for .describe() Source: https://zod.dev/v4?id=stringbool Shows how to achieve the same result as `.describe()` using the preferred `.meta()` method for adding a description. ```typescript z.string().meta({ description: "An email address" }); ``` -------------------------------- ### Example of Pretty-Printed ZodError Output Source: https://zod.dev/v4 Shows the multi-line string output generated by z.prettifyError for a ZodError instance, providing a user-friendly format. ```text ✖ Unrecognized key: "extraField" ✖ Invalid input: expected string, received number → at username ✖ Invalid input: expected number, received string → at favoriteNumbers[1] ``` -------------------------------- ### Describe Schema with .describe() Method Source: https://zod.dev/v4?id=an-extensible-foundation-zodv4core Use `.describe()` for backward compatibility with Zod 3, though `.meta()` is preferred. Both methods add metadata to the global registry. ```typescript z.string().describe("An email address"); // equivalent to z.string().meta({ description: "An email address" }); ``` -------------------------------- ### Migrate from .describe() to .meta() Source: https://zod.dev/v4?id=100x-reduction-in-tsc-instantiations `.describe()` remains available for Zod 3 compatibility but `.meta()` is the preferred approach for adding descriptions and other metadata. ```typescript z.string().describe("An email address"); // equivalent to z.string().meta({ description: "An email address" }); ``` -------------------------------- ### Create ZodError for Pretty Printing Source: https://zod.dev/v4?id=65x-faster-object-parsing Provides an example of manually constructing a `ZodError` instance containing multiple validation issues, suitable for processing by `z.prettifyError`. ```typescript const myError = new z.ZodError([ { code: 'unrecognized_keys', keys: [ 'extraField' ], path: [], message: 'Unrecognized key: "extraField"' }, { expected: 'string', code: 'invalid_type', path: [ 'username' ], message: 'Invalid input: expected string, received number' }, { origin: 'number', code: 'too_small', minimum: 0, inclusive: true, path: [ 'favoriteNumbers', 1 ], message: 'Too small: expected number to be >=0' } ]); z.prettifyError(myError); ``` -------------------------------- ### Add and Retrieve Schemas from Registry Source: https://zod.dev/v4?id=2x-reduction-in-core-bundle-size Use myRegistry.add() to associate a schema with metadata, and myRegistry.get() to retrieve it. Alternatively, use the .register() method on the schema itself. ```javascript const emailSchema = z.string().email(); myRegistry.add(emailSchema, { title: "Email address", description: "..." }); myRegistry.get(emailSchema); // => { title: "Email address", ... } ``` ```javascript emailSchema.register(myRegistry, { title: "Email address", description: "..." }) // => returns emailSchema ``` -------------------------------- ### Equivalent `.meta()` for `.describe()` Source: https://zod.dev/v4 Shows the `.meta()` equivalent for setting a schema description, which is the preferred approach in Zod 4. ```typescript z.string().meta({ description: "An email address" }); ```