Repository navigation
Conversation
`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
|
Warning Review limit reachedYou'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. View limit details
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. |
Spec performance1 faster of 3741 timed targets · Faster
+% slower than baseline, -% faster · noise floor create 3.0% · create+compile 4.9% · run 3.0% · scenario 3.0% |
…-cases-u2dnpj-fieldor-output # Conflicts: # packages/benchmarks/goldens/schema.json # packages/sury/specs/bundleSize.yaml
Spec performance2 faster of 3856 timed targets · Faster
+% slower than baseline, -% faster · noise floor create 3.0% · create+compile 5.1% · run 3.0% · scenario 3.0% |
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
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
Spec performance1 faster of 3883 timed targets · Faster
+% slower than baseline, -% faster · noise floor create 10.6% · create+compile 3.0% · run 52.2% · scenario 3.0% |
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
Spec performance2 slower of 3956 timed targets · Slower
+% slower than baseline, -% faster · noise floor create 3.4% · create+compile 3.6% · run 4.2% · async 7.0% · scenario 3.0% |
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
Spec performance2 slower, 10 faster of 4242 timed targets · Slower
Faster
+% slower than baseline, -% faster · noise floor create 3.0% · create+compile 3.0% · run 3.0% · async 4.3% · scenario 13.3% |
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
Spec performance11 faster of 4264 timed targets · Faster
…and 1 more. +% slower than baseline, -% faster · noise floor create 15.0% · create+compile 3.1% · run 3.0% · async 3.3% · scenario 10.4% |
Version bump only; no conflicts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XdDnkN38pECC46XqMZbwp8
Spec performance10 faster of 4264 timed targets · Faster
+% slower than baseline, -% faster · noise floor create 4.4% · create+compile 3.0% · run 6.4% · async 3.0% · scenario 9.7% |
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
Spec performance1 slower, 10 faster of 6484 timed targets · Slower
Faster
+% slower than baseline, -% faster · noise floor create 3.0% · create+compile 3.0% · run 3.0% · async 6.0% · scenario 3.0% |
Fixes known bug
fieldor-absent-item-output.{}used to passisOutput, encode to{ f: undefined }, and decode back to{ f: 2 }. The default replaces every absent value, so no Output ever holdsundefined. The same field spelleds.field("f", S.optional(S.nullish(S.number), 2))already saidnumber | null.Fix
fieldOrdecides on the Input (if(v===void 0)), so its Output is the item minus the arms onlyundefinedreaches.presentOfnarrows a bare union item to those arms, and the narrowed item is now:isOutput,reverse,toOutputJSONSchemaOrThrowandisEqualOutputread;v0===void 0it can never reach.Two deliberate limits:
unionFactory. The first cut usedunionFactoryand measuredobjectat +55%, because it brings the union planner to every object export. The item already carries its union decoder..tois left whole. There, the link decides what an absent value would have become.Not in this PR
undefined, exactly asS.optional(S.nullish(x), d)'s does, so it's wide but sound. Narrowing it withExcludewould be wrong for an item that decodes a present value toundefined(S.optional(S.nullAsOption(x))).fieldor-optional-defaultrecords this as a Zod divergence.fieldOr's Input expression still rendersnumber | undefined | undefined. That's pre-existing and separate.What changes for you
toOutputJSONSchemaOrThrow(a){ properties: { f: { anyOf: [number, null] } } }required: ["f"]isOutput(a)({})truefalsetoOutputJSONSchemaOrThrow(b){ properties: { f: { type: "number" } } }required: ["f"]isOutput(b)({})truefalsec's draft 2020-12contentSchemarequiredrequired: ["f"]encodeOrThrow("{}", c){}Failed at f: Expected number, received undefinedImpact
Bundle vs
main(2ebcac6), gzip:object13235 → 13438 (+203 B, +1.5%), total 73883 → 73979 (+96 B). Generated code:fieldor-nullish-defaultparse/decode drop the unreachablev0===void 0arm.Decision
Kept the PR's answers on top of #490's
fieldOrrewrite rather than revisiting them: the Output drops the absent arms, the static type stays wide.objectcosts more than before the merge (+203 B vs +79 B), because the one-arm case asksunionIsTransparent, which onmainnow carries theunionDescribingtable.Details
mainchanged. Fix the critical and most important bugs from the FIXME and known-bug triage #490 rewrotefieldOrSchema: its Output isoutputView(schema)(what the item claims once decoded, conversions dropped), and a present value decodes from the source's own present arm, notunknown. Neither drops the item'sundefinedarm, so onmainthe bug still reproduced andfieldor-absent-item-outputwas still listed.presentOfruns first and everything downstream is built from its result:outputView(presentOf(schema)), the reverse link's copy,setDefaultandparseAs. The source-arm logic in the parser ismain's, unchanged.unionIsTransparentkeepsmain'sconvertsparameter and is exported forpresentOf.fieldor-nullish-default(FIXME example replaced by anullencode, Outputrequired: ["f"]),fieldor-optional-defaultandfieldor-optional-jsonstring(new). Every otherfieldOrspec, 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-defaultno longer moves: Fix the critical and most important bugs from the FIXME and known-bug triage #490 already removed its duplicate arms.fieldor-absent-item-outputis deleted: with it removed, the fullfuzz:schemagate reports no findings.pnpm verifypasses, every step including ppx build and e2e.pnpm benchmarks --writeandpnpm spec schemaregenerated.🤖 Generated with Claude Code
https://claude.ai/code/session_01X9uDwr3yJ2L75BYdetqyE1