@mastra/schema-compat
Version:
Tool schema compatibility layer for Mastra.ai
170 lines (167 loc) • 5.89 kB
JavaScript
import { patchRecordSchemas } from './chunk-QEM5W7OR.js';
import { toStandardSchema as toStandardSchema$1 } from './chunk-23HDOZLF.js';
import { toStandardSchema as toStandardSchema$2 } from './chunk-RMMKMUYK.js';
import { toStandardSchema } from './chunk-QFEMCRFS.js';
import z3 from 'zod/v3';
import { toJSONSchema } from 'zod/v4';
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) {
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 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.ZodType;
}
function toStandardSchema5(schema) {
if (isZodV4(schema)) {
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 toStandardSchema(schema);
}
if (isVercelSchema(schema)) {
return toStandardSchema$1(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 toStandardSchema$2(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;
}
export { JSON_SCHEMA_LIBRARY_OPTIONS, isStandardJSONSchema, isStandardSchema, isStandardSchemaWithJSON, standardSchemaToJSONSchema, toStandardSchema5 as toStandardSchema };
//# sourceMappingURL=chunk-VNIPWNNH.js.map
//# sourceMappingURL=chunk-VNIPWNNH.js.map