jorel
Version:
A unified wrapper for working with LLMs from multiple providers, including streams, images, documents & automatic tool use.
26 lines (25 loc) • 1.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.zodSchemaToJsonSchema = void 0;
const zod_to_json_schema_1 = require("zod-to-json-schema");
const object_utils_1 = require("./object-utils");
const zodSchemaToJsonSchema = (zod, target = "jsonSchema7") => {
return deepClean((0, zod_to_json_schema_1.zodToJsonSchema)(zod, {
$refStrategy: "none",
target,
}), target === "openAi" ? ["$schema"] : ["$schema", "additionalProperties"]);
};
exports.zodSchemaToJsonSchema = zodSchemaToJsonSchema;
function deepClean(schema, keysToRemove) {
if (Array.isArray(schema)) {
return schema.map((item) => deepClean(item, keysToRemove));
}
else if (typeof schema === "object" && schema !== null) {
const cleaned = (0, object_utils_1.omit)(schema, keysToRemove);
for (const key of Object.keys(cleaned)) {
cleaned[key] = deepClean(cleaned[key], keysToRemove);
}
return cleaned;
}
return schema;
}