123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.zodToJsonSchema = void 0;
- const parseDef_js_1 = require("./parseDef.js");
- const Refs_js_1 = require("./Refs.js");
- const zodToJsonSchema = (schema, options) => {
- const refs = (0, Refs_js_1.getRefs)(options);
- const definitions = typeof options === "object" && options.definitions
- ? Object.entries(options.definitions).reduce((acc, [name, schema]) => ({
- ...acc,
- [name]: (0, parseDef_js_1.parseDef)(schema._def, {
- ...refs,
- currentPath: [...refs.basePath, refs.definitionPath, name],
- }, true) ?? {},
- }), {})
- : undefined;
- const name = typeof options === "string"
- ? options
- : options?.nameStrategy === "title"
- ? undefined
- : options?.name;
- const main = (0, parseDef_js_1.parseDef)(schema._def, name === undefined
- ? refs
- : {
- ...refs,
- currentPath: [...refs.basePath, refs.definitionPath, name],
- }, false) ?? {};
- const title = typeof options === "object" &&
- options.name !== undefined &&
- options.nameStrategy === "title"
- ? options.name
- : undefined;
- if (title !== undefined) {
- main.title = title;
- }
- const combined = name === undefined
- ? definitions
- ? {
- ...main,
- [refs.definitionPath]: definitions,
- }
- : main
- : {
- $ref: [
- ...(refs.$refStrategy === "relative" ? [] : refs.basePath),
- refs.definitionPath,
- name,
- ].join("/"),
- [refs.definitionPath]: {
- ...definitions,
- [name]: main,
- },
- };
- if (refs.target === "jsonSchema7") {
- combined.$schema = "http://json-schema.org/draft-07/schema#";
- }
- else if (refs.target === "jsonSchema2019-09" || refs.target === "openAi") {
- combined.$schema = "https://json-schema.org/draft/2019-09/schema#";
- }
- if (refs.target === "openAi" &&
- ("anyOf" in combined ||
- "oneOf" in combined ||
- "allOf" in combined ||
- ("type" in combined && Array.isArray(combined.type)))) {
- console.warn("Warning: OpenAI may not support schemas with unions as roots! Try wrapping it in an object property.");
- }
- return combined;
- };
- exports.zodToJsonSchema = zodToJsonSchema;
|