zodToJsonSchema.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.zodToJsonSchema = void 0;
  4. const parseDef_1 = require("./parseDef.js");
  5. const Refs_1 = require("./Refs.js");
  6. const util_1 = require("./util.js");
  7. const zodToJsonSchema = (schema, options) => {
  8. const refs = (0, Refs_1.getRefs)(options);
  9. const name = typeof options === 'string' ? options
  10. : options?.nameStrategy === 'title' ? undefined
  11. : options?.name;
  12. const main = (0, parseDef_1.parseDef)(schema._def, name === undefined ? refs : ({
  13. ...refs,
  14. currentPath: [...refs.basePath, refs.definitionPath, name],
  15. }), false) ?? {};
  16. const title = typeof options === 'object' && options.name !== undefined && options.nameStrategy === 'title' ?
  17. options.name
  18. : undefined;
  19. if (title !== undefined) {
  20. main.title = title;
  21. }
  22. const definitions = (() => {
  23. if ((0, util_1.isEmptyObj)(refs.definitions)) {
  24. return undefined;
  25. }
  26. const definitions = {};
  27. const processedDefinitions = new Set();
  28. // the call to `parseDef()` here might itself add more entries to `.definitions`
  29. // so we need to continually evaluate definitions until we've resolved all of them
  30. //
  31. // we have a generous iteration limit here to avoid blowing up the stack if there
  32. // are any bugs that would otherwise result in us iterating indefinitely
  33. for (let i = 0; i < 500; i++) {
  34. const newDefinitions = Object.entries(refs.definitions).filter(([key]) => !processedDefinitions.has(key));
  35. if (newDefinitions.length === 0)
  36. break;
  37. for (const [key, schema] of newDefinitions) {
  38. definitions[key] =
  39. (0, parseDef_1.parseDef)((0, util_1.zodDef)(schema), { ...refs, currentPath: [...refs.basePath, refs.definitionPath, key] }, true) ?? {};
  40. processedDefinitions.add(key);
  41. }
  42. }
  43. return definitions;
  44. })();
  45. const combined = name === undefined ?
  46. definitions ?
  47. {
  48. ...main,
  49. [refs.definitionPath]: definitions,
  50. }
  51. : main
  52. : refs.nameStrategy === 'duplicate-ref' ?
  53. {
  54. ...main,
  55. ...(definitions || refs.seenRefs.size ?
  56. {
  57. [refs.definitionPath]: {
  58. ...definitions,
  59. // only actually duplicate the schema definition if it was ever referenced
  60. // otherwise the duplication is completely pointless
  61. ...(refs.seenRefs.size ? { [name]: main } : undefined),
  62. },
  63. }
  64. : undefined),
  65. }
  66. : {
  67. $ref: [...(refs.$refStrategy === 'relative' ? [] : refs.basePath), refs.definitionPath, name].join('/'),
  68. [refs.definitionPath]: {
  69. ...definitions,
  70. [name]: main,
  71. },
  72. };
  73. if (refs.target === 'jsonSchema7') {
  74. combined.$schema = 'http://json-schema.org/draft-07/schema#';
  75. }
  76. else if (refs.target === 'jsonSchema2019-09') {
  77. combined.$schema = 'https://json-schema.org/draft/2019-09/schema#';
  78. }
  79. return combined;
  80. };
  81. exports.zodToJsonSchema = zodToJsonSchema;
  82. //# sourceMappingURL=zodToJsonSchema.js.map