@mastra/schema-compat
Version:
Tool schema compatibility layer for Mastra.ai
100 lines (99 loc) • 3.26 kB
JavaScript
import { r as __toESM, t as require_json_schema_traverse } from "../../json-schema-traverse-B35mHRM8.js";
import zodToJsonSchemaOriginal, { ignoreOverride } from "zod-to-json-schema";
//#region src/standard-schema/adapters/zod-v3.ts
var import_json_schema_traverse = /* @__PURE__ */ __toESM(require_json_schema_traverse(), 1);
/**
* Target mapping from Standard Schema targets to zod-to-json-schema targets.
*/
const TARGET_MAP = {
"draft-04": "jsonSchema7",
"draft-07": "jsonSchema7",
"draft-2020-12": "jsonSchema2019-09",
"openapi-3.0": "openApi3"
};
/**
* Converts a Zod schema to JSON Schema using the specified target format.
*
* @param zodSchema - The Zod schema to convert
* @param options - Standard Schema JSON options including the target format
* @returns The JSON Schema representation
* @throws Error if the target format is not supported
*
* @internal
*/
function convertToJsonSchema(zodSchema, options) {
const target = TARGET_MAP[options.target];
if (!target) {
const supportedTargets = Object.keys(TARGET_MAP);
throw new Error(`Unsupported JSON Schema target: "${options.target}". Supported targets are: ${supportedTargets.join(", ")}`);
}
const jsonSchema = zodToJsonSchemaOriginal(zodSchema, {
$refStrategy: "none",
target,
override: (def) => {
if (def.typeName === "ZodDate") return {
type: "string",
format: "date-time",
"x-date": true
};
return ignoreOverride;
}
});
(0, import_json_schema_traverse.default)(jsonSchema, { cb: { pre: (schema) => {
if (schema.type === "string" && schema.format === "email") schema.pattern = `^(?!\\.)(?!.*\\.\\.)([A-Za-z0-9_'+\\-\\.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$`;
} } });
return jsonSchema;
}
/**
* Wraps a Zod v3 schema to implement the full @standard-schema/spec interface.
*
* While Zod v3 natively implements `StandardSchemaV1` (validation), it does not
* implement `StandardJSONSchemaV1` (JSON Schema conversion). This adapter adds
* the `jsonSchema` property to provide JSON Schema conversion capabilities.
*
* @typeParam T - The Zod schema type
*
* @example
* ```typescript
* import { z } from 'zod';
* import { toStandardSchema } from '@mastra/schema-compat/adapters/zod-v3';
*
* const userSchema = z.object({
* name: z.string(),
* age: z.number().min(0),
* });
*
* const standardSchema = toStandardSchema(userSchema);
*
* // Use validation (from StandardSchemaV1)
* const result = standardSchema['~standard'].validate({ name: 'John', age: 30 });
*
* // Get JSON Schema (from StandardJSONSchemaV1)
* const jsonSchema = standardSchema['~standard'].jsonSchema.output({ target: 'draft-07' });
* ```
*/
function toStandardSchema(zodSchema) {
const wrapper = Object.create(zodSchema);
const existingStandard = zodSchema["~standard"];
const jsonSchemaConverter = {
input: (options) => {
return convertToJsonSchema(zodSchema, options);
},
output: (options) => {
return convertToJsonSchema(zodSchema, options);
}
};
Object.defineProperty(wrapper, "~standard", {
value: {
...existingStandard,
jsonSchema: jsonSchemaConverter
},
writable: false,
enumerable: true,
configurable: false
});
return wrapper;
}
//#endregion
export { toStandardSchema };
//# sourceMappingURL=zod-v3.js.map