Repository navigation
revert(core): drop the exactOptional parse prototype from #6432 - #6438
Conversation
The hunk is labelled OPTION B PROTOTYPE and changes what an exact optional accepts. Every inner that accepts undefined on its own -- coerce, any, unknown, undefined, literal(undefined), catch -- was rejected, and specific type errors were replaced with nonoptional. z.exactOptional(z.undefined()) could never match a present key. The propValues half of #6432 and its tests are untouched.
There was a problem hiding this comment.
β No new issues found.
Reviewed changes β a single-hunk revert in packages/zod/src/v4/core/schemas.ts, checked against the #6432 commit it reverts, the surrounding object/tuple/compile parse paths, and the open follow-up #6434.
- Drops the
OPTION B PROTOTYPEhunk from$ZodExactOptionalβ the parse override goes back to a baredef.innerType._zod.run(payload, ctx)delegation. This is a byte-for-byte restoration of the pre-#6432 state, including the original// Override parse to just delegate (no undefined handling)comment; diffing againsta38ab4a8confirms nothing else from that commit is touched. - The override itself correctly stays β
$ZodOptional.initinstalls a parse that short-circuits onundefined, so an exact optional has to replace it to reach the inner at all. - The rest of #6432 survives β the
field.optin !== undefinedaddition topropValuesin$ZodObject, its discriminated-union test, and the compile-side duplicate-discriminator guard are all untouched, and thean omittable discriminator claims undefinedtest still passes.
I ran both revisions to confirm the blast radius. Acceptance changes only for inners that themselves accept undefined, and the error for the common case gets more specific (expected: "string" instead of expected: "nonoptional"):
| probe | a38ab4a |
this PR |
|---|---|---|
z.object({ a: z.string().exactOptional() }).parse({ a: undefined }) |
ERR nonoptional |
ERR string |
z.object({ a: z.any().exactOptional() }).parse({ a: undefined }) |
ERR nonoptional |
accepted |
z.exactOptional(z.any()).parse(undefined) |
ERR nonoptional |
accepted |
Full suite is green locally (7324 passed, 12 skipped); the only failures are the three @zod/treeshaking tests that need pnpm build first.
βΉοΈ Merging this before #6434 reopens the absent-key window on main
The prototype was doing a second thing the description only mentions in passing: it was incidentally masking #6236. With it gone, an absent key on a middle-rung inner materializes whatever that inner makes of undefined again β z.object({ a: z.coerce.string().exactOptional() }).parse({}) goes from {} back to { a: "undefined" }, .catch("C") back to { a: "C" }, and the tuple analog back to ["x", "undefined"]. That's the pre-#6432 behavior, not a new bug, and #6434 is the real fix β but nothing in the suite pins it in either direction, so the window is invisible to CI.
Technical details
# Absent-key behavior regresses between #6438 and #6434
## Affected sites
- `packages/zod/src/v4/core/schemas.ts:3746` β removing the prototype restores delegation, so an absent key runs the inner on `undefined` and `handlePropertyResult` assigns the result whenever it is not `undefined`.
## Observed (both revisions run locally)
| probe | `a38ab4a` | this PR |
| --- | --- | --- |
| `z.object({ a: z.coerce.string().exactOptional() }).parse({})` | `{}` | `{ a: "undefined" }` |
| `z.object({ a: z.string().catch("C").exactOptional() }).parse({})` | `{}` | `{ a: "C" }` |
| `z.tuple([z.string(), z.coerce.string().exactOptional()]).parse(["x"])` | `["x"]` | `["x", "undefined"]` |
## Required outcome
- `main` should not ship a release in the state between this revert and #6434.
## Notes
- The dependency runs the other way too: #6434's new `optin-ladder.test.ts` cases pin `z.object({ a: z.coerce.string().exactOptional() }).parse({ a: undefined })` to `{ a: "undefined" }`, which only holds once this hunk is gone. So #6438 is a prerequisite for #6434, and #6434 closes the window it opens.
- The full suite passes identically with and without the reverted hunk, which is how the prototype rode into `main` in #6432 unnoticed. #6434 adds the pins for both halves; if it stalls, that coverage gap stays open.Claude Opus ο½ π

The parse override in
$ZodExactOptionalon main carries a hunk labelled// OPTION B PROTOTYPE, merged in #6432. It changes what an exact optional accepts, which #6432 is otherwise not about, and the full suite passes without it.That inner accepts only
undefined, so the schema can never match when the key is present.{ a: undefined }on mainz.string()expected: "nonoptional"expected: "string"z.coerce.string()z.any(),z.unknown()z.undefined(),z.literal(undefined)z.string().catch("c")Each of those inners accepts
undefinedon its own, so an exact optional wrapping one has no basis to reject it. The rejections also replaced the specific type in the error withnonoptional.Only that hunk is reverted. The
propValueschange from #6432 stays β an omittable slot still claimsundefinedat a discriminator lookup β along with its tests and the compile-side duplicate-value guard.The absent-key half that the prototype also affected is handled in #6434, in the object layer, without touching what is accepted.
Refs #6432.