@mastra/schema-compat 0.10.7 → 0.11.1
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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +40 -0
- package/README.md +0 -4
- package/dist/chunk-FTKGHMGD.js +27 -0
- package/dist/chunk-FTKGHMGD.js.map +1 -0
- package/dist/chunk-LNR4XKDU.cjs +33 -0
- package/dist/chunk-LNR4XKDU.cjs.map +1 -0
- package/dist/index.cjs +846 -84
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +10 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +845 -85
- package/dist/index.js.map +1 -1
- package/dist/provider-compats/anthropic.d.ts +6 -4
- package/dist/provider-compats/anthropic.d.ts.map +1 -1
- package/dist/provider-compats/deepseek.d.ts +6 -4
- package/dist/provider-compats/deepseek.d.ts.map +1 -1
- package/dist/provider-compats/google.d.ts +6 -4
- package/dist/provider-compats/google.d.ts.map +1 -1
- package/dist/provider-compats/meta.d.ts +6 -4
- package/dist/provider-compats/meta.d.ts.map +1 -1
- package/dist/provider-compats/openai-reasoning.d.ts +6 -4
- package/dist/provider-compats/openai-reasoning.d.ts.map +1 -1
- package/dist/provider-compats/openai.d.ts +6 -4
- package/dist/provider-compats/openai.d.ts.map +1 -1
- package/dist/schema-compatibility-v3.d.ts +319 -0
- package/dist/schema-compatibility-v3.d.ts.map +1 -0
- package/dist/schema-compatibility-v4.d.ts +310 -0
- package/dist/schema-compatibility-v4.d.ts.map +1 -0
- package/dist/schema-compatibility.d.ts +80 -134
- package/dist/schema-compatibility.d.ts.map +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils-test-suite.d.ts +2 -0
- package/dist/utils-test-suite.d.ts.map +1 -0
- package/dist/utils.d.ts +17 -5
- package/dist/utils.d.ts.map +1 -1
- package/dist/zod-to-json.cjs +12 -0
- package/dist/zod-to-json.cjs.map +1 -0
- package/dist/zod-to-json.d.ts +6 -0
- package/dist/zod-to-json.d.ts.map +1 -0
- package/dist/zod-to-json.js +3 -0
- package/dist/zod-to-json.js.map +1 -0
- package/dist/zodTypes.d.ts +21 -0
- package/dist/zodTypes.d.ts.map +1 -0
- package/package.json +16 -6
- package/src/index.ts +4 -3
- package/src/provider-compats/anthropic.ts +29 -11
- package/src/provider-compats/deepseek.ts +14 -9
- package/src/provider-compats/google.ts +18 -32
- package/src/provider-compats/meta.ts +15 -10
- package/src/provider-compats/openai-reasoning.ts +18 -24
- package/src/provider-compats/openai.ts +22 -12
- package/src/schema-compatibility-v3.ts +664 -0
- package/src/schema-compatibility-v4.test.ts +476 -0
- package/src/schema-compatibility-v4.ts +706 -0
- package/src/schema-compatibility.test.ts +12 -28
- package/src/schema-compatibility.ts +263 -383
- package/src/types.ts +5 -0
- package/src/utils-test-suite.ts +467 -0
- package/src/utils-v3.test.ts +9 -0
- package/src/utils-v4.test.ts +9 -0
- package/src/utils.ts +30 -24
- package/src/zod-to-json.ts +27 -0
- package/src/zodTypes.ts +56 -0
- package/tsup.config.ts +8 -3
- package/src/utils.test.ts +0 -460
|
@@ -1,116 +1,36 @@
|
|
|
1
1
|
import type { Schema } from 'ai';
|
|
2
2
|
import type { JSONSchema7 } from 'json-schema';
|
|
3
|
-
import { z
|
|
4
|
-
import type {
|
|
3
|
+
import type { z as zV3 } from 'zod/v3';
|
|
4
|
+
import type { z as zV4, ZodType } from 'zod/v4';
|
|
5
5
|
import type { Targets } from 'zod-to-json-schema';
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
export const isUnion = (v: ZodTypeAny): v is ZodUnion<[ZodTypeAny, ...ZodTypeAny[]]> => v instanceof ZodUnion;
|
|
35
|
-
export const isString = (v: ZodTypeAny): v is ZodString => v instanceof ZodString;
|
|
36
|
-
export const isNumber = (v: ZodTypeAny): v is ZodNumber => v instanceof ZodNumber;
|
|
37
|
-
export const isDate = (v: ZodTypeAny): v is ZodDate => v instanceof ZodDate;
|
|
38
|
-
export const isDefault = (v: ZodTypeAny): v is ZodDefault<any> => v instanceof ZodDefault;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Zod types that are not supported by most AI model providers and should be avoided.
|
|
42
|
-
* @constant
|
|
43
|
-
*/
|
|
44
|
-
export const UNSUPPORTED_ZOD_TYPES = ['ZodIntersection', 'ZodNever', 'ZodNull', 'ZodTuple', 'ZodUndefined'] as const;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Zod types that are generally supported by AI model providers.
|
|
48
|
-
* @constant
|
|
49
|
-
*/
|
|
50
|
-
export const SUPPORTED_ZOD_TYPES = [
|
|
51
|
-
'ZodObject',
|
|
52
|
-
'ZodArray',
|
|
53
|
-
'ZodUnion',
|
|
54
|
-
'ZodString',
|
|
55
|
-
'ZodNumber',
|
|
56
|
-
'ZodDate',
|
|
57
|
-
'ZodAny',
|
|
58
|
-
'ZodDefault',
|
|
59
|
-
] as const;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* All Zod types (both supported and unsupported).
|
|
63
|
-
* @constant
|
|
64
|
-
*/
|
|
65
|
-
export const ALL_ZOD_TYPES = [...SUPPORTED_ZOD_TYPES, ...UNSUPPORTED_ZOD_TYPES] as const;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Type representing string validation checks.
|
|
69
|
-
*/
|
|
70
|
-
export type StringCheckType = (typeof ALL_STRING_CHECKS)[number];
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Type representing number validation checks.
|
|
74
|
-
*/
|
|
75
|
-
export type NumberCheckType = (typeof ALL_NUMBER_CHECKS)[number];
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Type representing array validation checks.
|
|
79
|
-
*/
|
|
80
|
-
export type ArrayCheckType = (typeof ALL_ARRAY_CHECKS)[number];
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Type representing unsupported Zod schema types.
|
|
84
|
-
*/
|
|
85
|
-
export type UnsupportedZodType = (typeof UNSUPPORTED_ZOD_TYPES)[number];
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Type representing supported Zod schema types.
|
|
89
|
-
*/
|
|
90
|
-
export type SupportedZodType = (typeof SUPPORTED_ZOD_TYPES)[number];
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Type representing all Zod schema types (supported and unsupported).
|
|
94
|
-
*/
|
|
95
|
-
export type AllZodType = (typeof ALL_ZOD_TYPES)[number];
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Utility type to extract the shape of a Zod object schema.
|
|
99
|
-
*/
|
|
100
|
-
export type ZodShape<T extends z.AnyZodObject> = T['shape'];
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Utility type to extract the keys from a Zod object shape.
|
|
104
|
-
*/
|
|
105
|
-
export type ShapeKey<T extends z.AnyZodObject> = keyof ZodShape<T>;
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Utility type to extract the value types from a Zod object shape.
|
|
109
|
-
*/
|
|
110
|
-
export type ShapeValue<T extends z.AnyZodObject> = ZodShape<T>[ShapeKey<T>];
|
|
111
|
-
|
|
112
|
-
// Add constraint types at the top
|
|
113
|
-
|
|
6
|
+
import {
|
|
7
|
+
SchemaCompatLayer as SchemaCompatLayerV3,
|
|
8
|
+
ALL_STRING_CHECKS,
|
|
9
|
+
ALL_NUMBER_CHECKS,
|
|
10
|
+
ALL_ARRAY_CHECKS,
|
|
11
|
+
UNSUPPORTED_ZOD_TYPES as UNSUPPORTED_ZOD_TYPES_V3,
|
|
12
|
+
SUPPORTED_ZOD_TYPES as SUPPORTED_ZOD_TYPES_V3,
|
|
13
|
+
} from './schema-compatibility-v3';
|
|
14
|
+
import type {
|
|
15
|
+
UnsupportedZodType as UnsupportedZodTypeV3,
|
|
16
|
+
ShapeValue as ShapeValueV3,
|
|
17
|
+
StringCheckType,
|
|
18
|
+
NumberCheckType,
|
|
19
|
+
ArrayCheckType,
|
|
20
|
+
AllZodType as AllZodTypeV3,
|
|
21
|
+
} from './schema-compatibility-v3';
|
|
22
|
+
import {
|
|
23
|
+
SchemaCompatLayer as SchemaCompatLayerV4,
|
|
24
|
+
UNSUPPORTED_ZOD_TYPES as UNSUPPORTED_ZOD_TYPES_V4,
|
|
25
|
+
SUPPORTED_ZOD_TYPES as SUPPORTED_ZOD_TYPES_V4,
|
|
26
|
+
} from './schema-compatibility-v4';
|
|
27
|
+
import type {
|
|
28
|
+
UnsupportedZodType as UnsupportedZodTypeV4,
|
|
29
|
+
ShapeValue as ShapeValueV4,
|
|
30
|
+
AllZodType as AllZodTypeV4,
|
|
31
|
+
} from './schema-compatibility-v4';
|
|
32
|
+
|
|
33
|
+
// Define constraint types locally since they're not exported from v3/v4 files
|
|
114
34
|
type StringConstraints = {
|
|
115
35
|
minLength?: number;
|
|
116
36
|
maxLength?: number;
|
|
@@ -119,7 +39,7 @@ type StringConstraints = {
|
|
|
119
39
|
uuid?: boolean;
|
|
120
40
|
cuid?: boolean;
|
|
121
41
|
emoji?: boolean;
|
|
122
|
-
regex?: string;
|
|
42
|
+
regex?: { pattern: string; flags?: string };
|
|
123
43
|
};
|
|
124
44
|
|
|
125
45
|
type NumberConstraints = {
|
|
@@ -141,54 +61,13 @@ type DateConstraints = {
|
|
|
141
61
|
maxDate?: string;
|
|
142
62
|
dateFormat?: string;
|
|
143
63
|
};
|
|
64
|
+
import type { ModelInformation } from './types';
|
|
65
|
+
import { convertZodSchemaToAISDKSchema } from './utils';
|
|
144
66
|
|
|
145
|
-
export type ModelInformation = {
|
|
146
|
-
modelId: string;
|
|
147
|
-
provider: string;
|
|
148
|
-
supportsStructuredOutputs: boolean;
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Abstract base class for creating schema compatibility layers for different AI model providers.
|
|
153
|
-
*
|
|
154
|
-
* This class provides a framework for transforming Zod schemas to work with specific AI model
|
|
155
|
-
* provider requirements and limitations. Each provider may have different support levels for
|
|
156
|
-
* JSON Schema features, validation constraints, and data types.
|
|
157
|
-
*
|
|
158
|
-
* @abstract
|
|
159
|
-
*
|
|
160
|
-
* @example
|
|
161
|
-
* ```typescript
|
|
162
|
-
* import { SchemaCompatLayer } from '@mastra/schema-compat';
|
|
163
|
-
* import type { LanguageModelV1 } from 'ai';
|
|
164
|
-
*
|
|
165
|
-
* class CustomProviderCompat extends SchemaCompatLayer {
|
|
166
|
-
* constructor(model: LanguageModelV1) {
|
|
167
|
-
* super(model);
|
|
168
|
-
* }
|
|
169
|
-
*
|
|
170
|
-
* shouldApply(): boolean {
|
|
171
|
-
* return this.getModel().provider === 'custom-provider';
|
|
172
|
-
* }
|
|
173
|
-
*
|
|
174
|
-
* getSchemaTarget() {
|
|
175
|
-
* return 'jsonSchema7';
|
|
176
|
-
* }
|
|
177
|
-
*
|
|
178
|
-
* processZodType<T extends z.AnyZodObject>(value: z.ZodTypeAny): ShapeValue<T> {
|
|
179
|
-
* // Custom processing logic for this provider
|
|
180
|
-
* switch (value._def.typeName) {
|
|
181
|
-
* case 'ZodString':
|
|
182
|
-
* return this.defaultZodStringHandler(value, ['email', 'url']);
|
|
183
|
-
* default:
|
|
184
|
-
* return this.defaultUnsupportedZodTypeHandler(value);
|
|
185
|
-
* }
|
|
186
|
-
* }
|
|
187
|
-
* }
|
|
188
|
-
* ```
|
|
189
|
-
*/
|
|
190
67
|
export abstract class SchemaCompatLayer {
|
|
191
68
|
private model: ModelInformation;
|
|
69
|
+
private v3Layer: SchemaCompatLayerV3;
|
|
70
|
+
private v4Layer: SchemaCompatLayerV4;
|
|
192
71
|
|
|
193
72
|
/**
|
|
194
73
|
* Creates a new schema compatibility instance.
|
|
@@ -197,6 +76,8 @@ export abstract class SchemaCompatLayer {
|
|
|
197
76
|
*/
|
|
198
77
|
constructor(model: ModelInformation) {
|
|
199
78
|
this.model = model;
|
|
79
|
+
this.v3Layer = new SchemaCompatLayerV3(model, this);
|
|
80
|
+
this.v4Layer = new SchemaCompatLayerV4(model, this);
|
|
200
81
|
}
|
|
201
82
|
|
|
202
83
|
/**
|
|
@@ -208,6 +89,140 @@ export abstract class SchemaCompatLayer {
|
|
|
208
89
|
return this.model;
|
|
209
90
|
}
|
|
210
91
|
|
|
92
|
+
getUnsupportedZodTypes(v: ZodType): readonly string[] {
|
|
93
|
+
if ('_zod' in v) {
|
|
94
|
+
return this.v4Layer.getUnsupportedZodTypes();
|
|
95
|
+
} else {
|
|
96
|
+
return this.v3Layer.getUnsupportedZodTypes();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Type guard for optional Zod types
|
|
102
|
+
*/
|
|
103
|
+
isOptional(v: zV4.ZodType): v is zV4.ZodOptional<any>;
|
|
104
|
+
isOptional(v: zV3.ZodType): v is zV3.ZodOptional<any>;
|
|
105
|
+
isOptional(v: zV3.ZodType | zV4.ZodType) {
|
|
106
|
+
if ('_zod' in v) {
|
|
107
|
+
// @ts-expect-error - fix later
|
|
108
|
+
return this.v4Layer.isOptional(v);
|
|
109
|
+
} else {
|
|
110
|
+
return this.v3Layer.isOptional(v);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Type guard for object Zod types
|
|
116
|
+
*/
|
|
117
|
+
isObj(v: zV4.ZodType): v is zV4.ZodObject<any, any>;
|
|
118
|
+
isObj(v: zV3.ZodType): v is zV3.ZodObject<any, any, any, any, any>;
|
|
119
|
+
isObj(v: zV3.ZodType | zV4.ZodType) {
|
|
120
|
+
if ('_zod' in v) {
|
|
121
|
+
// @ts-expect-error - fix later
|
|
122
|
+
return this.v4Layer.isObj(v);
|
|
123
|
+
} else {
|
|
124
|
+
return this.v3Layer.isObj(v);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Type guard for null Zod types
|
|
130
|
+
*/
|
|
131
|
+
isNull(v: zV4.ZodType): v is zV4.ZodNull;
|
|
132
|
+
isNull(v: zV3.ZodType): v is zV3.ZodNull;
|
|
133
|
+
isNull(v: zV3.ZodType | zV4.ZodType) {
|
|
134
|
+
if ('_zod' in v) {
|
|
135
|
+
// @ts-expect-error - fix later
|
|
136
|
+
return this.v4Layer.isNull(v);
|
|
137
|
+
} else {
|
|
138
|
+
return this.v3Layer.isNull(v);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Type guard for array Zod types
|
|
144
|
+
*/
|
|
145
|
+
isArr(v: zV4.ZodType): v is zV4.ZodArray<any>;
|
|
146
|
+
isArr(v: zV3.ZodType): v is zV3.ZodArray<any, any>;
|
|
147
|
+
isArr(v: zV3.ZodType | zV4.ZodType) {
|
|
148
|
+
if ('_zod' in v) {
|
|
149
|
+
// @ts-expect-error - fix later
|
|
150
|
+
return this.v4Layer.isArr(v);
|
|
151
|
+
} else {
|
|
152
|
+
return this.v3Layer.isArr(v);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Type guard for union Zod types
|
|
158
|
+
*/
|
|
159
|
+
isUnion(v: zV4.ZodType): v is zV4.ZodUnion<[zV4.ZodType, ...zV4.ZodType[]]>;
|
|
160
|
+
isUnion(v: zV3.ZodType): v is zV3.ZodUnion<[zV3.ZodType, ...zV3.ZodType[]]>;
|
|
161
|
+
isUnion(v: zV3.ZodType | zV4.ZodType) {
|
|
162
|
+
if ('_zod' in v) {
|
|
163
|
+
// @ts-expect-error - fix later
|
|
164
|
+
return this.v4Layer.isUnion(v);
|
|
165
|
+
} else {
|
|
166
|
+
return this.v3Layer.isUnion(v);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Type guard for string Zod types
|
|
172
|
+
*/
|
|
173
|
+
isString(v: zV4.ZodType): v is zV4.ZodString;
|
|
174
|
+
isString(v: zV3.ZodType): v is zV3.ZodString;
|
|
175
|
+
isString(v: zV3.ZodType | zV4.ZodType) {
|
|
176
|
+
if ('_zod' in v) {
|
|
177
|
+
// @ts-expect-error - fix later
|
|
178
|
+
return this.v4Layer.isString(v);
|
|
179
|
+
} else {
|
|
180
|
+
return this.v3Layer.isString(v);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Type guard for number Zod types
|
|
186
|
+
*/
|
|
187
|
+
isNumber(v: zV4.ZodType): v is zV4.ZodNumber;
|
|
188
|
+
isNumber(v: zV3.ZodType): v is zV3.ZodNumber;
|
|
189
|
+
isNumber(v: zV3.ZodType | zV4.ZodType) {
|
|
190
|
+
if ('_zod' in v) {
|
|
191
|
+
// @ts-expect-error - fix later
|
|
192
|
+
return this.v4Layer.isNumber(v);
|
|
193
|
+
} else {
|
|
194
|
+
return this.v3Layer.isNumber(v);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Type guard for date Zod types
|
|
200
|
+
*/
|
|
201
|
+
isDate(v: zV4.ZodType): v is zV4.ZodDate;
|
|
202
|
+
isDate(v: zV3.ZodType): v is zV3.ZodDate;
|
|
203
|
+
isDate(v: zV3.ZodType | zV4.ZodType) {
|
|
204
|
+
if ('_zod' in v) {
|
|
205
|
+
// @ts-expect-error - fix later
|
|
206
|
+
return this.v4Layer.isDate(v);
|
|
207
|
+
} else {
|
|
208
|
+
return this.v3Layer.isDate(v);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Type guard for default Zod types
|
|
214
|
+
*/
|
|
215
|
+
isDefault(v: zV4.ZodType): v is zV4.ZodDefault<any>;
|
|
216
|
+
isDefault(v: zV3.ZodType): v is zV3.ZodDefault<any>;
|
|
217
|
+
isDefault(v: zV3.ZodType | zV4.ZodType) {
|
|
218
|
+
if ('_zod' in v) {
|
|
219
|
+
// @ts-expect-error - fix later
|
|
220
|
+
return this.v4Layer.isDefault(v);
|
|
221
|
+
} else {
|
|
222
|
+
return this.v3Layer.isDefault(v);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
211
226
|
/**
|
|
212
227
|
* Determines whether this compatibility layer should be applied for the current model.
|
|
213
228
|
*
|
|
@@ -231,7 +246,9 @@ export abstract class SchemaCompatLayer {
|
|
|
231
246
|
* @returns The processed Zod type
|
|
232
247
|
* @abstract
|
|
233
248
|
*/
|
|
234
|
-
abstract processZodType(value:
|
|
249
|
+
abstract processZodType(value: zV4.ZodType): zV4.ZodType;
|
|
250
|
+
abstract processZodType(value: zV3.ZodType): zV3.ZodType;
|
|
251
|
+
abstract processZodType(value: zV4.ZodType | zV3.ZodType): zV4.ZodType | zV3.ZodType;
|
|
235
252
|
|
|
236
253
|
/**
|
|
237
254
|
* Default handler for Zod object types. Recursively processes all properties in the object.
|
|
@@ -240,32 +257,22 @@ export abstract class SchemaCompatLayer {
|
|
|
240
257
|
* @returns The processed Zod object
|
|
241
258
|
*/
|
|
242
259
|
public defaultZodObjectHandler(
|
|
243
|
-
value: ZodObject<any, any
|
|
260
|
+
value: zV4.ZodObject<any, any>,
|
|
261
|
+
options?: { passthrough?: boolean },
|
|
262
|
+
): zV4.ZodObject<any, any>;
|
|
263
|
+
public defaultZodObjectHandler(
|
|
264
|
+
value: zV3.ZodObject<any, any>,
|
|
265
|
+
options?: { passthrough?: boolean },
|
|
266
|
+
): zV3.ZodObject<any, any>;
|
|
267
|
+
public defaultZodObjectHandler(
|
|
268
|
+
value: zV3.ZodObject<any, any, any, any, any> | zV4.ZodObject<any, any>,
|
|
244
269
|
options: { passthrough?: boolean } = { passthrough: true },
|
|
245
|
-
): ZodObject<any, any, any> {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
let result: ZodObject<any, any, any> = z.object(processedShape);
|
|
252
|
-
|
|
253
|
-
if (value._def.unknownKeys === 'strict') {
|
|
254
|
-
result = result.strict();
|
|
255
|
-
}
|
|
256
|
-
if (value._def.catchall && !(value._def.catchall instanceof z.ZodNever)) {
|
|
257
|
-
result = result.catchall(value._def.catchall);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
if (value.description) {
|
|
261
|
-
result = result.describe(value.description);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
if (options.passthrough && value._def.unknownKeys === 'passthrough') {
|
|
265
|
-
result = result.passthrough();
|
|
270
|
+
): zV3.ZodObject<any, any, any, any, any> | zV4.ZodObject<any, any> {
|
|
271
|
+
if ('_zod' in value) {
|
|
272
|
+
return this.v4Layer.defaultZodObjectHandler(value, options);
|
|
273
|
+
} else {
|
|
274
|
+
return this.v3Layer.defaultZodObjectHandler(value, options);
|
|
266
275
|
}
|
|
267
|
-
|
|
268
|
-
return result;
|
|
269
276
|
}
|
|
270
277
|
|
|
271
278
|
/**
|
|
@@ -287,11 +294,8 @@ export abstract class SchemaCompatLayer {
|
|
|
287
294
|
| DateConstraints
|
|
288
295
|
| { defaultValue?: unknown },
|
|
289
296
|
): string | undefined {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
} else {
|
|
293
|
-
return description;
|
|
294
|
-
}
|
|
297
|
+
// This method doesn't depend on Zod version, so we can use either layer
|
|
298
|
+
return this.v3Layer.mergeParameterDescription(description, constraints);
|
|
295
299
|
}
|
|
296
300
|
|
|
297
301
|
/**
|
|
@@ -302,14 +306,26 @@ export abstract class SchemaCompatLayer {
|
|
|
302
306
|
* @returns The original value if not in the throw list
|
|
303
307
|
* @throws Error if the type is in the unsupported list
|
|
304
308
|
*/
|
|
305
|
-
public defaultUnsupportedZodTypeHandler<T extends
|
|
306
|
-
value:
|
|
307
|
-
throwOnTypes
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
309
|
+
public defaultUnsupportedZodTypeHandler<T extends zV4.ZodObject | zV3.AnyZodObject>(
|
|
310
|
+
value: T,
|
|
311
|
+
throwOnTypes?: T extends zV4.ZodObject
|
|
312
|
+
? UnsupportedZodTypeV4[]
|
|
313
|
+
: T extends zV3.AnyZodObject
|
|
314
|
+
? UnsupportedZodTypeV3[]
|
|
315
|
+
: never,
|
|
316
|
+
): T extends zV4.ZodObject ? ShapeValueV4<T> : T extends zV3.AnyZodObject ? ShapeValueV3<T> : never {
|
|
317
|
+
if ('_zod' in value) {
|
|
318
|
+
return this.v4Layer.defaultUnsupportedZodTypeHandler(
|
|
319
|
+
// @ts-expect-error - fix later
|
|
320
|
+
value,
|
|
321
|
+
(throwOnTypes ?? UNSUPPORTED_ZOD_TYPES_V4) as typeof UNSUPPORTED_ZOD_TYPES_V4,
|
|
322
|
+
);
|
|
323
|
+
} else {
|
|
324
|
+
return this.v3Layer.defaultUnsupportedZodTypeHandler(
|
|
325
|
+
value,
|
|
326
|
+
(throwOnTypes ?? UNSUPPORTED_ZOD_TYPES_V3) as typeof UNSUPPORTED_ZOD_TYPES_V3,
|
|
327
|
+
);
|
|
311
328
|
}
|
|
312
|
-
return value as ShapeValue<T>;
|
|
313
329
|
}
|
|
314
330
|
|
|
315
331
|
/**
|
|
@@ -319,46 +335,20 @@ export abstract class SchemaCompatLayer {
|
|
|
319
335
|
* @param handleChecks - Array constraints to convert to descriptions vs keep as validation
|
|
320
336
|
* @returns The processed Zod array
|
|
321
337
|
*/
|
|
338
|
+
public defaultZodArrayHandler(value: zV4.ZodArray<any>, handleChecks?: readonly ArrayCheckType[]): zV4.ZodArray<any>;
|
|
339
|
+
public defaultZodArrayHandler(
|
|
340
|
+
value: zV3.ZodArray<any, any>,
|
|
341
|
+
handleChecks?: readonly ArrayCheckType[],
|
|
342
|
+
): zV3.ZodArray<any, any>;
|
|
322
343
|
public defaultZodArrayHandler(
|
|
323
|
-
value: ZodArray<any, any>,
|
|
344
|
+
value: zV4.ZodArray<any> | zV3.ZodArray<any, any>,
|
|
324
345
|
handleChecks: readonly ArrayCheckType[] = ALL_ARRAY_CHECKS,
|
|
325
|
-
): ZodArray<any, any> {
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
const constraints: ArrayConstraints = {};
|
|
332
|
-
|
|
333
|
-
if (zodArrayDef.minLength?.value !== undefined) {
|
|
334
|
-
if (handleChecks.includes('min')) {
|
|
335
|
-
constraints.minLength = zodArrayDef.minLength.value;
|
|
336
|
-
} else {
|
|
337
|
-
result = result.min(zodArrayDef.minLength.value);
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
if (zodArrayDef.maxLength?.value !== undefined) {
|
|
342
|
-
if (handleChecks.includes('max')) {
|
|
343
|
-
constraints.maxLength = zodArrayDef.maxLength.value;
|
|
344
|
-
} else {
|
|
345
|
-
result = result.max(zodArrayDef.maxLength.value);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
if (zodArrayDef.exactLength?.value !== undefined) {
|
|
350
|
-
if (handleChecks.includes('length')) {
|
|
351
|
-
constraints.exactLength = zodArrayDef.exactLength.value;
|
|
352
|
-
} else {
|
|
353
|
-
result = result.length(zodArrayDef.exactLength.value);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
const description = this.mergeParameterDescription(value.description, constraints);
|
|
358
|
-
if (description) {
|
|
359
|
-
result = result.describe(description);
|
|
346
|
+
): zV4.ZodArray<any> | zV3.ZodArray<any, any> {
|
|
347
|
+
if ('_zod' in value) {
|
|
348
|
+
return this.v4Layer.defaultZodArrayHandler(value, handleChecks);
|
|
349
|
+
} else {
|
|
350
|
+
return this.v3Layer.defaultZodArrayHandler(value, handleChecks);
|
|
360
351
|
}
|
|
361
|
-
return result;
|
|
362
352
|
}
|
|
363
353
|
|
|
364
354
|
/**
|
|
@@ -368,14 +358,17 @@ export abstract class SchemaCompatLayer {
|
|
|
368
358
|
* @returns The processed Zod union
|
|
369
359
|
* @throws Error if union has fewer than 2 options
|
|
370
360
|
*/
|
|
371
|
-
public defaultZodUnionHandler(value: ZodUnion<[
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
361
|
+
public defaultZodUnionHandler(value: zV4.ZodUnion<[zV4.ZodType, ...zV4.ZodType[]]>): zV4.ZodType;
|
|
362
|
+
public defaultZodUnionHandler(value: zV3.ZodUnion<[zV3.ZodType, ...zV3.ZodType[]]>): zV3.ZodType;
|
|
363
|
+
public defaultZodUnionHandler(
|
|
364
|
+
value: zV4.ZodUnion<[zV4.ZodType, ...zV4.ZodType[]]> | zV3.ZodUnion<[zV3.ZodType, ...zV3.ZodType[]]>,
|
|
365
|
+
): zV4.ZodType | zV3.ZodType {
|
|
366
|
+
if ('_zod' in value) {
|
|
367
|
+
// @ts-expect-error - fix later
|
|
368
|
+
return this.v4Layer.defaultZodUnionHandler(value);
|
|
369
|
+
} else {
|
|
370
|
+
return this.v3Layer.defaultZodUnionHandler(value);
|
|
377
371
|
}
|
|
378
|
-
return result;
|
|
379
372
|
}
|
|
380
373
|
|
|
381
374
|
/**
|
|
@@ -385,65 +378,17 @@ export abstract class SchemaCompatLayer {
|
|
|
385
378
|
* @param handleChecks - String constraints to convert to descriptions vs keep as validation
|
|
386
379
|
* @returns The processed Zod string
|
|
387
380
|
*/
|
|
381
|
+
public defaultZodStringHandler(value: zV4.ZodString, handleChecks?: readonly StringCheckType[]): zV4.ZodString;
|
|
382
|
+
public defaultZodStringHandler(value: zV3.ZodString, handleChecks?: readonly StringCheckType[]): zV3.ZodString;
|
|
388
383
|
public defaultZodStringHandler(
|
|
389
|
-
value: ZodString,
|
|
384
|
+
value: zV4.ZodString | zV3.ZodString,
|
|
390
385
|
handleChecks: readonly StringCheckType[] = ALL_STRING_CHECKS,
|
|
391
|
-
): ZodString {
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
for (const check of checks) {
|
|
397
|
-
if ('kind' in check) {
|
|
398
|
-
if (handleChecks.includes(check.kind as StringCheckType)) {
|
|
399
|
-
switch (check.kind) {
|
|
400
|
-
case 'regex': {
|
|
401
|
-
constraints.regex = `A string that must match the regex pattern: ${check.regex.source}, with flags: ${check.regex.flags}`;
|
|
402
|
-
break;
|
|
403
|
-
}
|
|
404
|
-
case 'emoji': {
|
|
405
|
-
constraints.emoji = true;
|
|
406
|
-
break;
|
|
407
|
-
}
|
|
408
|
-
case 'email': {
|
|
409
|
-
constraints.email = true;
|
|
410
|
-
break;
|
|
411
|
-
}
|
|
412
|
-
case 'url': {
|
|
413
|
-
constraints.url = true;
|
|
414
|
-
break;
|
|
415
|
-
}
|
|
416
|
-
case 'uuid': {
|
|
417
|
-
constraints.uuid = true;
|
|
418
|
-
break;
|
|
419
|
-
}
|
|
420
|
-
case 'cuid': {
|
|
421
|
-
constraints.cuid = true;
|
|
422
|
-
break;
|
|
423
|
-
}
|
|
424
|
-
case 'min': {
|
|
425
|
-
constraints.minLength = check.value;
|
|
426
|
-
break;
|
|
427
|
-
}
|
|
428
|
-
case 'max': {
|
|
429
|
-
constraints.maxLength = check.value;
|
|
430
|
-
break;
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
} else {
|
|
434
|
-
newChecks.push(check);
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
let result = z.string();
|
|
439
|
-
for (const check of newChecks) {
|
|
440
|
-
result = result._addCheck(check);
|
|
441
|
-
}
|
|
442
|
-
const description = this.mergeParameterDescription(value.description, constraints);
|
|
443
|
-
if (description) {
|
|
444
|
-
result = result.describe(description);
|
|
386
|
+
): zV4.ZodString | zV3.ZodString {
|
|
387
|
+
if ('_zod' in value) {
|
|
388
|
+
return this.v4Layer.defaultZodStringHandler(value);
|
|
389
|
+
} else {
|
|
390
|
+
return this.v3Layer.defaultZodStringHandler(value, handleChecks);
|
|
445
391
|
}
|
|
446
|
-
return result;
|
|
447
392
|
}
|
|
448
393
|
|
|
449
394
|
/**
|
|
@@ -453,60 +398,17 @@ export abstract class SchemaCompatLayer {
|
|
|
453
398
|
* @param handleChecks - Number constraints to convert to descriptions vs keep as validation
|
|
454
399
|
* @returns The processed Zod number
|
|
455
400
|
*/
|
|
401
|
+
public defaultZodNumberHandler(value: zV4.ZodNumber, handleChecks?: readonly NumberCheckType[]): zV4.ZodNumber;
|
|
402
|
+
public defaultZodNumberHandler(value: zV3.ZodNumber, handleChecks?: readonly NumberCheckType[]): zV3.ZodNumber;
|
|
456
403
|
public defaultZodNumberHandler(
|
|
457
|
-
value: ZodNumber,
|
|
404
|
+
value: zV4.ZodNumber | zV3.ZodNumber,
|
|
458
405
|
handleChecks: readonly NumberCheckType[] = ALL_NUMBER_CHECKS,
|
|
459
|
-
): ZodNumber {
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
for (const check of checks) {
|
|
465
|
-
if ('kind' in check) {
|
|
466
|
-
if (handleChecks.includes(check.kind as NumberCheckType)) {
|
|
467
|
-
switch (check.kind) {
|
|
468
|
-
case 'min':
|
|
469
|
-
if (check.inclusive) {
|
|
470
|
-
constraints.gte = check.value;
|
|
471
|
-
} else {
|
|
472
|
-
constraints.gt = check.value;
|
|
473
|
-
}
|
|
474
|
-
break;
|
|
475
|
-
case 'max':
|
|
476
|
-
if (check.inclusive) {
|
|
477
|
-
constraints.lte = check.value;
|
|
478
|
-
} else {
|
|
479
|
-
constraints.lt = check.value;
|
|
480
|
-
}
|
|
481
|
-
break;
|
|
482
|
-
case 'multipleOf': {
|
|
483
|
-
constraints.multipleOf = check.value;
|
|
484
|
-
break;
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
} else {
|
|
488
|
-
newChecks.push(check);
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
let result = z.number();
|
|
493
|
-
for (const check of newChecks) {
|
|
494
|
-
switch (check.kind) {
|
|
495
|
-
case 'int':
|
|
496
|
-
result = result.int();
|
|
497
|
-
break;
|
|
498
|
-
case 'finite':
|
|
499
|
-
result = result.finite();
|
|
500
|
-
break;
|
|
501
|
-
default:
|
|
502
|
-
result = result._addCheck(check);
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
const description = this.mergeParameterDescription(value.description, constraints);
|
|
506
|
-
if (description) {
|
|
507
|
-
result = result.describe(description);
|
|
406
|
+
): zV4.ZodNumber | zV3.ZodNumber {
|
|
407
|
+
if ('_zod' in value) {
|
|
408
|
+
return this.v4Layer.defaultZodNumberHandler(value);
|
|
409
|
+
} else {
|
|
410
|
+
return this.v3Layer.defaultZodNumberHandler(value, handleChecks);
|
|
508
411
|
}
|
|
509
|
-
return result;
|
|
510
412
|
}
|
|
511
413
|
|
|
512
414
|
/**
|
|
@@ -515,38 +417,14 @@ export abstract class SchemaCompatLayer {
|
|
|
515
417
|
* @param value - The Zod date to process
|
|
516
418
|
* @returns A Zod string schema representing the date in ISO format
|
|
517
419
|
*/
|
|
518
|
-
public defaultZodDateHandler(value: ZodDate): ZodString
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
switch (check.kind) {
|
|
526
|
-
case 'min':
|
|
527
|
-
const minDate = new Date(check.value);
|
|
528
|
-
if (!isNaN(minDate.getTime())) {
|
|
529
|
-
constraints.minDate = minDate.toISOString();
|
|
530
|
-
}
|
|
531
|
-
break;
|
|
532
|
-
case 'max':
|
|
533
|
-
const maxDate = new Date(check.value);
|
|
534
|
-
if (!isNaN(maxDate.getTime())) {
|
|
535
|
-
constraints.maxDate = maxDate.toISOString();
|
|
536
|
-
}
|
|
537
|
-
break;
|
|
538
|
-
default:
|
|
539
|
-
newChecks.push(check);
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
constraints.dateFormat = 'date-time';
|
|
544
|
-
let result = z.string().describe('date-time');
|
|
545
|
-
const description = this.mergeParameterDescription(value.description, constraints);
|
|
546
|
-
if (description) {
|
|
547
|
-
result = result.describe(description);
|
|
420
|
+
public defaultZodDateHandler(value: zV4.ZodDate): zV4.ZodString;
|
|
421
|
+
public defaultZodDateHandler(value: zV3.ZodDate): zV3.ZodString;
|
|
422
|
+
public defaultZodDateHandler(value: zV4.ZodDate | zV3.ZodDate): zV4.ZodString | zV3.ZodString {
|
|
423
|
+
if ('_zod' in value) {
|
|
424
|
+
return this.v4Layer.defaultZodDateHandler(value);
|
|
425
|
+
} else {
|
|
426
|
+
return this.v3Layer.defaultZodDateHandler(value);
|
|
548
427
|
}
|
|
549
|
-
return result;
|
|
550
428
|
}
|
|
551
429
|
|
|
552
430
|
/**
|
|
@@ -556,14 +434,16 @@ export abstract class SchemaCompatLayer {
|
|
|
556
434
|
* @param handleTypes - Types that should be processed vs passed through
|
|
557
435
|
* @returns The processed Zod optional
|
|
558
436
|
*/
|
|
437
|
+
public defaultZodOptionalHandler(value: zV4.ZodOptional<any>, handleTypes?: readonly AllZodTypeV4[]): zV4.ZodType;
|
|
438
|
+
public defaultZodOptionalHandler(value: zV3.ZodOptional<any>, handleTypes?: readonly AllZodTypeV3[]): zV3.ZodType;
|
|
559
439
|
public defaultZodOptionalHandler(
|
|
560
|
-
value: ZodOptional<any>,
|
|
561
|
-
handleTypes
|
|
562
|
-
):
|
|
563
|
-
if (
|
|
564
|
-
return this.
|
|
440
|
+
value: zV4.ZodOptional<any> | zV3.ZodOptional<any>,
|
|
441
|
+
handleTypes?: readonly AllZodTypeV3[] | readonly AllZodTypeV4[],
|
|
442
|
+
): zV4.ZodType | zV3.ZodType {
|
|
443
|
+
if ('_zod' in value) {
|
|
444
|
+
return this.v4Layer.defaultZodOptionalHandler(value, handleTypes ?? SUPPORTED_ZOD_TYPES_V4);
|
|
565
445
|
} else {
|
|
566
|
-
return value;
|
|
446
|
+
return this.v3Layer.defaultZodOptionalHandler(value, handleTypes ?? SUPPORTED_ZOD_TYPES_V3);
|
|
567
447
|
}
|
|
568
448
|
}
|
|
569
449
|
|
|
@@ -573,7 +453,7 @@ export abstract class SchemaCompatLayer {
|
|
|
573
453
|
* @param zodSchema - The Zod object schema to process
|
|
574
454
|
* @returns An AI SDK Schema with provider-specific compatibility applied
|
|
575
455
|
*/
|
|
576
|
-
public processToAISDKSchema(zodSchema:
|
|
456
|
+
public processToAISDKSchema(zodSchema: zV3.ZodSchema | zV4.ZodType): Schema {
|
|
577
457
|
const processedSchema = this.processZodType(zodSchema);
|
|
578
458
|
|
|
579
459
|
return convertZodSchemaToAISDKSchema(processedSchema, this.getSchemaTarget());
|
|
@@ -585,7 +465,7 @@ export abstract class SchemaCompatLayer {
|
|
|
585
465
|
* @param zodSchema - The Zod object schema to process
|
|
586
466
|
* @returns A JSONSchema7 object with provider-specific compatibility applied
|
|
587
467
|
*/
|
|
588
|
-
public processToJSONSchema(zodSchema:
|
|
468
|
+
public processToJSONSchema(zodSchema: zV3.ZodSchema | zV4.ZodType): JSONSchema7 {
|
|
589
469
|
return this.processToAISDKSchema(zodSchema).jsonSchema;
|
|
590
470
|
}
|
|
591
471
|
}
|