Repository navigation
Conversation
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
|
Thanks for digging into this! In the next release,
The way to check whether this is sound: any value the type allows should still pass the schema after
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 Since that change is already in progress, I'm going to leave this PR unmerged, but I appreciate the thorough write-up and tests! |
~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).
…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).
Fixes #1462
Summary
toJsonSchema()threw for types liketype({ maybe: "string | undefined" }):Since
undefinedis not a valid JSON value, a union branch with anundefinedunit can never be satisfied by JSON data. Such branches are now dropped during conversion instead of triggering the unjsonifiableunitfallback. A lone remaining branch is returned without ananyOfwrapper, sostring | undefinedconverts to{ type: "string" }.If every branch is undefined, e.g. a standalone
undefinedtype, conversion still fails loudly via the fallback.Tests
ark/type/__tests__/toJsonSchema.test.ts: newundefinedcases covering the issue repro, optional props,enumeratedincludingundefined,null | undefined, and standaloneundefinedark/schema/__tests__/jsonSchema.test.ts: schema-level cases for dropping undefined branches from unions, literal unions, and prop valuesark/type/__tests__/standardSchema.test.ts: updated the~standard.jsonSchemasnapshot to reflectstring | undefinednow converting to{ type: "string" }pnpm prCheckspasses locally (lint, build, typecheck, tests, TS version matrix).