Compose logical AND validation
Combine independent validation constraints that an input must satisfy simultaneously.
Zod · Function
Combines two Zod schemas with logical AND semantics. The input must satisfy both schemas, and the inferred type is their intersection.
z.intersection combines two schemas with logical AND semantics. Parsing succeeds only when the input satisfies both schemas, and the inferred TypeScript type is their intersection.
z.intersection(a, b)| Name | Type | Description |
|---|---|---|
a | ZodType | The first schema to combine. |
b | ZodType | The second schema to combine. |
A ZodIntersection schema representing the logical intersection of the two input schemas.
z.intersection.extend instead of an intersection when possible.pick and omit.import { z } from "zod";
const numeric = z.union([z.number(), z.string()]);
const booleanish = z.union([z.number(), z.boolean()]);
const both = z.intersection(numeric, booleanish);
console.log(both.parse(42)); // 42The value must satisfy both schemas; here, only a number belongs to both unions.
Combine independent validation constraints that an input must satisfy simultaneously.
Require an object to conform to two object schemas at once.
Derive the common accepted values of two union schemas.
It creates a schema representing a logical AND of two schemas. An input must pass both schemas, and the inferred type is the intersection of their types.
Follow-up: What does intersecting two unions demonstrate? The result keeps values accepted by both unions; for numeric, string and numeric, boolean unions, that common set is number.
Use z.intersection(a, b) with two schemas as arguments. The resulting schema can then be used for normal parsing and type inference.
Follow-up: How can the same composition be expressed fluently? For supported schemas, use the .and() method on the first schema.
For combining object schemas, the documentation recommends A.extend(B) instead. It returns a ZodObject, while z.intersection(A, B) returns a ZodIntersection.
Follow-up: Why does the returned class matter? A ZodObject retains object-specific methods, whereas the intersection instance lacks helpers such as pick and omit.
z.unionZodIntersection.and()z.objectz.extend