zodToJsonSchema.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.zodToJsonSchema = void 0;
  4. const parseDef_js_1 = require("./parseDef.js");
  5. const Refs_js_1 = require("./Refs.js");
  6. const any_js_1 = require("./parsers/any.js");
  7. const zodToJsonSchema = (schema, options) => {
  8. const refs = (0, Refs_js_1.getRefs)(options);
  9. let definitions = typeof options === "object" && options.definitions
  10. ? Object.entries(options.definitions).reduce((acc, [name, schema]) => ({
  11. ...acc,
  12. [name]: (0, parseDef_js_1.parseDef)(schema._def, {
  13. ...refs,
  14. currentPath: [...refs.basePath, refs.definitionPath, name],
  15. }, true) ?? (0, any_js_1.parseAnyDef)(refs),
  16. }), {})
  17. : undefined;
  18. const name = typeof options === "string"
  19. ? options
  20. : options?.nameStrategy === "title"
  21. ? undefined
  22. : options?.name;
  23. const main = (0, parseDef_js_1.parseDef)(schema._def, name === undefined
  24. ? refs
  25. : {
  26. ...refs,
  27. currentPath: [...refs.basePath, refs.definitionPath, name],
  28. }, false) ?? (0, any_js_1.parseAnyDef)(refs);
  29. const title = typeof options === "object" &&
  30. options.name !== undefined &&
  31. options.nameStrategy === "title"
  32. ? options.name
  33. : undefined;
  34. if (title !== undefined) {
  35. main.title = title;
  36. }
  37. if (refs.flags.hasReferencedOpenAiAnyType) {
  38. if (!definitions) {
  39. definitions = {};
  40. }
  41. if (!definitions[refs.openAiAnyTypeName]) {
  42. definitions[refs.openAiAnyTypeName] = {
  43. // Skipping "object" as no properties can be defined and additionalProperties must be "false"
  44. type: ["string", "number", "integer", "boolean", "array", "null"],
  45. items: {
  46. $ref: refs.$refStrategy === "relative"
  47. ? "1"
  48. : [
  49. ...refs.basePath,
  50. refs.definitionPath,
  51. refs.openAiAnyTypeName,
  52. ].join("/"),
  53. },
  54. };
  55. }
  56. }
  57. const combined = name === undefined
  58. ? definitions
  59. ? {
  60. ...main,
  61. [refs.definitionPath]: definitions,
  62. }
  63. : main
  64. : {
  65. $ref: [
  66. ...(refs.$refStrategy === "relative" ? [] : refs.basePath),
  67. refs.definitionPath,
  68. name,
  69. ].join("/"),
  70. [refs.definitionPath]: {
  71. ...definitions,
  72. [name]: main,
  73. },
  74. };
  75. if (refs.target === "jsonSchema7") {
  76. combined.$schema = "http://json-schema.org/draft-07/schema#";
  77. }
  78. else if (refs.target === "jsonSchema2019-09" || refs.target === "openAi") {
  79. combined.$schema = "https://json-schema.org/draft/2019-09/schema#";
  80. }
  81. if (refs.target === "openAi" &&
  82. ("anyOf" in combined ||
  83. "oneOf" in combined ||
  84. "allOf" in combined ||
  85. ("type" in combined && Array.isArray(combined.type)))) {
  86. console.warn("Warning: OpenAI may not support schemas with unions as roots! Try wrapping it in an object property.");
  87. }
  88. return combined;
  89. };
  90. exports.zodToJsonSchema = zodToJsonSchema;