json-schema-processors.cjs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.allProcessors = exports.lazyProcessor = exports.optionalProcessor = exports.promiseProcessor = exports.readonlyProcessor = exports.pipeProcessor = exports.catchProcessor = exports.prefaultProcessor = exports.defaultProcessor = exports.nonoptionalProcessor = exports.nullableProcessor = exports.recordProcessor = exports.tupleProcessor = exports.intersectionProcessor = exports.unionProcessor = exports.objectProcessor = exports.arrayProcessor = exports.setProcessor = exports.mapProcessor = exports.transformProcessor = exports.functionProcessor = exports.customProcessor = exports.successProcessor = exports.fileProcessor = exports.templateLiteralProcessor = exports.nanProcessor = exports.literalProcessor = exports.enumProcessor = exports.dateProcessor = exports.unknownProcessor = exports.anyProcessor = exports.neverProcessor = exports.voidProcessor = exports.undefinedProcessor = exports.nullProcessor = exports.symbolProcessor = exports.bigintProcessor = exports.booleanProcessor = exports.numberProcessor = exports.stringProcessor = void 0;
  4. exports.toJSONSchema = toJSONSchema;
  5. const to_json_schema_js_1 = require("./to-json-schema.cjs");
  6. const util_js_1 = require("./util.cjs");
  7. const formatMap = {
  8. guid: "uuid",
  9. url: "uri",
  10. datetime: "date-time",
  11. json_string: "json-string",
  12. regex: "", // do not set
  13. };
  14. // ==================== SIMPLE TYPE PROCESSORS ====================
  15. const stringProcessor = (schema, ctx, _json, _params) => {
  16. const json = _json;
  17. json.type = "string";
  18. const { minimum, maximum, format, patterns, contentEncoding } = schema._zod
  19. .bag;
  20. if (typeof minimum === "number")
  21. json.minLength = minimum;
  22. if (typeof maximum === "number")
  23. json.maxLength = maximum;
  24. // custom pattern overrides format
  25. if (format) {
  26. json.format = formatMap[format] ?? format;
  27. if (json.format === "")
  28. delete json.format; // empty format is not valid
  29. // JSON Schema format: "time" requires a full time with offset or Z
  30. // z.iso.time() does not include timezone information, so format: "time" should never be used
  31. if (format === "time") {
  32. delete json.format;
  33. }
  34. }
  35. if (contentEncoding)
  36. json.contentEncoding = contentEncoding;
  37. if (patterns && patterns.size > 0) {
  38. const regexes = [...patterns];
  39. if (regexes.length === 1)
  40. json.pattern = regexes[0].source;
  41. else if (regexes.length > 1) {
  42. json.allOf = [
  43. ...regexes.map((regex) => ({
  44. ...(ctx.target === "draft-07" || ctx.target === "draft-04" || ctx.target === "openapi-3.0"
  45. ? { type: "string" }
  46. : {}),
  47. pattern: regex.source,
  48. })),
  49. ];
  50. }
  51. }
  52. };
  53. exports.stringProcessor = stringProcessor;
  54. const numberProcessor = (schema, ctx, _json, _params) => {
  55. const json = _json;
  56. const { minimum, maximum, format, multipleOf, exclusiveMaximum, exclusiveMinimum } = schema._zod.bag;
  57. if (typeof format === "string" && format.includes("int"))
  58. json.type = "integer";
  59. else
  60. json.type = "number";
  61. if (typeof exclusiveMinimum === "number") {
  62. if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
  63. json.minimum = exclusiveMinimum;
  64. json.exclusiveMinimum = true;
  65. }
  66. else {
  67. json.exclusiveMinimum = exclusiveMinimum;
  68. }
  69. }
  70. if (typeof minimum === "number") {
  71. json.minimum = minimum;
  72. if (typeof exclusiveMinimum === "number" && ctx.target !== "draft-04") {
  73. if (exclusiveMinimum >= minimum)
  74. delete json.minimum;
  75. else
  76. delete json.exclusiveMinimum;
  77. }
  78. }
  79. if (typeof exclusiveMaximum === "number") {
  80. if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
  81. json.maximum = exclusiveMaximum;
  82. json.exclusiveMaximum = true;
  83. }
  84. else {
  85. json.exclusiveMaximum = exclusiveMaximum;
  86. }
  87. }
  88. if (typeof maximum === "number") {
  89. json.maximum = maximum;
  90. if (typeof exclusiveMaximum === "number" && ctx.target !== "draft-04") {
  91. if (exclusiveMaximum <= maximum)
  92. delete json.maximum;
  93. else
  94. delete json.exclusiveMaximum;
  95. }
  96. }
  97. if (typeof multipleOf === "number")
  98. json.multipleOf = multipleOf;
  99. };
  100. exports.numberProcessor = numberProcessor;
  101. const booleanProcessor = (_schema, _ctx, json, _params) => {
  102. json.type = "boolean";
  103. };
  104. exports.booleanProcessor = booleanProcessor;
  105. const bigintProcessor = (_schema, ctx, _json, _params) => {
  106. if (ctx.unrepresentable === "throw") {
  107. throw new Error("BigInt cannot be represented in JSON Schema");
  108. }
  109. };
  110. exports.bigintProcessor = bigintProcessor;
  111. const symbolProcessor = (_schema, ctx, _json, _params) => {
  112. if (ctx.unrepresentable === "throw") {
  113. throw new Error("Symbols cannot be represented in JSON Schema");
  114. }
  115. };
  116. exports.symbolProcessor = symbolProcessor;
  117. const nullProcessor = (_schema, ctx, json, _params) => {
  118. if (ctx.target === "openapi-3.0") {
  119. json.type = "string";
  120. json.nullable = true;
  121. json.enum = [null];
  122. }
  123. else {
  124. json.type = "null";
  125. }
  126. };
  127. exports.nullProcessor = nullProcessor;
  128. const undefinedProcessor = (_schema, ctx, _json, _params) => {
  129. if (ctx.unrepresentable === "throw") {
  130. throw new Error("Undefined cannot be represented in JSON Schema");
  131. }
  132. };
  133. exports.undefinedProcessor = undefinedProcessor;
  134. const voidProcessor = (_schema, ctx, _json, _params) => {
  135. if (ctx.unrepresentable === "throw") {
  136. throw new Error("Void cannot be represented in JSON Schema");
  137. }
  138. };
  139. exports.voidProcessor = voidProcessor;
  140. const neverProcessor = (_schema, _ctx, json, _params) => {
  141. json.not = {};
  142. };
  143. exports.neverProcessor = neverProcessor;
  144. const anyProcessor = (_schema, _ctx, _json, _params) => {
  145. // empty schema accepts anything
  146. };
  147. exports.anyProcessor = anyProcessor;
  148. const unknownProcessor = (_schema, _ctx, _json, _params) => {
  149. // empty schema accepts anything
  150. };
  151. exports.unknownProcessor = unknownProcessor;
  152. const dateProcessor = (_schema, ctx, _json, _params) => {
  153. if (ctx.unrepresentable === "throw") {
  154. throw new Error("Date cannot be represented in JSON Schema");
  155. }
  156. };
  157. exports.dateProcessor = dateProcessor;
  158. const enumProcessor = (schema, _ctx, json, _params) => {
  159. const def = schema._zod.def;
  160. const values = (0, util_js_1.getEnumValues)(def.entries);
  161. // Number enums can have both string and number values
  162. if (values.every((v) => typeof v === "number"))
  163. json.type = "number";
  164. if (values.every((v) => typeof v === "string"))
  165. json.type = "string";
  166. json.enum = values;
  167. };
  168. exports.enumProcessor = enumProcessor;
  169. const literalProcessor = (schema, ctx, json, _params) => {
  170. const def = schema._zod.def;
  171. const vals = [];
  172. for (const val of def.values) {
  173. if (val === undefined) {
  174. if (ctx.unrepresentable === "throw") {
  175. throw new Error("Literal `undefined` cannot be represented in JSON Schema");
  176. }
  177. else {
  178. // do not add to vals
  179. }
  180. }
  181. else if (typeof val === "bigint") {
  182. if (ctx.unrepresentable === "throw") {
  183. throw new Error("BigInt literals cannot be represented in JSON Schema");
  184. }
  185. else {
  186. vals.push(Number(val));
  187. }
  188. }
  189. else {
  190. vals.push(val);
  191. }
  192. }
  193. if (vals.length === 0) {
  194. // do nothing (an undefined literal was stripped)
  195. }
  196. else if (vals.length === 1) {
  197. const val = vals[0];
  198. json.type = val === null ? "null" : typeof val;
  199. if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
  200. json.enum = [val];
  201. }
  202. else {
  203. json.const = val;
  204. }
  205. }
  206. else {
  207. if (vals.every((v) => typeof v === "number"))
  208. json.type = "number";
  209. if (vals.every((v) => typeof v === "string"))
  210. json.type = "string";
  211. if (vals.every((v) => typeof v === "boolean"))
  212. json.type = "boolean";
  213. if (vals.every((v) => v === null))
  214. json.type = "null";
  215. json.enum = vals;
  216. }
  217. };
  218. exports.literalProcessor = literalProcessor;
  219. const nanProcessor = (_schema, ctx, _json, _params) => {
  220. if (ctx.unrepresentable === "throw") {
  221. throw new Error("NaN cannot be represented in JSON Schema");
  222. }
  223. };
  224. exports.nanProcessor = nanProcessor;
  225. const templateLiteralProcessor = (schema, _ctx, json, _params) => {
  226. const _json = json;
  227. const pattern = schema._zod.pattern;
  228. if (!pattern)
  229. throw new Error("Pattern not found in template literal");
  230. _json.type = "string";
  231. _json.pattern = pattern.source;
  232. };
  233. exports.templateLiteralProcessor = templateLiteralProcessor;
  234. const fileProcessor = (schema, _ctx, json, _params) => {
  235. const _json = json;
  236. const file = {
  237. type: "string",
  238. format: "binary",
  239. contentEncoding: "binary",
  240. };
  241. const { minimum, maximum, mime } = schema._zod.bag;
  242. if (minimum !== undefined)
  243. file.minLength = minimum;
  244. if (maximum !== undefined)
  245. file.maxLength = maximum;
  246. if (mime) {
  247. if (mime.length === 1) {
  248. file.contentMediaType = mime[0];
  249. Object.assign(_json, file);
  250. }
  251. else {
  252. Object.assign(_json, file); // shared props at root
  253. _json.anyOf = mime.map((m) => ({ contentMediaType: m })); // only contentMediaType differs
  254. }
  255. }
  256. else {
  257. Object.assign(_json, file);
  258. }
  259. };
  260. exports.fileProcessor = fileProcessor;
  261. const successProcessor = (_schema, _ctx, json, _params) => {
  262. json.type = "boolean";
  263. };
  264. exports.successProcessor = successProcessor;
  265. const customProcessor = (_schema, ctx, _json, _params) => {
  266. if (ctx.unrepresentable === "throw") {
  267. throw new Error("Custom types cannot be represented in JSON Schema");
  268. }
  269. };
  270. exports.customProcessor = customProcessor;
  271. const functionProcessor = (_schema, ctx, _json, _params) => {
  272. if (ctx.unrepresentable === "throw") {
  273. throw new Error("Function types cannot be represented in JSON Schema");
  274. }
  275. };
  276. exports.functionProcessor = functionProcessor;
  277. const transformProcessor = (_schema, ctx, _json, _params) => {
  278. if (ctx.unrepresentable === "throw") {
  279. throw new Error("Transforms cannot be represented in JSON Schema");
  280. }
  281. };
  282. exports.transformProcessor = transformProcessor;
  283. const mapProcessor = (_schema, ctx, _json, _params) => {
  284. if (ctx.unrepresentable === "throw") {
  285. throw new Error("Map cannot be represented in JSON Schema");
  286. }
  287. };
  288. exports.mapProcessor = mapProcessor;
  289. const setProcessor = (_schema, ctx, _json, _params) => {
  290. if (ctx.unrepresentable === "throw") {
  291. throw new Error("Set cannot be represented in JSON Schema");
  292. }
  293. };
  294. exports.setProcessor = setProcessor;
  295. // ==================== COMPOSITE TYPE PROCESSORS ====================
  296. const arrayProcessor = (schema, ctx, _json, params) => {
  297. const json = _json;
  298. const def = schema._zod.def;
  299. const { minimum, maximum } = schema._zod.bag;
  300. if (typeof minimum === "number")
  301. json.minItems = minimum;
  302. if (typeof maximum === "number")
  303. json.maxItems = maximum;
  304. json.type = "array";
  305. json.items = (0, to_json_schema_js_1.process)(def.element, ctx, { ...params, path: [...params.path, "items"] });
  306. };
  307. exports.arrayProcessor = arrayProcessor;
  308. const objectProcessor = (schema, ctx, _json, params) => {
  309. const json = _json;
  310. const def = schema._zod.def;
  311. json.type = "object";
  312. json.properties = {};
  313. const shape = def.shape;
  314. for (const key in shape) {
  315. json.properties[key] = (0, to_json_schema_js_1.process)(shape[key], ctx, {
  316. ...params,
  317. path: [...params.path, "properties", key],
  318. });
  319. }
  320. // required keys
  321. const allKeys = new Set(Object.keys(shape));
  322. const requiredKeys = new Set([...allKeys].filter((key) => {
  323. const v = def.shape[key]._zod;
  324. if (ctx.io === "input") {
  325. return v.optin === undefined;
  326. }
  327. else {
  328. return v.optout === undefined;
  329. }
  330. }));
  331. if (requiredKeys.size > 0) {
  332. json.required = Array.from(requiredKeys);
  333. }
  334. // catchall
  335. if (def.catchall?._zod.def.type === "never") {
  336. // strict
  337. json.additionalProperties = false;
  338. }
  339. else if (!def.catchall) {
  340. // regular
  341. if (ctx.io === "output")
  342. json.additionalProperties = false;
  343. }
  344. else if (def.catchall) {
  345. json.additionalProperties = (0, to_json_schema_js_1.process)(def.catchall, ctx, {
  346. ...params,
  347. path: [...params.path, "additionalProperties"],
  348. });
  349. }
  350. };
  351. exports.objectProcessor = objectProcessor;
  352. const unionProcessor = (schema, ctx, json, params) => {
  353. const def = schema._zod.def;
  354. // Exclusive unions (inclusive === false) use oneOf (exactly one match) instead of anyOf (one or more matches)
  355. // This includes both z.xor() and discriminated unions
  356. const isExclusive = def.inclusive === false;
  357. const options = def.options.map((x, i) => (0, to_json_schema_js_1.process)(x, ctx, {
  358. ...params,
  359. path: [...params.path, isExclusive ? "oneOf" : "anyOf", i],
  360. }));
  361. if (isExclusive) {
  362. json.oneOf = options;
  363. }
  364. else {
  365. json.anyOf = options;
  366. }
  367. };
  368. exports.unionProcessor = unionProcessor;
  369. const intersectionProcessor = (schema, ctx, json, params) => {
  370. const def = schema._zod.def;
  371. const a = (0, to_json_schema_js_1.process)(def.left, ctx, {
  372. ...params,
  373. path: [...params.path, "allOf", 0],
  374. });
  375. const b = (0, to_json_schema_js_1.process)(def.right, ctx, {
  376. ...params,
  377. path: [...params.path, "allOf", 1],
  378. });
  379. const isSimpleIntersection = (val) => "allOf" in val && Object.keys(val).length === 1;
  380. const allOf = [
  381. ...(isSimpleIntersection(a) ? a.allOf : [a]),
  382. ...(isSimpleIntersection(b) ? b.allOf : [b]),
  383. ];
  384. json.allOf = allOf;
  385. };
  386. exports.intersectionProcessor = intersectionProcessor;
  387. const tupleProcessor = (schema, ctx, _json, params) => {
  388. const json = _json;
  389. const def = schema._zod.def;
  390. json.type = "array";
  391. const prefixPath = ctx.target === "draft-2020-12" ? "prefixItems" : "items";
  392. const restPath = ctx.target === "draft-2020-12" ? "items" : ctx.target === "openapi-3.0" ? "items" : "additionalItems";
  393. const prefixItems = def.items.map((x, i) => (0, to_json_schema_js_1.process)(x, ctx, {
  394. ...params,
  395. path: [...params.path, prefixPath, i],
  396. }));
  397. const rest = def.rest
  398. ? (0, to_json_schema_js_1.process)(def.rest, ctx, {
  399. ...params,
  400. path: [...params.path, restPath, ...(ctx.target === "openapi-3.0" ? [def.items.length] : [])],
  401. })
  402. : null;
  403. if (ctx.target === "draft-2020-12") {
  404. json.prefixItems = prefixItems;
  405. if (rest) {
  406. json.items = rest;
  407. }
  408. }
  409. else if (ctx.target === "openapi-3.0") {
  410. json.items = {
  411. anyOf: prefixItems,
  412. };
  413. if (rest) {
  414. json.items.anyOf.push(rest);
  415. }
  416. json.minItems = prefixItems.length;
  417. if (!rest) {
  418. json.maxItems = prefixItems.length;
  419. }
  420. }
  421. else {
  422. json.items = prefixItems;
  423. if (rest) {
  424. json.additionalItems = rest;
  425. }
  426. }
  427. // length
  428. const { minimum, maximum } = schema._zod.bag;
  429. if (typeof minimum === "number")
  430. json.minItems = minimum;
  431. if (typeof maximum === "number")
  432. json.maxItems = maximum;
  433. };
  434. exports.tupleProcessor = tupleProcessor;
  435. const recordProcessor = (schema, ctx, _json, params) => {
  436. const json = _json;
  437. const def = schema._zod.def;
  438. json.type = "object";
  439. // For looseRecord with regex patterns, use patternProperties
  440. // This correctly represents "only validate keys matching the pattern" semantics
  441. // and composes well with allOf (intersections)
  442. const keyType = def.keyType;
  443. const keyBag = keyType._zod.bag;
  444. const patterns = keyBag?.patterns;
  445. if (def.mode === "loose" && patterns && patterns.size > 0) {
  446. // Use patternProperties for looseRecord with regex patterns
  447. const valueSchema = (0, to_json_schema_js_1.process)(def.valueType, ctx, {
  448. ...params,
  449. path: [...params.path, "patternProperties", "*"],
  450. });
  451. json.patternProperties = {};
  452. for (const pattern of patterns) {
  453. json.patternProperties[pattern.source] = valueSchema;
  454. }
  455. }
  456. else {
  457. // Default behavior: use propertyNames + additionalProperties
  458. if (ctx.target === "draft-07" || ctx.target === "draft-2020-12") {
  459. json.propertyNames = (0, to_json_schema_js_1.process)(def.keyType, ctx, {
  460. ...params,
  461. path: [...params.path, "propertyNames"],
  462. });
  463. }
  464. json.additionalProperties = (0, to_json_schema_js_1.process)(def.valueType, ctx, {
  465. ...params,
  466. path: [...params.path, "additionalProperties"],
  467. });
  468. }
  469. // Add required for keys with discrete values (enum, literal, etc.)
  470. const keyValues = keyType._zod.values;
  471. if (keyValues) {
  472. const validKeyValues = [...keyValues].filter((v) => typeof v === "string" || typeof v === "number");
  473. if (validKeyValues.length > 0) {
  474. json.required = validKeyValues;
  475. }
  476. }
  477. };
  478. exports.recordProcessor = recordProcessor;
  479. const nullableProcessor = (schema, ctx, json, params) => {
  480. const def = schema._zod.def;
  481. const inner = (0, to_json_schema_js_1.process)(def.innerType, ctx, params);
  482. const seen = ctx.seen.get(schema);
  483. if (ctx.target === "openapi-3.0") {
  484. seen.ref = def.innerType;
  485. json.nullable = true;
  486. }
  487. else {
  488. json.anyOf = [inner, { type: "null" }];
  489. }
  490. };
  491. exports.nullableProcessor = nullableProcessor;
  492. const nonoptionalProcessor = (schema, ctx, _json, params) => {
  493. const def = schema._zod.def;
  494. (0, to_json_schema_js_1.process)(def.innerType, ctx, params);
  495. const seen = ctx.seen.get(schema);
  496. seen.ref = def.innerType;
  497. };
  498. exports.nonoptionalProcessor = nonoptionalProcessor;
  499. const defaultProcessor = (schema, ctx, json, params) => {
  500. const def = schema._zod.def;
  501. (0, to_json_schema_js_1.process)(def.innerType, ctx, params);
  502. const seen = ctx.seen.get(schema);
  503. seen.ref = def.innerType;
  504. json.default = JSON.parse(JSON.stringify(def.defaultValue));
  505. };
  506. exports.defaultProcessor = defaultProcessor;
  507. const prefaultProcessor = (schema, ctx, json, params) => {
  508. const def = schema._zod.def;
  509. (0, to_json_schema_js_1.process)(def.innerType, ctx, params);
  510. const seen = ctx.seen.get(schema);
  511. seen.ref = def.innerType;
  512. if (ctx.io === "input")
  513. json._prefault = JSON.parse(JSON.stringify(def.defaultValue));
  514. };
  515. exports.prefaultProcessor = prefaultProcessor;
  516. const catchProcessor = (schema, ctx, json, params) => {
  517. const def = schema._zod.def;
  518. (0, to_json_schema_js_1.process)(def.innerType, ctx, params);
  519. const seen = ctx.seen.get(schema);
  520. seen.ref = def.innerType;
  521. let catchValue;
  522. try {
  523. catchValue = def.catchValue(undefined);
  524. }
  525. catch {
  526. throw new Error("Dynamic catch values are not supported in JSON Schema");
  527. }
  528. json.default = catchValue;
  529. };
  530. exports.catchProcessor = catchProcessor;
  531. const pipeProcessor = (schema, ctx, _json, params) => {
  532. const def = schema._zod.def;
  533. const innerType = ctx.io === "input" ? (def.in._zod.def.type === "transform" ? def.out : def.in) : def.out;
  534. (0, to_json_schema_js_1.process)(innerType, ctx, params);
  535. const seen = ctx.seen.get(schema);
  536. seen.ref = innerType;
  537. };
  538. exports.pipeProcessor = pipeProcessor;
  539. const readonlyProcessor = (schema, ctx, json, params) => {
  540. const def = schema._zod.def;
  541. (0, to_json_schema_js_1.process)(def.innerType, ctx, params);
  542. const seen = ctx.seen.get(schema);
  543. seen.ref = def.innerType;
  544. json.readOnly = true;
  545. };
  546. exports.readonlyProcessor = readonlyProcessor;
  547. const promiseProcessor = (schema, ctx, _json, params) => {
  548. const def = schema._zod.def;
  549. (0, to_json_schema_js_1.process)(def.innerType, ctx, params);
  550. const seen = ctx.seen.get(schema);
  551. seen.ref = def.innerType;
  552. };
  553. exports.promiseProcessor = promiseProcessor;
  554. const optionalProcessor = (schema, ctx, _json, params) => {
  555. const def = schema._zod.def;
  556. (0, to_json_schema_js_1.process)(def.innerType, ctx, params);
  557. const seen = ctx.seen.get(schema);
  558. seen.ref = def.innerType;
  559. };
  560. exports.optionalProcessor = optionalProcessor;
  561. const lazyProcessor = (schema, ctx, _json, params) => {
  562. const innerType = schema._zod.innerType;
  563. (0, to_json_schema_js_1.process)(innerType, ctx, params);
  564. const seen = ctx.seen.get(schema);
  565. seen.ref = innerType;
  566. };
  567. exports.lazyProcessor = lazyProcessor;
  568. // ==================== ALL PROCESSORS ====================
  569. exports.allProcessors = {
  570. string: exports.stringProcessor,
  571. number: exports.numberProcessor,
  572. boolean: exports.booleanProcessor,
  573. bigint: exports.bigintProcessor,
  574. symbol: exports.symbolProcessor,
  575. null: exports.nullProcessor,
  576. undefined: exports.undefinedProcessor,
  577. void: exports.voidProcessor,
  578. never: exports.neverProcessor,
  579. any: exports.anyProcessor,
  580. unknown: exports.unknownProcessor,
  581. date: exports.dateProcessor,
  582. enum: exports.enumProcessor,
  583. literal: exports.literalProcessor,
  584. nan: exports.nanProcessor,
  585. template_literal: exports.templateLiteralProcessor,
  586. file: exports.fileProcessor,
  587. success: exports.successProcessor,
  588. custom: exports.customProcessor,
  589. function: exports.functionProcessor,
  590. transform: exports.transformProcessor,
  591. map: exports.mapProcessor,
  592. set: exports.setProcessor,
  593. array: exports.arrayProcessor,
  594. object: exports.objectProcessor,
  595. union: exports.unionProcessor,
  596. intersection: exports.intersectionProcessor,
  597. tuple: exports.tupleProcessor,
  598. record: exports.recordProcessor,
  599. nullable: exports.nullableProcessor,
  600. nonoptional: exports.nonoptionalProcessor,
  601. default: exports.defaultProcessor,
  602. prefault: exports.prefaultProcessor,
  603. catch: exports.catchProcessor,
  604. pipe: exports.pipeProcessor,
  605. readonly: exports.readonlyProcessor,
  606. promise: exports.promiseProcessor,
  607. optional: exports.optionalProcessor,
  608. lazy: exports.lazyProcessor,
  609. };
  610. function toJSONSchema(input, params) {
  611. if ("_idmap" in input) {
  612. // Registry case
  613. const registry = input;
  614. const ctx = (0, to_json_schema_js_1.initializeContext)({ ...params, processors: exports.allProcessors });
  615. const defs = {};
  616. // First pass: process all schemas to build the seen map
  617. for (const entry of registry._idmap.entries()) {
  618. const [_, schema] = entry;
  619. (0, to_json_schema_js_1.process)(schema, ctx);
  620. }
  621. const schemas = {};
  622. const external = {
  623. registry,
  624. uri: params?.uri,
  625. defs,
  626. };
  627. // Update the context with external configuration
  628. ctx.external = external;
  629. // Second pass: emit each schema
  630. for (const entry of registry._idmap.entries()) {
  631. const [key, schema] = entry;
  632. (0, to_json_schema_js_1.extractDefs)(ctx, schema);
  633. schemas[key] = (0, to_json_schema_js_1.finalize)(ctx, schema);
  634. }
  635. if (Object.keys(defs).length > 0) {
  636. const defsSegment = ctx.target === "draft-2020-12" ? "$defs" : "definitions";
  637. schemas.__shared = {
  638. [defsSegment]: defs,
  639. };
  640. }
  641. return { schemas };
  642. }
  643. // Single schema case
  644. const ctx = (0, to_json_schema_js_1.initializeContext)({ ...params, processors: exports.allProcessors });
  645. (0, to_json_schema_js_1.process)(input, ctx);
  646. (0, to_json_schema_js_1.extractDefs)(ctx, input);
  647. return (0, to_json_schema_js_1.finalize)(ctx, input);
  648. }