arktype
arktype copied to clipboard
JSON Schema generation fails for discriminated unions in recursive scopes
Report a bug
toJsonSchema() throws "Unexpected JSON Schema input for structure: {}" when generating JSON schema for a recursive scope containing a discriminated union where the union members have additional fields beyond the discriminator.
Workaround is to define the union outside the scope.
🔎 Search Terms
- toJSONSchema
- discriminated unions
- recursive types
- cyclic types
🧩 Context
- ArkType version:
2.1.26 - TypeScript version (5.1+):
5.8.3 - Other context you think may be relevant (JS flavor, OS, etc.):
- Runtimes: Node.js v22.19.0, Bun 1.3.0
🧑💻 Repro
import { scope } from "arktype";
const s = scope({
TypeA: {
kind: "'a'",
value: "number" // Additional field beyond discriminator
},
TypeB: {
kind: "'b'",
value: "number" // Additional field beyond discriminator
},
Union: "TypeA | TypeB",
Node: {
source: "SearchNode", // Recursive reference
data: "Union",
},
SearchNode: "Node",
});
const out = s.type("SearchNode").toJsonSchema(); // ❌ Throws error