@mastra/schema-compat
Version:
Tool schema compatibility layer for Mastra.ai
181 lines (175 loc) • 6.26 kB
JavaScript
;
var chunkFJGZKUBH_cjs = require('./chunk-FJGZKUBH.cjs');
var chunkFS3P4V5M_cjs = require('./chunk-FS3P4V5M.cjs');
var chunk77T2HV4Q_cjs = require('./chunk-77T2HV4Q.cjs');
var chunkBQ3VTMIR_cjs = require('./chunk-BQ3VTMIR.cjs');
var z3 = require('zod/v3');
var v4 = require('zod/v4');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var z3__default = /*#__PURE__*/_interopDefault(z3);
var SUPPORTED_TARGETS = /* @__PURE__ */ new Set(["draft-07", "draft-04", "draft-2020-12"]);
var ZOD_V4_TARGET_MAP = {
"draft-07": "draft-7",
"draft-04": "draft-4"
};
function convertToJsonSchema(zodSchema, options, adapterOptions) {
chunkFJGZKUBH_cjs.patchRecordSchemas(zodSchema);
const target = SUPPORTED_TARGETS.has(options.target) ? options.target : "draft-07";
const jsonSchemaOptions = {
target: ZOD_V4_TARGET_MAP[target] ?? target
};
if (adapterOptions.unrepresentable) {
jsonSchemaOptions.unrepresentable = adapterOptions.unrepresentable;
}
if (adapterOptions.override) {
jsonSchemaOptions.override = adapterOptions.override;
}
return v4.toJSONSchema(zodSchema, jsonSchemaOptions);
}
function toStandardSchema4(zodSchema, adapterOptions = {}) {
const wrapper = Object.create(zodSchema);
const existingStandard = zodSchema["~standard"];
const jsonSchemaConverter = {
input: (options) => {
return convertToJsonSchema(zodSchema, options, adapterOptions);
},
output: (options) => {
return convertToJsonSchema(zodSchema, options, adapterOptions);
}
};
Object.defineProperty(wrapper, "~standard", {
value: {
...existingStandard,
jsonSchema: jsonSchemaConverter
},
writable: false,
enumerable: true,
configurable: false
});
return wrapper;
}
// src/standard-schema/standard-schema.ts
function jsonSchemaOverride(ctx) {
const zodSchema = ctx.zodSchema;
if (ctx.jsonSchema.type === "object" && ctx.jsonSchema.properties !== void 0 && !ctx.jsonSchema.additionalProperties) {
ctx.jsonSchema.additionalProperties = false;
}
if (zodSchema) {
const isDateType = zodSchema?.type === "date" || zodSchema?._def?.typeName === "ZodDate";
if (isDateType) {
if (zodSchema?.type === "date") {
ctx.jsonSchema.type = "string";
ctx.jsonSchema.format = "date-time";
}
ctx.jsonSchema["x-date"] = !zodSchema._zod?.def?.coerce;
} else if (zodSchema?.type === "object" && zodSchema._zod?.def?.catchall?.type === "unknown") {
ctx.jsonSchema.additionalProperties = true;
}
}
return void 0;
}
var JSON_SCHEMA_LIBRARY_OPTIONS = {
unrepresentable: "any",
override: jsonSchemaOverride
};
function isVercelSchema(schema) {
return typeof schema === "object" && schema !== null && "_type" in schema && "jsonSchema" in schema && typeof schema.jsonSchema === "object";
}
function isZodV4(schema) {
return typeof schema === "object" && schema !== null && "_zod" in schema;
}
function isZodV3(schema) {
if (schema === null || typeof schema !== "object") {
return false;
}
if (isZodV4(schema)) {
return false;
}
if ("~standard" in schema) {
const std = schema["~standard"];
if (typeof std === "object" && std !== null && std.vendor === "zod" && !("jsonSchema" in std)) {
return true;
}
}
return schema instanceof z3__default.default.ZodType;
}
function toStandardSchema5(schema) {
if (isZodV4(schema)) {
chunkFJGZKUBH_cjs.patchRecordSchemas(schema);
}
if (isStandardSchemaWithJSON(schema)) {
return schema;
}
if (isZodV4(schema)) {
return toStandardSchema4(schema, {
unrepresentable: JSON_SCHEMA_LIBRARY_OPTIONS.unrepresentable,
override: JSON_SCHEMA_LIBRARY_OPTIONS.override
});
}
if (isZodV3(schema)) {
return chunkBQ3VTMIR_cjs.toStandardSchema(schema);
}
if (isVercelSchema(schema)) {
return chunkFS3P4V5M_cjs.toStandardSchema(schema);
}
if (schema === null || typeof schema !== "object" && typeof schema !== "function") {
throw new Error(`Unsupported schema type: ${typeof schema}`);
}
if (typeof schema === "function") {
throw new Error(`Unsupported schema type: function (schema libraries should implement StandardSchemaWithJSON)`);
}
return chunk77T2HV4Q_cjs.toStandardSchema(schema);
}
function isStandardSchema(value) {
if (value === null || typeof value !== "object" && typeof value !== "function") {
return false;
}
if (!("~standard" in value)) {
return false;
}
const std = value["~standard"];
return typeof std === "object" && std !== null && "version" in std && std.version === 1 && "vendor" in std && "validate" in std && typeof std.validate === "function";
}
function isStandardJSONSchema(value) {
if (value === null || typeof value !== "object" && typeof value !== "function") {
return false;
}
if (!("~standard" in value)) {
return false;
}
const std = value["~standard"];
if (typeof std !== "object" || std === null) {
return false;
}
if (!("version" in std) || std.version !== 1 || !("vendor" in std)) {
return false;
}
if (!("jsonSchema" in std) || typeof std.jsonSchema !== "object") {
return false;
}
return typeof std.jsonSchema.input === "function" && typeof std.jsonSchema.output === "function";
}
function isStandardSchemaWithJSON(value) {
return isStandardSchema(value) && isStandardJSONSchema(value);
}
function standardSchemaToJSONSchema(schema, options = {}) {
const { target = "draft-07", io = "output", override = JSON_SCHEMA_LIBRARY_OPTIONS.override } = options;
const jsonSchemaFn = schema["~standard"].jsonSchema[io];
let jsonSchema = jsonSchemaFn({
target,
libraryOptions: {
...JSON_SCHEMA_LIBRARY_OPTIONS,
override
}
});
jsonSchema = JSON.parse(JSON.stringify(jsonSchema));
return jsonSchema;
}
exports.JSON_SCHEMA_LIBRARY_OPTIONS = JSON_SCHEMA_LIBRARY_OPTIONS;
exports.isStandardJSONSchema = isStandardJSONSchema;
exports.isStandardSchema = isStandardSchema;
exports.isStandardSchemaWithJSON = isStandardSchemaWithJSON;
exports.standardSchemaToJSONSchema = standardSchemaToJSONSchema;
exports.toStandardSchema = toStandardSchema5;
//# sourceMappingURL=chunk-ZTO66ALH.cjs.map
//# sourceMappingURL=chunk-ZTO66ALH.cjs.map