Skip to content

Drop the absent arms from s.fieldOr's Output - #463

Open
DZakh wants to merge 12 commits into
mainfrom
claude/schema-failing-cases-u2dnpj-fieldor-output
Open

DZakh wants to merge 12 commits into
mainfrom
claude/schema-failing-cases-u2dnpj-fieldor-output

Conversation

@DZakh

@DZakh DZakh commented Sep 24, 2026 •

Copy link
Copy Markdown
Owner

Fixes known bug fieldor-absent-item-output.

const schema = S.object((s) => ({ f: s.fieldOr("f", S.nullish(S.number), 2) }));
S.toOutputExpression(schema); // before: { f: number | undefined | null; }   after: { f: number | null; }
S.isOutput(schema, {});       // before: true                                after: false

{} used to pass isOutput, encode to { f: undefined }, and decode back to { f: 2 }. The default replaces every absent value, so no Output ever holds undefined. The same field spelled s.field("f", S.optional(S.nullish(S.number), 2)) already said number | null.

Fix

fieldOr decides on the Input (if(v===void 0)), so its Output is the item minus the arms only undefined reaches. presentOf narrows a bare union item to those arms, and the narrowed item is now:

  • the Output target, which isOutput, reverse, toOutputJSONSchemaOrThrow and isEqualOutput read;
  • what the default is validated against;
  • what a present value decodes with, so it no longer tests a v0===void 0 it can never reach.

Two deliberate limits:

  • A copy, not unionFactory. The first cut used unionFactory and measured object at +55%, because it brings the union planner to every object export. The item already carries its union decoder.
  • A union behind a .to is left whole. There, the link decides what an absent value would have become.

Not in this PR

  • Static type. It keeps the item's undefined, exactly as S.optional(S.nullish(x), d)'s does, so it's wide but sound. Narrowing it with Exclude would be wrong for an item that decodes a present value to undefined (S.optional(S.nullAsOption(x))). fieldor-optional-default records this as a Zod divergence.
  • Error message. fieldOr's Input expression still renders number | undefined | undefined. That's pre-existing and separate.

What changes for you

const a = S.object((s) => ({ f: s.fieldOr("f", S.nullish(S.number), 2) }));
const b = S.object((s) => ({ f: s.fieldOr("f", S.optional(S.number), 2) }));
const c = S.object((s) => ({ f: s.fieldOr("f", S.optional(S.number), 1) })).with(S.to, S.jsonString);
main this PR
toOutputJSONSchemaOrThrow(a) { properties: { f: { anyOf: [number, null] } } } same, plus required: ["f"]
isOutput(a)({}) true false
toOutputJSONSchemaOrThrow(b) { properties: { f: { type: "number" } } } same, plus required: ["f"]
isOutput(b)({}) true false
c's draft 2020-12 contentSchema no required required: ["f"]
encodeOrThrow("{}", c) {} throws Failed at f: Expected number, received undefined

Impact

Bundle vs main (2ebcac6), gzip: object 13235 → 13438 (+203 B, +1.5%), total 73883 → 73979 (+96 B). Generated code: fieldor-nullish-default parse/decode drop the unreachable v0===void 0 arm.

Decision

Kept the PR's answers on top of #490's fieldOr rewrite rather than revisiting them: the Output drops the absent arms, the static type stays wide. object costs more than before the merge (+203 B vs +79 B), because the one-arm case asks unionIsTransparent, which on main now carries the unionDescribing table.

Details

  • What main changed. Fix the critical and most important bugs from the FIXME and known-bug triage #490 rewrote fieldOrSchema: its Output is outputView(schema) (what the item claims once decoded, conversions dropped), and a present value decodes from the source's own present arm, not unknown. Neither drops the item's undefined arm, so on main the bug still reproduced and fieldor-absent-item-output was still listed.
  • How the fix sits on it. presentOf runs first and everything downstream is built from its result: outputView(presentOf(schema)), the reverse link's copy, setDefault and parseAs. The source-arm logic in the parser is main's, unchanged. unionIsTransparent keeps main's converts parameter and is exported for presentOf.
  • Specs. fieldor-nullish-default (FIXME example replaced by a null encode, Output required: ["f"]), fieldor-optional-default and fieldor-optional-jsonstring (new). Every other fieldOr spec, including Fix the critical and most important bugs from the FIXME and known-bug triage #490's (fieldor-from-optional-source, fieldor-async-item, fieldor-from-record-union), passes unchanged. fieldor-optional-union-default no longer moves: Fix the critical and most important bugs from the FIXME and known-bug triage #490 already removed its duplicate arms.
  • knownBugs. fieldor-absent-item-output is deleted: with it removed, the full fuzz:schema gate reports no findings.
  • Verify. Full pnpm verify passes, every step including ppx build and e2e. pnpm benchmarks --write and pnpm spec schema regenerated.

🤖 Generated with Claude Code

https://claude.ai/code/session_01X9uDwr3yJ2L75BYdetqyE1

`s.fieldOr("f", S.nullish(S.number), 2)` claimed `number | undefined | null`
as its Output, so `isOutput({})` was true and `{}` encoded to `{ f: undefined }`,
which decodes back to `{ f: 2 }`. The default replaces every absent value,
so no Output ever holds `undefined`: the same field spelled
`s.field("f", S.optional(S.nullish(S.number), 2))` already said `number | null`.

`fieldOr` decides on the Input (`if(v===void 0)`), so its Output is the item
less the arms only `undefined` reaches. `presentOf` narrows a bare union item
to those arms, and that narrowed item is now the Output target, what the
default is validated against, and what a present value decodes with. A copy,
not `unionFactory`, which would bring the union planner to every object export
(measured: `object` +55%). A union behind a `.to` is left whole, since there
the link decides what an absent value would have become.

The Output JSON Schema now marks the field required, and a present value no
longer tests `v0===void 0` it can never be. `fieldor-optional-union-default`'s
Output loses the duplicate arms it carried. The static type keeps the item's
`undefined`, as `S.optional(S.nullish(x), d)`'s does; narrowing it would be
wrong for an item that decodes a present value to `undefined`.

Known bug `fieldor-absent-item-output` is deleted. `fieldor-nullish-default`
drops its FIXME for a `null` encode, and `fieldor-optional-default` is new.
Bundle: `object` 10004 -> 10082 gzip, total +75.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
@coderabbitai

coderabbitai Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 45 seconds.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cc62a015-a611-4360-8650-a97f2d978157
Commits

Reviewing files that changed from the base of the PR and between 2ebcac6 and 6a22d4c.

Files selected for processing: 8
  • packages/benchmarks/goldens/schema.json
  • packages/sury/scripts/knownBugs.ts
  • packages/sury/specs/bundleSize.yaml
  • packages/sury/specs/fieldor-nullish-default.yaml
  • packages/sury/specs/fieldor-optional-default.yaml
  • packages/sury/specs/fieldor-optional-jsonstring.yaml
  • packages/sury/src/factory.ts
  • packages/sury/src/union.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions

Copy link
Copy Markdown

Spec performance

1 faster of 3741 timed targets · 1490738 vs e84e97a (origin/main)

Faster

target Δ vs baseline
jsonstring-novalidation-format · encode · accepts -5.2% faster

Full report ↗

+% slower than baseline, -% faster · noise floor create 3.0% · create+compile 4.9% · run 3.0% · scenario 3.0%
3740 unchanged · 132 constant-schema targets skipped · 40 async examples skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

…-cases-u2dnpj-fieldor-output

# Conflicts:
#	packages/benchmarks/goldens/schema.json
#	packages/sury/specs/bundleSize.yaml
@github-actions

Copy link
Copy Markdown

Spec performance

2 faster of 3856 timed targets · 1603f18 vs 4a723c8 (origin/main)

Faster

target Δ vs baseline
codec-uint8array-jsonstring-pack · decode · accepts -3.6% faster
fieldor-optional-default · create+compile · decode -7.3% faster

Full report ↗

+% slower than baseline, -% faster · noise floor create 3.0% · create+compile 5.1% · run 3.0% · scenario 3.0%
3854 unchanged · 132 constant-schema targets skipped · 40 async examples skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

Kept this branch's examples in fieldor-optional-union-default.yaml; its
code and every other generated file regenerated rather than hand-merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
@github-actions

Copy link
Copy Markdown

Spec performance

1 faster of 3883 timed targets · de06ba8 vs b9bf517 (origin/main)

Faster

target Δ vs baseline
fieldor-optional-default · create+compile · decode -7.8% faster

Full report ↗

+% slower than baseline, -% faster · noise floor create 10.6% · create+compile 3.0% · run 52.2% · scenario 3.0%
3882 unchanged · 132 constant-schema targets skipped · 45 async examples skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

No conflicts; every golden regenerates unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
Generated files (bundleSize.yaml, benchmark goldens) regenerated rather
than hand-merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
@github-actions

Copy link
Copy Markdown

Spec performance

2 slower of 3956 timed targets · e3a641b vs 0491207 (origin/main)

Slower

target Δ vs baseline
array · parse · accepts ×2 +9.4% slower
cidrv6 · encode · accepts +6.1% slower

Full report ↗

+% slower than baseline, -% faster · noise floor create 3.4% · create+compile 3.6% · run 4.2% · async 7.0% · scenario 3.0%
3954 unchanged · 132 constant-schema targets skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

claude added 3 commits October 1, 2026 10:05
Generated files (bundleSize.yaml, benchmark goldens) regenerated rather
than hand-merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
`presentOf` narrowed `S.optional(S.number)` to a union holding only
`number`, a shape `unionFactory` never builds: a union of one is its
member. Code since #478 relies on that. A per-variant conversion builds
`unionFactory(variants)` and marks the result per-variant, so with one
variant the mark landed on a plain number schema and the union encoder
crashed reading its arms:

  S.object((s) => ({ f: s.fieldOr("f", S.optional(S.number), 1) })).with(S.to, S.jsonString)
  // before: TypeError reading 'length' on the first isInput/parse
  // after:  {} parses to "{\"f\":1}"

A lone arm now comes back as itself when the union carries nothing of
its own, and the union stays whole when it has a refiner or metadata
that narrowing would drop. Found by fuzz:schema after merging main.
Spec: fieldor-optional-jsonstring.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
#477 rewrote knownBugs.ts and the fieldOr specs: both take main's
version, less this branch's fieldor-absent-item-output entry and with
its `null` encode example in place of the FIXME one. setHas now takes
the member schema rather than its type.

presentOf leaves an item with a default of its own whole: that item
already outputs its default in place of `undefined`, so it has no absent
arm to drop. Narrowing it anyway also took away the fuzzer's only draw
of main's jsonstring-null-default-inlined bug, whose spec still records
the crash, and the gate requires a listed bug to stay reached.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

Spec performance

2 slower, 10 faster of 4242 timed targets · 8c99f78 vs 4b9e6fb (origin/main)

Slower

target Δ vs baseline
fieldor-jsonstring-carrier · parse · accepts ×2 +6.7% slower
codec-string-jsonstring-pack · parse · accepts ×2 +3.1% slower

Faster

target Δ vs baseline
fieldor-optional-jsonstring · parse · accepts ×2 -17.9% faster
fieldor-optional-jsonstring · decode · accepts -21.2% faster
fieldor-optional-default · create+compile · encode -42.6% faster
fieldor-optional-jsonstring · create+compile · parse -48.4% faster
fieldor-optional-jsonstring · create+compile · decode -48.7% faster
fieldor-optional-jsonstring · create+compile · encode -50.8% faster
fieldor-optional-default · create+compile · decode -58.7% faster
fieldor-optional-default · create+compile · parse -64.7% faster
fieldor-optional-jsonstring · create -82.2% faster
fieldor-optional-default · create -86.6% faster

Full report ↗

+% slower than baseline, -% faster · noise floor create 3.0% · create+compile 3.0% · run 3.0% · async 4.3% · scenario 13.3%
4230 unchanged · 132 constant-schema targets skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

Generated files (bundleSize.yaml, benchmark goldens) regenerated rather
than hand-merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

Spec performance

11 faster of 4264 timed targets · eec4a1e vs 5522d8f (origin/main)

Faster

target Δ vs baseline
codec-string-jsonstring-pack · decode · accepts ×2 -7.4% faster
fieldor-optional-jsonstring · parse · accepts ×2 -18.1% faster
fieldor-optional-jsonstring · decode · accepts -18.6% faster
fieldor-optional-default · create+compile · encode -42.1% faster
fieldor-optional-jsonstring · create+compile · decode -43.9% faster
fieldor-optional-jsonstring · create+compile · parse -44.9% faster
fieldor-optional-jsonstring · create+compile · encode -51.3% faster
fieldor-optional-default · create+compile · parse -57.2% faster
fieldor-optional-default · create+compile · decode -59.9% faster
fieldor-optional-jsonstring · create -81.9% faster

…and 1 more.

Full report ↗

+% slower than baseline, -% faster · noise floor create 15.0% · create+compile 3.1% · run 3.0% · async 3.3% · scenario 10.4%
4253 unchanged · 132 constant-schema targets skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

Version bump only; no conflicts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown

Spec performance

10 faster of 4264 timed targets · 88b41c9 vs 63b73cf (origin/main)

Faster

target Δ vs baseline
fieldor-optional-jsonstring · parse · accepts ×2 -10.9% faster
fieldor-optional-jsonstring · decode · accepts -18.0% faster
fieldor-optional-default · create+compile · encode -43.3% faster
fieldor-optional-jsonstring · create+compile · parse -49.7% faster
fieldor-optional-jsonstring · create+compile · decode -50.4% faster
fieldor-optional-jsonstring · create+compile · encode -50.6% faster
fieldor-optional-default · create+compile · parse -58.6% faster
fieldor-optional-default · create+compile · decode -60.0% faster
fieldor-optional-jsonstring · create -82.4% faster
fieldor-optional-default · create -86.2% faster

Full report ↗

+% slower than baseline, -% faster · noise floor create 4.4% · create+compile 3.0% · run 6.4% · async 3.0% · scenario 9.7%
4254 unchanged · 132 constant-schema targets skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

Main's #490 rewrote s.fieldOr to read its Output through `outputView` and its
present item from the source. The absent-arm drop now sits in front of that:
`presentOf` trims the item first, and `outputView`, the reverse link, the
default and `parseAs` are all built from the trimmed item. `unionIsTransparent`
takes main's `converts` parameter and is exported for it.

The schema fuzzer gate no longer reaches `fieldor-absent-item-output`, so the
entry is gone; `admitsUndefined` stays imported for
`json-absent-recursive-undefined`. Bundle size and benchmark goldens are
regenerated over main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X9uDwr3yJ2L75BYdetqyE1
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Spec performance

1 slower, 10 faster of 6484 timed targets · 6a22d4c vs 2ebcac6 (origin/main)

Slower

target Δ vs baseline
recursive-same-name · decode · accepts +6.3% slower

Faster

target Δ vs baseline
fieldor-optional-jsonstring · decode · accepts -7.6% faster
fieldor-optional-jsonstring · parse · accepts ×2 -17.6% faster
fieldor-optional-jsonstring · create+compile · encode -41.3% faster
fieldor-optional-default · create+compile · encode -41.7% faster
fieldor-optional-jsonstring · create+compile · decode -46.7% faster
fieldor-optional-jsonstring · create+compile · parse -48.8% faster
fieldor-optional-default · create+compile · parse -53.2% faster
fieldor-optional-default · create+compile · decode -59.1% faster
fieldor-optional-jsonstring · create -77.4% faster
fieldor-optional-default · create -83.1% faster

Full report ↗

+% slower than baseline, -% faster · noise floor create 3.0% · create+compile 3.0% · run 3.0% · async 6.0% · scenario 3.0%
6473 unchanged · 134 constant-schema targets skipped · advisory only
node 24.16.0 · linux x64 · 4 cores · 8×2 rounds · 2 screening jobs · confirmed by 2 fresh processes

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