Skip to content

fix(schema): drop undefined union branches in toJsonSchema() - #1715

Open
SynthLuvr wants to merge 1 commit into
arktypeio:mainfrom
SynthLuvr:fix/1462-toJsonSchema-undefined
Open

SynthLuvr wants to merge 1 commit into
arktypeio:mainfrom
SynthLuvr:fix/1462-toJsonSchema-undefined

Conversation

@SynthLuvr

Copy link
Copy Markdown
Contributor

Fixes #1462

Summary

toJsonSchema() threw for types like type({ maybe: "string | undefined" }):

ToJsonSchemaError: {
    code: "unit",
    base: {},
    unit: undefined
}

Since undefined is not a valid JSON value, a union branch with an undefined unit can never be satisfied by JSON data. Such branches are now dropped during conversion instead of triggering the unjsonifiable unit fallback. A lone remaining branch is returned without an anyOf wrapper, so string | undefined converts to { type: "string" }.

If every branch is undefined, e.g. a standalone undefined type, conversion still fails loudly via the fallback.

Tests

  • ark/type/__tests__/toJsonSchema.test.ts: new undefined cases covering the issue repro, optional props, enumerated including undefined, null | undefined, and standalone undefined
  • ark/schema/__tests__/jsonSchema.test.ts: schema-level cases for dropping undefined branches from unions, literal unions, and prop values
  • ark/type/__tests__/standardSchema.test.ts: updated the ~standard.jsonSchema snapshot to reflect string | undefined now converting to { type: "string" }

pnpm prChecks passes locally (lint, build, typecheck, tests, TS version matrix).

undefined is not a valid JSON value, so a branch with an undefined
unit can never be satisfied by JSON data. Such branches are now
dropped during conversion instead of triggering the unjsonifiable
"unit" fallback. If every branch is undefined, e.g. a standalone
undefined type, conversion still fails loudly via the fallback.

Fixes arktypeio#1462
@ssalbdivad

Copy link
Copy Markdown
Member

Thanks for digging into this! In the next release, undefined will be handled at the key level rather than by dropping union branches:

  • An optional key whose value allows undefined leaves it out, so { "a?": "string | undefined" } converts to { type: "object", properties: { a: { type: "string" } } }.
  • Anywhere else, undefined still goes through the unit fallback, which throws by default.

The way to check whether this is sound: any value the type allows should still pass the schema after JSON.stringify. For an optional key that holds, because { a: undefined } serializes to {}, and the key can already be absent. Dropping the branch everywhere breaks it:

  • { a: "string | undefined" } would become a required a: string, but { a: undefined } serializes to {}, which that schema rejects.
  • (string | undefined)[] would become string[], but [undefined] serializes to [null].

So outside an optional key there's no single right answer for the library to pick silently. If dropping those branches is what you want, the fallback makes it a one-liner:

T.toJsonSchema({
	fallback: {
		// a schema that allows nothing, so the branch drops out of the union
		unit: ctx => (ctx.unit === undefined ? { not: {} } : ctx.base)
	}
})

You could also return { type: "null" } to match how arrays serialize.

Since that change is already in progress, I'm going to leave this PR unmerged, but I appreciate the thorough write-up and tests!

ssalbdivad added a commit that referenced this pull request Nov 9, 2026
~standard.jsonSchema.input for type({ a: "string | undefined" }) wrote
a: { anyOf: [{ type: "string" }, {}] }, whose {} branch lets any JSON value
through, though no JSON input can match the undefined it stands for. JSON
has no undefined, so the inputs a union accepts from JSON are exactly its
other branches: an input schema now writes a: { type: "string" }, keeps a
in required (the key still has to be present), and does the same for array
elements and nullable unions.

Only the input side changes. Output and toJsonSchema() still route a
required key's undefined to fallback.unit, as 1e000b9 decided: data with
a: undefined serializes without the key, which no schema listing a in
required accepts, so dropping the branch there would describe output the
type can produce as invalid.

Refs #1462 (and supersedes the input half of PR #1715, which drops
undefined branches for every io and bypasses a configured fallback.unit).
ssalbdivad added a commit that referenced this pull request Nov 9, 2026
…uthors

Each fix below landed on featherduster from a port or an independent fix,
but each started from a reasonable community PR against 2.x, and the
CHANGELOG thanks every contributor whose work this release supersedes:

- #1721 (@RaphaelFakhri): fast-check keeps a regex's flags (#1720)
- #1722 (@louisss1016): Temporal values survive a morph / deepClone (#1719)
- #1725 (@RClaudiuM): an undefined discriminant case checks its key (#1724)
- #1715 (@SynthLuvr): ~standard.jsonSchema.input drops undefined (#1462);
  only its input half was taken, output keeps routing to fallback.unit

#1718 is not credited: 3.0 answers #1469 with jsonSchema meta rather than
its bare pattern key. #1705 and #1695 were already thanked.

Also notes that a cyclic index signature with no way out now generates
shrinking dictionaries instead of overflowing the stack (6f4dd5d).
@ssalbdivad ssalbdivad mentioned this pull request Nov 9, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To do

Development

Successfully merging this pull request may close these issues.

toJsonSchema() fails when a field is string | undefined

2 participants