arktype icon indicating copy to clipboard operation
arktype copied to clipboard

JSON Schema generation fails for discriminated unions in recursive scopes

Open seatedro opened this issue 2 months ago • 0 comments

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

Playground Link

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

seatedro avatar Nov 14 '25 02:11 seatedro