`z.union()` inside `.pipe()` loses `strictObject`'s `unrecognized_keys`
Author: andreww2012Created Sep 29, 2026Updated Sep 30, 2026
The minimal repro:
import * as z from 'zod';
const schema = z.strictObject({a: z.string()}).pipe(
z.union([
z.object({
a: z.literal('x'),
}),
z.object({a: z.string()}),
]),
);
schema.safeParse({a: 'x', extra: 1}).success;
// 4.4.3: false
// 4.5.0 - 4.6.5: true ❌This is expected to return false.
Affected: z.union() with 2+ options, .or(), z.lazy(() => z.union(...)), and a .transform() before the union.
Not affected: single-option z.union(), z.discriminatedUnion(), z.xor().
Still present on main.
Cause: since #6412, handlePipeResult doesn't stop on unrecognized_keys and passes these issues to the next schema.
When an option matches, $ZodUnion returns that option's own payload, so the passed issues are dropped:
if (result.issues.length === 0) return result;handleUnionResults and the single-option fast path write into the incoming payload instead, so they keep the issues.
Possible fix (packages/zod/src/v4/core/schemas.ts, $ZodUnion), which fixes all the cases above:
- if (result.issues.length === 0) return result;
+ if (result.issues.length === 0) {
+ payload.value = result.value;
+ return payload;
+ }Source: colinhacks/zod