@mastra/schema-compat 1.3.4-alpha.2 → 1.3.5-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/dist/_types/@internal_ai-sdk-v5/dist/index.d.ts +4 -4
- package/dist/_types/@internal_ai-v6/dist/index.d.ts +10 -6
- package/dist/index.cjs +5455 -5998
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +5467 -5988
- package/dist/index.js.map +1 -1
- package/dist/json-schema-BstKYbJi.js +6797 -0
- package/dist/json-schema-BstKYbJi.js.map +1 -0
- package/dist/json-schema-pcL1TeZs.cjs +6809 -0
- package/dist/json-schema-pcL1TeZs.cjs.map +1 -0
- package/dist/json-schema-traverse-B35mHRM8.js +101 -0
- package/dist/json-schema-traverse-B35mHRM8.js.map +1 -0
- package/dist/json-schema-traverse-CSNSaf0y.cjs +83 -0
- package/dist/json-schema-traverse-CSNSaf0y.cjs.map +1 -0
- package/dist/json-to-zod.cjs +185 -245
- package/dist/json-to-zod.cjs.map +1 -1
- package/dist/json-to-zod.js +176 -235
- package/dist/json-to-zod.js.map +1 -1
- package/dist/provider-compats/anthropic.d.ts +1 -0
- package/dist/provider-compats/anthropic.d.ts.map +1 -1
- package/dist/rolldown-runtime-emK7D4bc.cjs +35 -0
- package/dist/schema-Bv9OdW1f.js +258 -0
- package/dist/schema-Bv9OdW1f.js.map +1 -0
- package/dist/schema-Cjk6OMG9.cjs +295 -0
- package/dist/schema-Cjk6OMG9.cjs.map +1 -0
- package/dist/schema.cjs +8 -32
- package/dist/schema.js +2 -3
- package/dist/standard-schema/adapters/ai-sdk.cjs +156 -14
- package/dist/standard-schema/adapters/ai-sdk.cjs.map +1 -1
- package/dist/standard-schema/adapters/ai-sdk.js +155 -2
- package/dist/standard-schema/adapters/ai-sdk.js.map +1 -1
- package/dist/standard-schema/adapters/json-schema.cjs +4 -16
- package/dist/standard-schema/adapters/json-schema.js +2 -3
- package/dist/standard-schema/adapters/zod-v3.cjs +101 -10
- package/dist/standard-schema/adapters/zod-v3.cjs.map +1 -1
- package/dist/standard-schema/adapters/zod-v3.js +99 -2
- package/dist/standard-schema/adapters/zod-v3.js.map +1 -1
- package/dist/zod-to-json.cjs +165 -22
- package/dist/zod-to-json.cjs.map +1 -1
- package/dist/zod-to-json.js +160 -2
- package/dist/zod-to-json.js.map +1 -1
- package/package.json +18 -17
- package/dist/chunk-23HDOZLF.js +0 -105
- package/dist/chunk-23HDOZLF.js.map +0 -1
- package/dist/chunk-5WRI5ZAA.js +0 -29
- package/dist/chunk-5WRI5ZAA.js.map +0 -1
- package/dist/chunk-77T2HV4Q.cjs +0 -7352
- package/dist/chunk-77T2HV4Q.cjs.map +0 -1
- package/dist/chunk-BQ3VTMIR.cjs +0 -76
- package/dist/chunk-BQ3VTMIR.cjs.map +0 -1
- package/dist/chunk-DZUJEN5N.cjs +0 -32
- package/dist/chunk-DZUJEN5N.cjs.map +0 -1
- package/dist/chunk-FJGZKUBH.cjs +0 -208
- package/dist/chunk-FJGZKUBH.cjs.map +0 -1
- package/dist/chunk-FS3P4V5M.cjs +0 -108
- package/dist/chunk-FS3P4V5M.cjs.map +0 -1
- package/dist/chunk-QEM5W7OR.js +0 -199
- package/dist/chunk-QEM5W7OR.js.map +0 -1
- package/dist/chunk-QFEMCRFS.js +0 -70
- package/dist/chunk-QFEMCRFS.js.map +0 -1
- package/dist/chunk-RMMKMUYK.js +0 -7349
- package/dist/chunk-RMMKMUYK.js.map +0 -1
- package/dist/chunk-SKPYVQBT.cjs +0 -94
- package/dist/chunk-SKPYVQBT.cjs.map +0 -1
- package/dist/chunk-VNIPWNNH.js +0 -170
- package/dist/chunk-VNIPWNNH.js.map +0 -1
- package/dist/chunk-Y4VRRNL5.js +0 -92
- package/dist/chunk-Y4VRRNL5.js.map +0 -1
- package/dist/chunk-ZTO66ALH.cjs +0 -181
- package/dist/chunk-ZTO66ALH.cjs.map +0 -1
- package/dist/schema.cjs.map +0 -1
- package/dist/schema.js.map +0 -1
- package/dist/standard-schema/adapters/json-schema.cjs.map +0 -1
- package/dist/standard-schema/adapters/json-schema.js.map +0 -1
package/dist/chunk-BQ3VTMIR.cjs
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var chunkSKPYVQBT_cjs = require('./chunk-SKPYVQBT.cjs');
|
|
4
|
-
var chunkDZUJEN5N_cjs = require('./chunk-DZUJEN5N.cjs');
|
|
5
|
-
var zodToJsonSchemaOriginal = require('zod-to-json-schema');
|
|
6
|
-
|
|
7
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
-
|
|
9
|
-
var zodToJsonSchemaOriginal__default = /*#__PURE__*/_interopDefault(zodToJsonSchemaOriginal);
|
|
10
|
-
|
|
11
|
-
// src/standard-schema/adapters/zod-v3.ts
|
|
12
|
-
var import_json_schema_traverse = chunkDZUJEN5N_cjs.__toESM(chunkSKPYVQBT_cjs.require_json_schema_traverse(), 1);
|
|
13
|
-
var TARGET_MAP = {
|
|
14
|
-
"draft-04": "jsonSchema7",
|
|
15
|
-
"draft-07": "jsonSchema7",
|
|
16
|
-
"draft-2020-12": "jsonSchema2019-09",
|
|
17
|
-
"openapi-3.0": "openApi3"
|
|
18
|
-
};
|
|
19
|
-
function convertToJsonSchema(zodSchema, options) {
|
|
20
|
-
const target = TARGET_MAP[options.target];
|
|
21
|
-
if (!target) {
|
|
22
|
-
const supportedTargets = Object.keys(TARGET_MAP);
|
|
23
|
-
throw new Error(
|
|
24
|
-
`Unsupported JSON Schema target: "${options.target}". Supported targets are: ${supportedTargets.join(", ")}`
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
const jsonSchema = zodToJsonSchemaOriginal__default.default(zodSchema, {
|
|
28
|
-
$refStrategy: "none",
|
|
29
|
-
target,
|
|
30
|
-
override: (def) => {
|
|
31
|
-
if (def.typeName === "ZodDate") {
|
|
32
|
-
return { type: "string", format: "date-time", "x-date": true };
|
|
33
|
-
}
|
|
34
|
-
return zodToJsonSchemaOriginal.ignoreOverride;
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
(0, import_json_schema_traverse.default)(jsonSchema, {
|
|
38
|
-
cb: {
|
|
39
|
-
pre: (schema) => {
|
|
40
|
-
if (schema.type === "string" && schema.format === "email") {
|
|
41
|
-
schema.pattern = `^(?!\\.)(?!.*\\.\\.)([A-Za-z0-9_'+\\-\\.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$`;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
// post: (schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema) => {
|
|
45
|
-
// this.postProcessJSONNode(schema, parentSchema);
|
|
46
|
-
// },
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
return jsonSchema;
|
|
50
|
-
}
|
|
51
|
-
function toStandardSchema(zodSchema) {
|
|
52
|
-
const wrapper = Object.create(zodSchema);
|
|
53
|
-
const existingStandard = zodSchema["~standard"];
|
|
54
|
-
const jsonSchemaConverter = {
|
|
55
|
-
input: (options) => {
|
|
56
|
-
return convertToJsonSchema(zodSchema, options);
|
|
57
|
-
},
|
|
58
|
-
output: (options) => {
|
|
59
|
-
return convertToJsonSchema(zodSchema, options);
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
Object.defineProperty(wrapper, "~standard", {
|
|
63
|
-
value: {
|
|
64
|
-
...existingStandard,
|
|
65
|
-
jsonSchema: jsonSchemaConverter
|
|
66
|
-
},
|
|
67
|
-
writable: false,
|
|
68
|
-
enumerable: true,
|
|
69
|
-
configurable: false
|
|
70
|
-
});
|
|
71
|
-
return wrapper;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
exports.toStandardSchema = toStandardSchema;
|
|
75
|
-
//# sourceMappingURL=chunk-BQ3VTMIR.cjs.map
|
|
76
|
-
//# sourceMappingURL=chunk-BQ3VTMIR.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/standard-schema/adapters/zod-v3.ts"],"names":["__toESM","require_json_schema_traverse","zodToJsonSchemaOriginal","ignoreOverride","traverse"],"mappings":";;;;;;;;;;;AACA,IAAA,2BAAA,GAAqBA,yBAAA,CAAAC,8CAAA,EAAA,EAAA,CAAA,CAAA;AAYrB,IAAM,UAAA,GAAoD;AAAA,EACxD,UAAA,EAAY,aAAA;AAAA,EACZ,UAAA,EAAY,aAAA;AAAA,EACZ,eAAA,EAAiB,mBAAA;AAAA,EACjB,aAAA,EAAe;AACjB,CAAA;AAYA,SAAS,mBAAA,CACP,WACA,OAAA,EACyB;AACzB,EAAA,MAAM,MAAA,GAAS,UAAA,CAAW,OAAA,CAAQ,MAAM,CAAA;AAExC,EAAA,IAAI,CAAC,MAAA,EAAQ;AAEX,IAAA,MAAM,gBAAA,GAAmB,MAAA,CAAO,IAAA,CAAK,UAAU,CAAA;AAC/C,IAAA,MAAM,IAAI,KAAA;AAAA,MACR,oCAAoC,OAAA,CAAQ,MAAM,6BAAkC,gBAAA,CAAiB,IAAA,CAAK,IAAI,CAAC,CAAA;AAAA,KACjH;AAAA,EACF;AAEA,EAAA,MAAM,UAAA,GAAaC,yCAAwB,SAAA,EAAW;AAAA,IACpD,YAAA,EAAc,MAAA;AAAA,IACd,MAAA;AAAA,IACA,QAAA,EAAU,CAAC,GAAA,KAAa;AAGtB,MAAA,IAAI,GAAA,CAAI,aAAa,SAAA,EAAW;AAC9B,QAAA,OAAO,EAAE,IAAA,EAAM,QAAA,EAAU,MAAA,EAAQ,WAAA,EAAa,UAAU,IAAA,EAAK;AAAA,MAC/D;AACA,MAAA,OAAOC,sCAAA;AAAA,IACT;AAAA,GACD,CAAA;AAED,EAAA,IAAA,2BAAA,CAAAC,SAAS,UAAA,EAAY;AAAA,IACnB,EAAA,EAAI;AAAA,MACF,KAAK,CAAA,MAAA,KAAU;AACb,QAAA,IAAI,MAAA,CAAO,IAAA,KAAS,QAAA,IAAY,MAAA,CAAO,WAAW,OAAA,EAAS;AACzD,UAAA,MAAA,CAAO,OAAA,GAAU,CAAA,uGAAA,CAAA;AAAA,QACnB;AAAA,MACF;AAAA;AAAA;AAAA;AAAA;AAIF,GACD,CAAA;AAED,EAAA,OAAO,UAAA;AACT;AA8BO,SAAS,iBAAoB,SAAA,EAAwE;AAE1G,EAAA,MAAM,OAAA,GAAU,MAAA,CAAO,MAAA,CAAO,SAAS,CAAA;AAGvC,EAAA,MAAM,gBAAA,GAAmB,UAAU,WAAW,CAAA;AAG9C,EAAA,MAAM,mBAAA,GAAsD;AAAA,IAC1D,KAAA,EAAO,CAAC,OAAA,KAAmE;AACzE,MAAA,OAAO,mBAAA,CAAoB,WAAW,OAAO,CAAA;AAAA,IAC/C,CAAA;AAAA,IACA,MAAA,EAAQ,CAAC,OAAA,KAAmE;AAG1E,MAAA,OAAO,mBAAA,CAAoB,WAAW,OAAO,CAAA;AAAA,IAC/C;AAAA,GACF;AAGA,EAAA,MAAA,CAAO,cAAA,CAAe,SAAS,WAAA,EAAa;AAAA,IAC1C,KAAA,EAAO;AAAA,MACL,GAAG,gBAAA;AAAA,MACH,UAAA,EAAY;AAAA,KACd;AAAA,IACA,QAAA,EAAU,KAAA;AAAA,IACV,UAAA,EAAY,IAAA;AAAA,IACZ,YAAA,EAAc;AAAA,GACf,CAAA;AAED,EAAA,OAAO,OAAA;AACT","file":"chunk-BQ3VTMIR.cjs","sourcesContent":["import type { StandardSchemaV1, StandardJSONSchemaV1 } from '@standard-schema/spec';\nimport traverse from 'json-schema-traverse';\n// Use zod/v3 types for v3 compatibility (v4's v3-compat layer)\nimport type { ZodType, ZodTypeDef } from 'zod/v3';\nimport zodToJsonSchemaOriginal, { ignoreOverride } from 'zod-to-json-schema';\nimport type {\n StandardSchemaWithJSON,\n StandardSchemaWithJSONProps,\n ZodToJsonSchemaTarget,\n} from '../standard-schema.types';\n/**\n * Target mapping from Standard Schema targets to zod-to-json-schema targets.\n */\nconst TARGET_MAP: Record<string, ZodToJsonSchemaTarget> = {\n 'draft-04': 'jsonSchema7',\n 'draft-07': 'jsonSchema7',\n 'draft-2020-12': 'jsonSchema2019-09',\n 'openapi-3.0': 'openApi3',\n};\n\n/**\n * Converts a Zod schema to JSON Schema using the specified target format.\n *\n * @param zodSchema - The Zod schema to convert\n * @param options - Standard Schema JSON options including the target format\n * @returns The JSON Schema representation\n * @throws Error if the target format is not supported\n *\n * @internal\n */\nfunction convertToJsonSchema<T extends ZodType<any, ZodTypeDef, any>>(\n zodSchema: T,\n options: StandardJSONSchemaV1.Options,\n): Record<string, unknown> {\n const target = TARGET_MAP[options.target];\n\n if (!target) {\n // For unknown targets, try to use jsonSchema7 as fallback or throw\n const supportedTargets = Object.keys(TARGET_MAP);\n throw new Error(\n `Unsupported JSON Schema target: \"${options.target}\". ` + `Supported targets are: ${supportedTargets.join(', ')}`,\n );\n }\n\n const jsonSchema = zodToJsonSchemaOriginal(zodSchema, {\n $refStrategy: 'none',\n target,\n override: (def: any) => {\n // Mark z.date() with x-date for downstream string→Date conversion.\n // Zod v3 has no z.coerce.date(), so all dates are strict.\n if (def.typeName === 'ZodDate') {\n return { type: 'string', format: 'date-time', 'x-date': true };\n }\n return ignoreOverride;\n },\n });\n\n traverse(jsonSchema, {\n cb: {\n pre: schema => {\n if (schema.type === 'string' && schema.format === 'email') {\n schema.pattern = `^(?!\\\\.)(?!.*\\\\.\\\\.)([A-Za-z0-9_'+\\\\-\\\\.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9\\\\-]*\\\\.)+[A-Za-z]{2,}$`;\n }\n },\n // post: (schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema) => {\n // this.postProcessJSONNode(schema, parentSchema);\n // },\n },\n });\n\n return jsonSchema as Record<string, unknown>;\n}\n\n/**\n * Wraps a Zod v3 schema to implement the full @standard-schema/spec interface.\n *\n * While Zod v3 natively implements `StandardSchemaV1` (validation), it does not\n * implement `StandardJSONSchemaV1` (JSON Schema conversion). This adapter adds\n * the `jsonSchema` property to provide JSON Schema conversion capabilities.\n *\n * @typeParam T - The Zod schema type\n *\n * @example\n * ```typescript\n * import { z } from 'zod';\n * import { toStandardSchema } from '@mastra/schema-compat/adapters/zod-v3';\n *\n * const userSchema = z.object({\n * name: z.string(),\n * age: z.number().min(0),\n * });\n *\n * const standardSchema = toStandardSchema(userSchema);\n *\n * // Use validation (from StandardSchemaV1)\n * const result = standardSchema['~standard'].validate({ name: 'John', age: 30 });\n *\n * // Get JSON Schema (from StandardJSONSchemaV1)\n * const jsonSchema = standardSchema['~standard'].jsonSchema.output({ target: 'draft-07' });\n * ```\n */\nexport function toStandardSchema<T>(zodSchema: ZodType<T, ZodTypeDef, T>): T & StandardSchemaWithJSON<T, T> {\n // Create a wrapper object that includes the jsonSchema converter\n const wrapper = Object.create(zodSchema) as T & StandardSchemaWithJSON<T, T>;\n\n // Get the existing ~standard property from Zod\n const existingStandard = zodSchema['~standard'] as StandardSchemaV1.Props<T, T>;\n\n // Create the JSON Schema converter\n const jsonSchemaConverter: StandardJSONSchemaV1.Converter = {\n input: (options: StandardJSONSchemaV1.Options): Record<string, unknown> => {\n return convertToJsonSchema(zodSchema, options);\n },\n output: (options: StandardJSONSchemaV1.Options): Record<string, unknown> => {\n // For Zod schemas, input and output JSON Schema are typically the same\n // unless using transforms, which would need special handling\n return convertToJsonSchema(zodSchema, options);\n },\n };\n\n // Define the enhanced ~standard property\n Object.defineProperty(wrapper, '~standard', {\n value: {\n ...existingStandard,\n jsonSchema: jsonSchemaConverter,\n } satisfies StandardSchemaWithJSONProps<T, T>,\n writable: false,\n enumerable: true,\n configurable: false,\n });\n\n return wrapper;\n}\n"]}
|
package/dist/chunk-DZUJEN5N.cjs
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var __create = Object.create;
|
|
4
|
-
var __defProp = Object.defineProperty;
|
|
5
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
-
var __commonJS = (cb, mod) => function __require() {
|
|
10
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
|
|
29
|
-
exports.__commonJS = __commonJS;
|
|
30
|
-
exports.__toESM = __toESM;
|
|
31
|
-
//# sourceMappingURL=chunk-DZUJEN5N.cjs.map
|
|
32
|
-
//# sourceMappingURL=chunk-DZUJEN5N.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"names":[],"mappings":"","file":"chunk-DZUJEN5N.cjs"}
|
package/dist/chunk-FJGZKUBH.cjs
DELETED
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var v4 = require('zod/v4');
|
|
4
|
-
var zodToJsonSchemaOriginal = require('zod-to-json-schema');
|
|
5
|
-
|
|
6
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
7
|
-
|
|
8
|
-
var zodToJsonSchemaOriginal__default = /*#__PURE__*/_interopDefault(zodToJsonSchemaOriginal);
|
|
9
|
-
|
|
10
|
-
// src/zod-to-json.ts
|
|
11
|
-
var PATCHED = /* @__PURE__ */ Symbol("__mastra_patched__");
|
|
12
|
-
function patchRecordSchemas(schema) {
|
|
13
|
-
if (!schema || typeof schema !== "object") return schema;
|
|
14
|
-
if (schema[PATCHED]) return schema;
|
|
15
|
-
schema[PATCHED] = true;
|
|
16
|
-
const def = schema._zod?.def;
|
|
17
|
-
if (def?.type === "record" && def.keyType && !def.valueType) {
|
|
18
|
-
def.valueType = def.keyType;
|
|
19
|
-
def.keyType = v4.z.string();
|
|
20
|
-
}
|
|
21
|
-
if (!def) return schema;
|
|
22
|
-
if (def.type === "object" && def.shape) {
|
|
23
|
-
const shape = typeof def.shape === "function" ? def.shape() : def.shape;
|
|
24
|
-
for (const key of Object.keys(shape)) {
|
|
25
|
-
patchRecordSchemas(shape[key]);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
if (def.type === "array" && def.element) {
|
|
29
|
-
patchRecordSchemas(def.element);
|
|
30
|
-
}
|
|
31
|
-
if (def.type === "union" && def.options) {
|
|
32
|
-
def.options.forEach(patchRecordSchemas);
|
|
33
|
-
}
|
|
34
|
-
if (def.type === "record") {
|
|
35
|
-
if (def.keyType) patchRecordSchemas(def.keyType);
|
|
36
|
-
if (def.valueType) patchRecordSchemas(def.valueType);
|
|
37
|
-
}
|
|
38
|
-
if (def.type === "intersection") {
|
|
39
|
-
if (def.left) patchRecordSchemas(def.left);
|
|
40
|
-
if (def.right) patchRecordSchemas(def.right);
|
|
41
|
-
}
|
|
42
|
-
if (def.type === "lazy") {
|
|
43
|
-
if (def.getter && typeof def.getter === "function") {
|
|
44
|
-
const originalGetter = def.getter;
|
|
45
|
-
def.getter = function() {
|
|
46
|
-
const innerSchema = originalGetter();
|
|
47
|
-
if (innerSchema) {
|
|
48
|
-
patchRecordSchemas(innerSchema);
|
|
49
|
-
}
|
|
50
|
-
return innerSchema;
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
if (def.innerType) {
|
|
55
|
-
patchRecordSchemas(def.innerType);
|
|
56
|
-
}
|
|
57
|
-
return schema;
|
|
58
|
-
}
|
|
59
|
-
function fixAnyOfNullable(schema) {
|
|
60
|
-
if (typeof schema !== "object" || schema === null) {
|
|
61
|
-
return schema;
|
|
62
|
-
}
|
|
63
|
-
const result = { ...schema };
|
|
64
|
-
if (result.anyOf && Array.isArray(result.anyOf) && result.anyOf.length === 2) {
|
|
65
|
-
const nullSchema = result.anyOf.find((s) => typeof s === "object" && s !== null && s.type === "null");
|
|
66
|
-
const otherSchema = result.anyOf.find((s) => typeof s === "object" && s !== null && s.type !== "null");
|
|
67
|
-
if (nullSchema && otherSchema && typeof otherSchema === "object" && otherSchema.type) {
|
|
68
|
-
const { anyOf, ...rest } = result;
|
|
69
|
-
const fixedRest = fixAnyOfNullable(rest);
|
|
70
|
-
const fixedOther = fixAnyOfNullable(otherSchema);
|
|
71
|
-
return {
|
|
72
|
-
...fixedRest,
|
|
73
|
-
...fixedOther,
|
|
74
|
-
type: Array.isArray(fixedOther.type) ? [...fixedOther.type, "null"] : [fixedOther.type, "null"]
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
if (result.properties && typeof result.properties === "object" && !Array.isArray(result.properties)) {
|
|
79
|
-
result.properties = Object.fromEntries(
|
|
80
|
-
Object.entries(result.properties).map(([key, value]) => {
|
|
81
|
-
const propSchema = value;
|
|
82
|
-
if (typeof propSchema === "object" && propSchema !== null && !Array.isArray(propSchema) && Object.keys(propSchema).length === 0) {
|
|
83
|
-
return [key, { type: ["string", "number", "boolean", "null"] }];
|
|
84
|
-
}
|
|
85
|
-
return [key, fixAnyOfNullable(propSchema)];
|
|
86
|
-
})
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
if (result.items) {
|
|
90
|
-
if (Array.isArray(result.items)) {
|
|
91
|
-
result.items = result.items.map((item) => fixAnyOfNullable(item));
|
|
92
|
-
} else {
|
|
93
|
-
result.items = fixAnyOfNullable(result.items);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
if (result.anyOf && Array.isArray(result.anyOf)) {
|
|
97
|
-
result.anyOf = result.anyOf.map((s) => fixAnyOfNullable(s));
|
|
98
|
-
}
|
|
99
|
-
if (result.oneOf && Array.isArray(result.oneOf)) {
|
|
100
|
-
result.oneOf = result.oneOf.map((s) => fixAnyOfNullable(s));
|
|
101
|
-
}
|
|
102
|
-
if (result.allOf && Array.isArray(result.allOf)) {
|
|
103
|
-
result.allOf = result.allOf.map((s) => fixAnyOfNullable(s));
|
|
104
|
-
}
|
|
105
|
-
return result;
|
|
106
|
-
}
|
|
107
|
-
function ensureAllPropertiesRequired(schema) {
|
|
108
|
-
if (typeof schema !== "object" || schema === null) {
|
|
109
|
-
return schema;
|
|
110
|
-
}
|
|
111
|
-
const result = { ...schema };
|
|
112
|
-
if (result.type === "object" && result.properties) {
|
|
113
|
-
result.required = Object.keys(result.properties);
|
|
114
|
-
result.properties = Object.fromEntries(
|
|
115
|
-
Object.entries(result.properties).map(([key, value]) => [key, ensureAllPropertiesRequired(value)])
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
if (result.items) {
|
|
119
|
-
if (Array.isArray(result.items)) {
|
|
120
|
-
result.items = result.items.map((item) => ensureAllPropertiesRequired(item));
|
|
121
|
-
} else if (typeof result.items === "object") {
|
|
122
|
-
result.items = ensureAllPropertiesRequired(result.items);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
if (result.additionalProperties && typeof result.additionalProperties === "object") {
|
|
126
|
-
result.additionalProperties = ensureAllPropertiesRequired(result.additionalProperties);
|
|
127
|
-
}
|
|
128
|
-
if (result.anyOf && Array.isArray(result.anyOf)) {
|
|
129
|
-
result.anyOf = result.anyOf.map((s) => ensureAllPropertiesRequired(s));
|
|
130
|
-
}
|
|
131
|
-
if (result.oneOf && Array.isArray(result.oneOf)) {
|
|
132
|
-
result.oneOf = result.oneOf.map((s) => ensureAllPropertiesRequired(s));
|
|
133
|
-
}
|
|
134
|
-
if (result.allOf && Array.isArray(result.allOf)) {
|
|
135
|
-
result.allOf = result.allOf.map((s) => ensureAllPropertiesRequired(s));
|
|
136
|
-
}
|
|
137
|
-
return result;
|
|
138
|
-
}
|
|
139
|
-
function prepareJsonSchemaForOpenAIStrictMode(schema) {
|
|
140
|
-
const withRequired = ensureAllPropertiesRequired(schema);
|
|
141
|
-
return ensureAdditionalPropertiesFalse(withRequired);
|
|
142
|
-
}
|
|
143
|
-
function ensureAdditionalPropertiesFalse(schema) {
|
|
144
|
-
if (typeof schema !== "object" || schema === null) {
|
|
145
|
-
return schema;
|
|
146
|
-
}
|
|
147
|
-
const result = { ...schema };
|
|
148
|
-
if (result.type === "object" || result.properties) {
|
|
149
|
-
result.additionalProperties = false;
|
|
150
|
-
}
|
|
151
|
-
if (result.properties) {
|
|
152
|
-
result.properties = Object.fromEntries(
|
|
153
|
-
Object.entries(result.properties).map(([key, value]) => [
|
|
154
|
-
key,
|
|
155
|
-
ensureAdditionalPropertiesFalse(value)
|
|
156
|
-
])
|
|
157
|
-
);
|
|
158
|
-
}
|
|
159
|
-
if (result.items) {
|
|
160
|
-
if (Array.isArray(result.items)) {
|
|
161
|
-
result.items = result.items.map((item) => ensureAdditionalPropertiesFalse(item));
|
|
162
|
-
} else if (typeof result.items === "object") {
|
|
163
|
-
result.items = ensureAdditionalPropertiesFalse(result.items);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
if (result.anyOf && Array.isArray(result.anyOf)) {
|
|
167
|
-
result.anyOf = result.anyOf.map((s) => ensureAdditionalPropertiesFalse(s));
|
|
168
|
-
}
|
|
169
|
-
if (result.oneOf && Array.isArray(result.oneOf)) {
|
|
170
|
-
result.oneOf = result.oneOf.map((s) => ensureAdditionalPropertiesFalse(s));
|
|
171
|
-
}
|
|
172
|
-
if (result.allOf && Array.isArray(result.allOf)) {
|
|
173
|
-
result.allOf = result.allOf.map((s) => ensureAdditionalPropertiesFalse(s));
|
|
174
|
-
}
|
|
175
|
-
return result;
|
|
176
|
-
}
|
|
177
|
-
function zodToJsonSchema(zodSchema, target = "jsonSchema7", strategy = "relative") {
|
|
178
|
-
if (zodSchema?._zod) {
|
|
179
|
-
patchRecordSchemas(zodSchema);
|
|
180
|
-
const jsonSchema = v4.z.toJSONSchema(zodSchema, {
|
|
181
|
-
unrepresentable: "any",
|
|
182
|
-
io: "input",
|
|
183
|
-
override: (ctx) => {
|
|
184
|
-
const def = ctx.zodSchema?._def || ctx.zodSchema?._zod?.def;
|
|
185
|
-
if (def && (def.typeName === "ZodDate" || def.type === "date")) {
|
|
186
|
-
ctx.jsonSchema.type = "string";
|
|
187
|
-
ctx.jsonSchema.format = "date-time";
|
|
188
|
-
}
|
|
189
|
-
if (def && (def.typeName === "ZodObject" || def.type === "object")) {
|
|
190
|
-
ctx.jsonSchema.additionalProperties = false;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
});
|
|
194
|
-
return fixAnyOfNullable(jsonSchema);
|
|
195
|
-
} else {
|
|
196
|
-
return zodToJsonSchemaOriginal__default.default(zodSchema, {
|
|
197
|
-
$refStrategy: strategy,
|
|
198
|
-
target
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
exports.ensureAllPropertiesRequired = ensureAllPropertiesRequired;
|
|
204
|
-
exports.patchRecordSchemas = patchRecordSchemas;
|
|
205
|
-
exports.prepareJsonSchemaForOpenAIStrictMode = prepareJsonSchemaForOpenAIStrictMode;
|
|
206
|
-
exports.zodToJsonSchema = zodToJsonSchema;
|
|
207
|
-
//# sourceMappingURL=chunk-FJGZKUBH.cjs.map
|
|
208
|
-
//# sourceMappingURL=chunk-FJGZKUBH.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/zod-to-json.ts"],"names":["zV4","zodToJsonSchemaOriginal"],"mappings":";;;;;;;;;;AAOA,IAAM,OAAA,0BAAiB,oBAAoB,CAAA;AAcpC,SAAS,mBAAmB,MAAA,EAAkB;AACnD,EAAA,IAAI,CAAC,MAAA,IAAU,OAAO,MAAA,KAAW,UAAU,OAAO,MAAA;AAGlD,EAAA,IAAK,MAAA,CAAe,OAAO,CAAA,EAAG,OAAO,MAAA;AACrC,EAAC,MAAA,CAAe,OAAO,CAAA,GAAI,IAAA;AAG3B,EAAA,MAAM,GAAA,GAAM,OAAO,IAAA,EAAM,GAAA;AAGzB,EAAA,IAAI,KAAK,IAAA,KAAS,QAAA,IAAY,IAAI,OAAA,IAAW,CAAC,IAAI,SAAA,EAAW;AAG3D,IAAA,GAAA,CAAI,YAAY,GAAA,CAAI,OAAA;AACpB,IAAA,GAAA,CAAI,OAAA,GAAUA,KAAI,MAAA,EAAO;AAAA,EAC3B;AAGA,EAAA,IAAI,CAAC,KAAK,OAAO,MAAA;AAEjB,EAAA,IAAI,GAAA,CAAI,IAAA,KAAS,QAAA,IAAY,GAAA,CAAI,KAAA,EAAO;AACtC,IAAA,MAAM,KAAA,GAAQ,OAAO,GAAA,CAAI,KAAA,KAAU,aAAa,GAAA,CAAI,KAAA,KAAU,GAAA,CAAI,KAAA;AAClE,IAAA,KAAA,MAAW,GAAA,IAAO,MAAA,CAAO,IAAA,CAAK,KAAK,CAAA,EAAG;AACpC,MAAA,kBAAA,CAAmB,KAAA,CAAM,GAAG,CAAC,CAAA;AAAA,IAC/B;AAAA,EACF;AAEA,EAAA,IAAI,GAAA,CAAI,IAAA,KAAS,OAAA,IAAW,GAAA,CAAI,OAA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AAA;AAGD,IAAA,OAAO,iBAAiB,UAAU,CAAA;AAAA,EACpC,CAAA,MAAO;AAEL,IAAA,OAAOC,yCAAwB,SAAA,EAA0B;AAAA,MACvD,YAAA,EAAc,QAAA;AAAA,MACd;AAAA,KACD,CAAA;AAAA,EACH;AACF","file":"chunk-FJGZKUBH.cjs","sourcesContent":["import type { JSONSchema7 } from 'json-schema';\nimport type { ZodSchema as ZodSchemaV3 } from 'zod/v3';\nimport { z as zV4 } from 'zod/v4';\nimport type { Targets } from 'zod-to-json-schema';\nimport zodToJsonSchemaOriginal from 'zod-to-json-schema';\n\n// Symbol to mark schemas as already patched (for idempotency)\nconst PATCHED = Symbol('__mastra_patched__');\n\n/**\n * Recursively patch Zod v4 record schemas that are missing valueType.\n * This fixes a bug in Zod v4 where z.record(valueSchema) doesn't set def.valueType.\n * The single-arg form should set valueType but instead only sets keyType.\n *\n * Idempotent — marks patched schemas with a Symbol so repeat calls no-op.\n *\n * @internal Exported so the Zod v4 standard-schema adapter can apply the\n * same patch before its native `toJSONSchema` call (the legacy `zodToJsonSchema`\n * entry already calls it; the `applyCompatLayer` path otherwise wouldn't).\n * Not part of the public API.\n */\nexport function patchRecordSchemas(schema: any): any {\n if (!schema || typeof schema !== 'object') return schema;\n\n // Skip if already patched (idempotency check)\n if ((schema as any)[PATCHED]) return schema;\n (schema as any)[PATCHED] = true;\n\n // Check the _zod.def location (v4 structure)\n const def = schema._zod?.def;\n\n // Fix record schemas with missing valueType\n if (def?.type === 'record' && def.keyType && !def.valueType) {\n // The bug: z.record(valueSchema) puts the value in keyType instead of valueType\n // Fix: move it to valueType and set keyType to string (the default)\n def.valueType = def.keyType;\n def.keyType = zV4.string();\n }\n\n // Recursively patch nested schemas\n if (!def) return schema;\n\n if (def.type === 'object' && def.shape) {\n const shape = typeof def.shape === 'function' ? def.shape() : def.shape;\n for (const key of Object.keys(shape)) {\n patchRecordSchemas(shape[key]);\n }\n }\n\n if (def.type === 'array' && def.element) {\n patchRecordSchemas(def.element);\n }\n\n if (def.type === 'union' && def.options) {\n def.options.forEach(patchRecordSchemas);\n }\n\n if (def.type === 'record') {\n if (def.keyType) patchRecordSchemas(def.keyType);\n if (def.valueType) patchRecordSchemas(def.valueType);\n }\n\n // Handle intersection types\n if (def.type === 'intersection') {\n if (def.left) patchRecordSchemas(def.left);\n if (def.right) patchRecordSchemas(def.right);\n }\n\n // Handle lazy types - patch the schema returned by the getter\n if (def.type === 'lazy') {\n // For lazy schemas, we need to patch the schema when it's accessed\n // Store the original getter and wrap it\n if (def.getter && typeof def.getter === 'function') {\n const originalGetter = def.getter;\n def.getter = function () {\n const innerSchema = originalGetter();\n if (innerSchema) {\n patchRecordSchemas(innerSchema);\n }\n return innerSchema;\n };\n }\n }\n\n // Handle wrapper types that have innerType\n // This covers: optional, nullable, default, catch, nullish, and any other wrappers\n if (def.innerType) {\n patchRecordSchemas(def.innerType);\n }\n\n return schema;\n}\n\n/**\n * Recursively fixes anyOf patterns that some providers (like OpenAI) don't accept.\n * Converts anyOf: [{type: X}, {type: \"null\"}] to type: [X, \"null\"]\n * Also fixes empty {} property schemas by converting to a union of primitive types.\n */\nfunction fixAnyOfNullable(schema: JSONSchema7): JSONSchema7 {\n if (typeof schema !== 'object' || schema === null) {\n return schema;\n }\n\n const result = { ...schema };\n\n // Fix anyOf pattern: [{type: X}, {type: \"null\"}] or [{type: \"null\"}, {type: X}]\n if (result.anyOf && Array.isArray(result.anyOf) && result.anyOf.length === 2) {\n const nullSchema = result.anyOf.find((s: any) => typeof s === 'object' && s !== null && s.type === 'null');\n const otherSchema = result.anyOf.find((s: any) => typeof s === 'object' && s !== null && s.type !== 'null');\n\n if (nullSchema && otherSchema && typeof otherSchema === 'object' && otherSchema.type) {\n // Convert anyOf to type array format\n // Normalize sibling fields (like properties/items) before returning\n const { anyOf, ...rest } = result;\n const fixedRest = fixAnyOfNullable(rest as JSONSchema7);\n const fixedOther = fixAnyOfNullable(otherSchema as JSONSchema7);\n return {\n ...fixedRest,\n ...fixedOther,\n type: (Array.isArray(fixedOther.type)\n ? [...fixedOther.type, 'null']\n : [fixedOther.type, 'null']) as JSONSchema7['type'],\n };\n }\n }\n\n // Fix empty property schemas {} - OpenAI requires a type key\n if (result.properties && typeof result.properties === 'object' && !Array.isArray(result.properties)) {\n result.properties = Object.fromEntries(\n Object.entries(result.properties).map(([key, value]) => {\n const propSchema = value as JSONSchema7;\n\n // If property is an empty object {}, convert to allow primitive types\n // Note: We exclude 'object' (requires additionalProperties) and 'array' (requires items) for OpenAI\n if (\n typeof propSchema === 'object' &&\n propSchema !== null &&\n !Array.isArray(propSchema) &&\n Object.keys(propSchema).length === 0\n ) {\n return [key, { type: ['string', 'number', 'boolean', 'null'] as JSONSchema7['type'] }];\n }\n\n // Recursively fix nested schemas\n return [key, fixAnyOfNullable(propSchema)];\n }),\n );\n }\n\n // Recursively fix items in arrays\n if (result.items) {\n if (Array.isArray(result.items)) {\n result.items = result.items.map(item => fixAnyOfNullable(item as JSONSchema7));\n } else {\n result.items = fixAnyOfNullable(result.items as JSONSchema7);\n }\n }\n\n // Recursively fix anyOf/oneOf/allOf schemas\n if (result.anyOf && Array.isArray(result.anyOf)) {\n result.anyOf = result.anyOf.map(s => fixAnyOfNullable(s as JSONSchema7));\n }\n if (result.oneOf && Array.isArray(result.oneOf)) {\n result.oneOf = result.oneOf.map(s => fixAnyOfNullable(s as JSONSchema7));\n }\n if (result.allOf && Array.isArray(result.allOf)) {\n result.allOf = result.allOf.map(s => fixAnyOfNullable(s as JSONSchema7));\n }\n\n return result;\n}\n\n/**\n * Recursively ensures all properties in an object schema are included in the `required` array.\n * OpenAI's strict structured output mode requires every key in `properties` to also appear in `required`.\n *\n * @param schema - The JSON Schema to process\n * @returns A new schema with all properties marked as required\n */\nexport function ensureAllPropertiesRequired(schema: JSONSchema7): JSONSchema7 {\n if (typeof schema !== 'object' || schema === null) {\n return schema;\n }\n\n const result = { ...schema };\n\n if (result.type === 'object' && result.properties) {\n result.required = Object.keys(result.properties);\n result.properties = Object.fromEntries(\n Object.entries(result.properties).map(([key, value]) => [key, ensureAllPropertiesRequired(value as JSONSchema7)]),\n );\n }\n\n if (result.items) {\n if (Array.isArray(result.items)) {\n result.items = result.items.map(item => ensureAllPropertiesRequired(item as JSONSchema7));\n } else if (typeof result.items === 'object') {\n result.items = ensureAllPropertiesRequired(result.items as JSONSchema7);\n }\n }\n\n if (result.additionalProperties && typeof result.additionalProperties === 'object') {\n result.additionalProperties = ensureAllPropertiesRequired(result.additionalProperties as JSONSchema7);\n }\n\n if (result.anyOf && Array.isArray(result.anyOf)) {\n result.anyOf = result.anyOf.map(s => ensureAllPropertiesRequired(s as JSONSchema7));\n }\n if (result.oneOf && Array.isArray(result.oneOf)) {\n result.oneOf = result.oneOf.map(s => ensureAllPropertiesRequired(s as JSONSchema7));\n }\n if (result.allOf && Array.isArray(result.allOf)) {\n result.allOf = result.allOf.map(s => ensureAllPropertiesRequired(s as JSONSchema7));\n }\n\n return result;\n}\n\n/**\n * Prepare a JSON Schema for OpenAI strict mode by ensuring all object properties\n * are required and all objects have additionalProperties: false.\n */\nexport function prepareJsonSchemaForOpenAIStrictMode(schema: JSONSchema7): JSONSchema7 {\n const withRequired = ensureAllPropertiesRequired(schema);\n return ensureAdditionalPropertiesFalse(withRequired);\n}\n\nfunction ensureAdditionalPropertiesFalse(schema: JSONSchema7): JSONSchema7 {\n if (typeof schema !== 'object' || schema === null) {\n return schema;\n }\n\n const result = { ...schema };\n\n if (result.type === 'object' || result.properties) {\n result.additionalProperties = false;\n }\n\n if (result.properties) {\n result.properties = Object.fromEntries(\n Object.entries(result.properties).map(([key, value]) => [\n key,\n ensureAdditionalPropertiesFalse(value as JSONSchema7),\n ]),\n );\n }\n\n if (result.items) {\n if (Array.isArray(result.items)) {\n result.items = result.items.map(item => ensureAdditionalPropertiesFalse(item as JSONSchema7));\n } else if (typeof result.items === 'object') {\n result.items = ensureAdditionalPropertiesFalse(result.items as JSONSchema7);\n }\n }\n\n if (result.anyOf && Array.isArray(result.anyOf)) {\n result.anyOf = result.anyOf.map(s => ensureAdditionalPropertiesFalse(s as JSONSchema7));\n }\n if (result.oneOf && Array.isArray(result.oneOf)) {\n result.oneOf = result.oneOf.map(s => ensureAdditionalPropertiesFalse(s as JSONSchema7));\n }\n if (result.allOf && Array.isArray(result.allOf)) {\n result.allOf = result.allOf.map(s => ensureAdditionalPropertiesFalse(s as JSONSchema7));\n }\n\n return result;\n}\n\n// export function zotToJsonSchema(zodSchema: ZodSchemaV3 | ZodSchemaV4, target: Targets = 'jsonSchema7', strategy: 'none' | 'seen' | 'root' | 'relative' = 'relative'): JSONSchema7 {\n// const target = 'draft-07' as StandardJSONSchemaV1.Target;\n// const standardSchema = toStandardSchema(zodSchema);\n// const jsonSchema = standardSchemaToJSONSchema(standardSchema, {\n// target,\n// });\n\n// traverse(jsonSchema, {\n// cb: {\n// pre: (schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema) => {\n// this.preProcessJSONNode(schema, parentSchema);\n// },\n// post: (schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema) => {\n// this.postProcessJSONNode(schema, parentSchema);\n// },\n// },\n// });\n\n// }\n\nexport function zodToJsonSchema(\n zodSchema: any,\n target: Targets = 'jsonSchema7',\n strategy: 'none' | 'seen' | 'root' | 'relative' = 'relative',\n): JSONSchema7 {\n // Route based on whether the schema is v4 (has _zod) or v3 (only has _def).\n // We use zV4.toJSONSchema (imported from 'zod/v4') for v4 schemas, since the\n // default 'zod' import may resolve to v3 depending on the environment.\n // Without this check, v3 schemas passed to v4's toJSONSchema would throw\n // \"Cannot read properties of undefined (reading 'def')\".\n if (zodSchema?._zod) {\n // Zod v4 path - patch record schemas before converting\n patchRecordSchemas(zodSchema);\n\n const jsonSchema = zV4.toJSONSchema(zodSchema, {\n unrepresentable: 'any',\n io: 'input',\n override: (ctx: any) => {\n // Handle both Zod v4 structures: _def directly or nested in _zod\n const def = ctx.zodSchema?._def || ctx.zodSchema?._zod?.def;\n // Check for date type using both possible property names\n if (def && (def.typeName === 'ZodDate' || def.type === 'date')) {\n ctx.jsonSchema.type = 'string';\n ctx.jsonSchema.format = 'date-time';\n }\n // Add additionalProperties: false for object types to match Zod v3 behavior\n // This is required for OpenAI strict mode function calling\n if (def && (def.typeName === 'ZodObject' || def.type === 'object')) {\n ctx.jsonSchema.additionalProperties = false;\n }\n },\n }) as JSONSchema7;\n\n // Fix anyOf patterns for nullable fields - required for OpenAI compatibility\n return fixAnyOfNullable(jsonSchema);\n } else {\n // Zod v3 path - use the original converter\n return zodToJsonSchemaOriginal(zodSchema as ZodSchemaV3, {\n $refStrategy: strategy,\n target,\n }) as JSONSchema7;\n }\n}\n"]}
|
package/dist/chunk-FS3P4V5M.cjs
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
// src/standard-schema/adapters/ai-sdk.ts
|
|
4
|
-
var VENDOR = "ai-sdk";
|
|
5
|
-
var AiSdkSchemaWrapper = class {
|
|
6
|
-
#schema;
|
|
7
|
-
"~standard";
|
|
8
|
-
constructor(schema) {
|
|
9
|
-
this.#schema = schema;
|
|
10
|
-
this["~standard"] = {
|
|
11
|
-
version: 1,
|
|
12
|
-
vendor: VENDOR,
|
|
13
|
-
validate: this.#validate.bind(this),
|
|
14
|
-
jsonSchema: {
|
|
15
|
-
input: this.#toJsonSchema.bind(this),
|
|
16
|
-
output: this.#toJsonSchema.bind(this)
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Validates a value against the AI SDK Schema.
|
|
22
|
-
*
|
|
23
|
-
* @param value - The value to validate
|
|
24
|
-
* @returns A result object with either the validated value or validation issues
|
|
25
|
-
*/
|
|
26
|
-
#validate(value) {
|
|
27
|
-
if (!this.#schema.validate) {
|
|
28
|
-
return { value };
|
|
29
|
-
}
|
|
30
|
-
try {
|
|
31
|
-
const result = this.#schema.validate(value);
|
|
32
|
-
if (result && typeof result === "object" && "then" in result && typeof result.then === "function") {
|
|
33
|
-
return Promise.resolve(
|
|
34
|
-
result
|
|
35
|
-
).then((res) => this.#convertValidationResult(res)).catch((error) => {
|
|
36
|
-
const message = error instanceof Error ? error.message : "Unknown validation error";
|
|
37
|
-
return {
|
|
38
|
-
issues: [{ message: `Schema validation error: ${message}` }]
|
|
39
|
-
};
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
return this.#convertValidationResult(
|
|
43
|
-
result
|
|
44
|
-
);
|
|
45
|
-
} catch (error) {
|
|
46
|
-
const message = error instanceof Error ? error.message : "Unknown validation error";
|
|
47
|
-
return {
|
|
48
|
-
issues: [{ message: `Schema validation error: ${message}` }]
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Converts an AI SDK ValidationResult to a StandardSchemaV1.Result.
|
|
54
|
-
*
|
|
55
|
-
* @param result - The AI SDK validation result
|
|
56
|
-
* @returns A StandardSchemaV1.Result
|
|
57
|
-
*/
|
|
58
|
-
#convertValidationResult(result) {
|
|
59
|
-
if (result.success) {
|
|
60
|
-
return { value: result.value };
|
|
61
|
-
}
|
|
62
|
-
const failureResult = result;
|
|
63
|
-
return {
|
|
64
|
-
issues: [{ message: failureResult.error.message }]
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Returns the JSON Schema in the requested target format.
|
|
69
|
-
*
|
|
70
|
-
* @param options - Options including the target format
|
|
71
|
-
* @returns The JSON Schema as a Record
|
|
72
|
-
*/
|
|
73
|
-
#toJsonSchema(options) {
|
|
74
|
-
const { target } = options;
|
|
75
|
-
const clonedSchema = JSON.parse(JSON.stringify(this.#schema.jsonSchema));
|
|
76
|
-
if (!clonedSchema.$schema) {
|
|
77
|
-
switch (target) {
|
|
78
|
-
case "draft-07":
|
|
79
|
-
clonedSchema.$schema = "http://json-schema.org/draft-07/schema#";
|
|
80
|
-
break;
|
|
81
|
-
case "draft-2020-12":
|
|
82
|
-
clonedSchema.$schema = "https://json-schema.org/draft/2020-12/schema";
|
|
83
|
-
break;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return clonedSchema;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Returns the original AI SDK Schema.
|
|
90
|
-
*/
|
|
91
|
-
getSchema() {
|
|
92
|
-
return this.#schema;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Returns the original JSON Schema from the AI SDK Schema.
|
|
96
|
-
*/
|
|
97
|
-
getJsonSchema() {
|
|
98
|
-
return this.#schema.jsonSchema;
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
function toStandardSchema(schema) {
|
|
102
|
-
return new AiSdkSchemaWrapper(schema);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
exports.AiSdkSchemaWrapper = AiSdkSchemaWrapper;
|
|
106
|
-
exports.toStandardSchema = toStandardSchema;
|
|
107
|
-
//# sourceMappingURL=chunk-FS3P4V5M.cjs.map
|
|
108
|
-
//# sourceMappingURL=chunk-FS3P4V5M.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/standard-schema/adapters/ai-sdk.ts"],"names":[],"mappings":";;;AAQA,IAAM,MAAA,GAAS,QAAA;AAoCR,IAAM,qBAAN,MAA2G;AAAA,EACvG,OAAA;AAAA,EACA,WAAA;AAAA,EAET,YAAY,MAAA,EAAwB;AAClC,IAAA,IAAA,CAAK,OAAA,GAAU,MAAA;AAGf,IAAA,IAAA,CAAK,WAAW,CAAA,GAAI;AAAA,MAClB,OAAA,EAAS,CAAA;AAAA,MACT,MAAA,EAAQ,MAAA;AAAA,MACR,QAAA,EAAU,IAAA,CAAK,SAAA,CAAU,IAAA,CAAK,IAAI,CAAA;AAAA,MAClC,UAAA,EAAY;AAAA,QACV,KAAA,EAAO,IAAA,CAAK,aAAA,CAAc,IAAA,CAAK,IAAI,CAAA;AAAA,QACn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ACE,MAAA,EACiC;AACjC,IAAA,IAAI,OAAO,OAAA,EAAS;AAClB,MAAA,OAAO,EAAE,KAAA,EAAO,MAAA,CAAO,KAAA,EAAM;AAAA,IAC/B;AAIA,IAAA,MAAM,aAAA,GAAgB,MAAA;AACtB,IAAA,OAAO;AAAA,MACL,QAAQ,CAAC,EAAE,SAAS,aAAA,CAAc,KAAA,CAAM,SAAS;AAAA,KACnD;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,cAAc,OAAA,EAAgE;AAC5E,IAAA,MAAM,EAAE,QAAO,GAAI,OAAA;AAGnB,IAAA,MAAM,YAAA,GAAe,KAAK,KAAA,CAAM,IAAA,CAAK,UAAU,IAAA,CAAK,OAAA,CAAQ,UAAU,CAAC,CAAA;AAGvE,IAAA,IAAI,CAAC,aAAa,OAAA,EAAS;AACzB,MAAA,QAAQ,MAAA;AAAQ,QACd,KAAK,UAAA;AACH,UAAA,YAAA,CAAa,OAAA,GAAU,yCAAA;AACvB,UAAA;AAAA,QACF,KAAK,eAAA;AACH,UAAA,YAAA,CAAa,OAAA,GAAU,8CAAA;AACvB,UAAA;AAMA;AACJ,IACF;AAEA,IAAA,OAAO,YAAA;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKA,SAAA,GAA4B;AAC1B,IAAA,OAAO,IAAA,CAAK,OAAA;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKA,aAAA,GAA6B;AAC3B,IAAA,OAAO,KAAK,OAAA,CAAQ,UAAA;AAAA,EACtB;AACF;AAmCO,SAAS,iBAA8B,MAAA,EAA6C;AACzF,EAAA,OAAO,IAAI,mBAAyB,MAAM,CAAA;AAC5C","file":"chunk-FS3P4V5M.cjs","sourcesContent":["import type { Schema } from '@internal/ai-v6';\nimport type { StandardSchemaV1, StandardJSONSchemaV1 } from '@standard-schema/spec';\nimport type { JSONSchema7 } from 'json-schema';\nimport type { StandardSchemaWithJSON, StandardSchemaWithJSONProps } from '../standard-schema.types';\n\n/**\n * Vendor name for AI SDK wrapped schemas.\n */\nconst VENDOR = 'ai-sdk';\n\n/**\n * A wrapper class that makes AI SDK Schema compatible with @standard-schema/spec.\n *\n * This class implements both `StandardSchemaV1` (validation) and `StandardJSONSchemaV1`\n * (JSON Schema conversion) interfaces. It wraps an AI SDK Schema and adapts its\n * validation method and jsonSchema property to the standard-schema interface.\n *\n * @typeParam T - The TypeScript type that the AI SDK Schema represents\n *\n * @example\n * ```typescript\n * import { jsonSchema } from '@internal/ai-v6';\n * import { toStandardSchema } from '@mastra/schema-compat/adapters/ai-sdk';\n *\n * // Create an AI SDK schema\n * const aiSdkSchema = jsonSchema<{ name: string; age: number }>({\n * type: 'object',\n * properties: {\n * name: { type: 'string' },\n * age: { type: 'number' },\n * },\n * required: ['name', 'age'],\n * });\n *\n * // Convert to standard-schema\n * const standardSchema = toStandardSchema(aiSdkSchema);\n *\n * // Use validation (from StandardSchemaV1)\n * const result = standardSchema['~standard'].validate({ name: 'John', age: 30 });\n *\n * // Get JSON Schema (from StandardJSONSchemaV1)\n * const jsonSchema = standardSchema['~standard'].jsonSchema.output({ target: 'draft-07' });\n * ```\n */\nexport class AiSdkSchemaWrapper<Input = unknown, Output = Input> implements StandardSchemaWithJSON<Input, Output> {\n readonly #schema: Schema<Output>;\n readonly '~standard': StandardSchemaWithJSONProps<Input, Output>;\n\n constructor(schema: Schema<Output>) {\n this.#schema = schema;\n\n // Create the ~standard property\n this['~standard'] = {\n version: 1,\n vendor: VENDOR,\n validate: this.#validate.bind(this),\n jsonSchema: {\n input: this.#toJsonSchema.bind(this),\n output: this.#toJsonSchema.bind(this),\n },\n };\n }\n\n /**\n * Validates a value against the AI SDK Schema.\n *\n * @param value - The value to validate\n * @returns A result object with either the validated value or validation issues\n */\n #validate(value: unknown): StandardSchemaV1.Result<Output> | Promise<StandardSchemaV1.Result<Output>> {\n // Check if the schema has a validate method (it's optional on AI SDK Schema)\n if (!this.#schema.validate) {\n // If no validate method, we can't validate - just pass through\n return { value: value as Output };\n }\n\n try {\n const result = this.#schema.validate(value);\n\n // Handle both sync and async validation results\n // The AI SDK Schema.validate returns ValidationResult<OBJECT> | PromiseLike<ValidationResult<OBJECT>>\n // We need to check if it's a thenable (promise-like)\n if (result && typeof result === 'object' && 'then' in result && typeof result.then === 'function') {\n // Wrap PromiseLike in a proper Promise to satisfy the StandardSchemaV1 interface\n return Promise.resolve(\n result as PromiseLike<{ success: true; value: Output } | { success: false; error: Error }>,\n )\n .then(res => this.#convertValidationResult(res))\n .catch((error: unknown) => {\n // Convert rejected promises to the expected { issues: [...] } shape\n const message = error instanceof Error ? error.message : 'Unknown validation error';\n return {\n issues: [{ message: `Schema validation error: ${message}` }],\n } as StandardSchemaV1.Result<Output>;\n });\n }\n\n // It's a sync result\n return this.#convertValidationResult(\n result as { success: true; value: Output } | { success: false; error: Error },\n );\n } catch (error) {\n // If validation fails unexpectedly, return a validation error\n const message = error instanceof Error ? error.message : 'Unknown validation error';\n return {\n issues: [{ message: `Schema validation error: ${message}` }],\n };\n }\n }\n\n /**\n * Converts an AI SDK ValidationResult to a StandardSchemaV1.Result.\n *\n * @param result - The AI SDK validation result\n * @returns A StandardSchemaV1.Result\n */\n #convertValidationResult(\n result: { success: true; value: Output } | { success: false; error: Error },\n ): StandardSchemaV1.Result<Output> {\n if (result.success) {\n return { value: result.value };\n }\n\n // Convert the AI SDK error to a Standard Schema issue\n // Cast to the failure type since TypeScript can't narrow discriminated unions with private field access\n const failureResult = result as { success: false; error: Error };\n return {\n issues: [{ message: failureResult.error.message }],\n };\n }\n\n /**\n * Returns the JSON Schema in the requested target format.\n *\n * @param options - Options including the target format\n * @returns The JSON Schema as a Record\n */\n #toJsonSchema(options: StandardJSONSchemaV1.Options): Record<string, unknown> {\n const { target } = options;\n\n // Clone the schema to avoid mutations\n const clonedSchema = JSON.parse(JSON.stringify(this.#schema.jsonSchema)) as Record<string, unknown>;\n\n // Add $schema if not present, based on target\n if (!clonedSchema.$schema) {\n switch (target) {\n case 'draft-07':\n clonedSchema.$schema = 'http://json-schema.org/draft-07/schema#';\n break;\n case 'draft-2020-12':\n clonedSchema.$schema = 'https://json-schema.org/draft/2020-12/schema';\n break;\n case 'openapi-3.0':\n // OpenAPI 3.0 doesn't use $schema\n break;\n default:\n // For unknown targets, don't add $schema\n break;\n }\n }\n\n return clonedSchema;\n }\n\n /**\n * Returns the original AI SDK Schema.\n */\n getSchema(): Schema<Output> {\n return this.#schema;\n }\n\n /**\n * Returns the original JSON Schema from the AI SDK Schema.\n */\n getJsonSchema(): JSONSchema7 {\n return this.#schema.jsonSchema as JSONSchema7;\n }\n}\n\n/**\n * Wraps an AI SDK Schema to implement the full @standard-schema/spec interface.\n *\n * This function creates a wrapper that implements both `StandardSchemaV1` (validation)\n * and `StandardJSONSchemaV1` (JSON Schema conversion) interfaces.\n *\n * @typeParam T - The TypeScript type that the AI SDK Schema represents\n * @param schema - The AI SDK Schema to wrap\n * @returns A wrapper implementing StandardSchemaWithJSON\n *\n * @example\n * ```typescript\n * import { jsonSchema } from '@internal/ai-v6';\n * import { toStandardSchema } from '@mastra/schema-compat/adapters/ai-sdk';\n *\n * const aiSdkSchema = jsonSchema<{ name: string; age: number }>({\n * type: 'object',\n * properties: {\n * name: { type: 'string' },\n * age: { type: 'number' },\n * },\n * required: ['name', 'age'],\n * });\n *\n * const standardSchema = toStandardSchema(aiSdkSchema);\n *\n * // Validate data\n * const result = standardSchema['~standard'].validate({ name: 'John', age: 30 });\n *\n * // Get JSON Schema\n * const jsonSchema = standardSchema['~standard'].jsonSchema.output({ target: 'draft-07' });\n * ```\n */\nexport function toStandardSchema<T = unknown>(schema: Schema<T>): AiSdkSchemaWrapper<T, T> {\n return new AiSdkSchemaWrapper<T, T>(schema);\n}\n"]}
|