Skip to content

revert(core): drop the exactOptional parse prototype from #6432 - #6438

Merged
colinhacks merged 1 commit into
mainfrom
fix/revert-exactoptional-prototype
Aug 19, 2026
Merged

colinhacks merged 1 commit into
mainfrom
fix/revert-exactoptional-prototype

Conversation

@colinhacks

Copy link
Copy Markdown
Owner

The parse override in $ZodExactOptional on 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.

z.object({ a: z.exactOptional(z.undefined()) }).safeParse({ a: undefined }); // rejected on main

That inner accepts only undefined, so the schema can never match when the key is present.

inner { a: undefined } on main after this revert
z.string() rejected, expected: "nonoptional" rejected, expected: "string"
z.coerce.string() rejected accepted
z.any(), z.unknown() rejected accepted
z.undefined(), z.literal(undefined) rejected accepted
z.string().catch("c") rejected accepted

Each of those inners accepts undefined on its own, so an exact optional wrapping one has no basis to reject it. The rejections also replaced the specific type in the error with nonoptional.

Only that hunk is reverted. The propValues change from #6432 stays β€” an omittable slot still claims undefined at 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.

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.
Copilot AI lite review requested due to automatic review settings August 19, 2026 14:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

βœ… 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 PROTOTYPE hunk from $ZodExactOptional β€” the parse override goes back to a bare def.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 against a38ab4a8 confirms nothing else from that commit is touched.
  • The override itself correctly stays β€” $ZodOptional.init installs a parse that short-circuits on undefined, so an exact optional has to replace it to reach the inner at all.
  • The rest of #6432 survives β€” the field.optin !== undefined addition to propValues in $ZodObject, its discriminated-union test, and the compile-side duplicate-discriminator guard are all untouched, and the an omittable discriminator claims undefined test 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.

Pullfrog  | View workflow run | Using Claude Opus | 𝕏

@colinhacks
colinhacks merged commit 11c9268 into main Aug 19, 2026
9 checks passed
@colinhacks
colinhacks deleted the fix/revert-exactoptional-prototype branch August 19, 2026 14:51

This branch was successfully deployed

1 active deployment
Preview – zod-v4 β€” 17a20f37 Deployed Aug 19, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants