@mastra/schema-compat 0.10.6-alpha.0 → 0.10.6-alpha.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.
Files changed (68) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +41 -0
  3. package/README.md +0 -4
  4. package/dist/chunk-FTKGHMGD.js +27 -0
  5. package/dist/chunk-FTKGHMGD.js.map +1 -0
  6. package/dist/chunk-LNR4XKDU.cjs +33 -0
  7. package/dist/chunk-LNR4XKDU.cjs.map +1 -0
  8. package/dist/index.cjs +843 -84
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.d.ts +10 -8
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +842 -85
  13. package/dist/index.js.map +1 -1
  14. package/dist/provider-compats/anthropic.d.ts +7 -5
  15. package/dist/provider-compats/anthropic.d.ts.map +1 -1
  16. package/dist/provider-compats/deepseek.d.ts +7 -5
  17. package/dist/provider-compats/deepseek.d.ts.map +1 -1
  18. package/dist/provider-compats/google.d.ts +7 -5
  19. package/dist/provider-compats/google.d.ts.map +1 -1
  20. package/dist/provider-compats/meta.d.ts +7 -5
  21. package/dist/provider-compats/meta.d.ts.map +1 -1
  22. package/dist/provider-compats/openai-reasoning.d.ts +7 -5
  23. package/dist/provider-compats/openai-reasoning.d.ts.map +1 -1
  24. package/dist/provider-compats/openai.d.ts +7 -5
  25. package/dist/provider-compats/openai.d.ts.map +1 -1
  26. package/dist/schema-compatibility-v3.d.ts +319 -0
  27. package/dist/schema-compatibility-v3.d.ts.map +1 -0
  28. package/dist/schema-compatibility-v4.d.ts +310 -0
  29. package/dist/schema-compatibility-v4.d.ts.map +1 -0
  30. package/dist/schema-compatibility.d.ts +79 -131
  31. package/dist/schema-compatibility.d.ts.map +1 -1
  32. package/dist/types.d.ts +6 -0
  33. package/dist/types.d.ts.map +1 -0
  34. package/dist/utils-test-suite.d.ts +2 -0
  35. package/dist/utils-test-suite.d.ts.map +1 -0
  36. package/dist/utils.d.ts +17 -5
  37. package/dist/utils.d.ts.map +1 -1
  38. package/dist/zod-to-json.cjs +12 -0
  39. package/dist/zod-to-json.cjs.map +1 -0
  40. package/dist/zod-to-json.d.ts +6 -0
  41. package/dist/zod-to-json.d.ts.map +1 -0
  42. package/dist/zod-to-json.js +3 -0
  43. package/dist/zod-to-json.js.map +1 -0
  44. package/dist/zodTypes.d.ts +21 -0
  45. package/dist/zodTypes.d.ts.map +1 -0
  46. package/package.json +16 -6
  47. package/src/index.ts +4 -3
  48. package/src/provider-compats/anthropic.ts +30 -13
  49. package/src/provider-compats/deepseek.ts +15 -10
  50. package/src/provider-compats/google.ts +19 -33
  51. package/src/provider-compats/meta.ts +16 -11
  52. package/src/provider-compats/openai-reasoning.ts +24 -26
  53. package/src/provider-compats/openai.ts +23 -14
  54. package/src/provider-compats.test.ts +120 -25
  55. package/src/schema-compatibility-v3.ts +664 -0
  56. package/src/schema-compatibility-v4.test.ts +476 -0
  57. package/src/schema-compatibility-v4.ts +706 -0
  58. package/src/schema-compatibility.test.ts +9 -9
  59. package/src/schema-compatibility.ts +266 -383
  60. package/src/types.ts +5 -0
  61. package/src/utils-test-suite.ts +467 -0
  62. package/src/utils-v3.test.ts +9 -0
  63. package/src/utils-v4.test.ts +9 -0
  64. package/src/utils.ts +30 -24
  65. package/src/zod-to-json.ts +27 -0
  66. package/src/zodTypes.ts +56 -0
  67. package/tsup.config.ts +8 -3
  68. package/src/utils.test.ts +0 -434
@@ -1,116 +1,36 @@
1
- import type { Schema, LanguageModelV1 } from 'ai';
1
+ import type { Schema } from 'ai';
2
2
  import type { JSONSchema7 } from 'json-schema';
3
- import { z, ZodOptional, ZodObject, ZodArray, ZodUnion, ZodString, ZodNumber, ZodDate, ZodDefault, ZodNull } from 'zod';
4
- import type { ZodTypeAny } from 'zod';
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 { convertZodSchemaToAISDKSchema } from './utils';
7
-
8
- /**
9
- * All supported string validation check types that can be processed or converted to descriptions.
10
- * @constant
11
- */
12
- export const ALL_STRING_CHECKS = ['regex', 'emoji', 'email', 'url', 'uuid', 'cuid', 'min', 'max'] as const;
13
-
14
- /**
15
- * All supported number validation check types that can be processed or converted to descriptions.
16
- * @constant
17
- */
18
- export const ALL_NUMBER_CHECKS = [
19
- 'min', // gte internally
20
- 'max', // lte internally
21
- 'multipleOf',
22
- ] as const;
23
-
24
- /**
25
- * All supported array validation check types that can be processed or converted to descriptions.
26
- * @constant
27
- */
28
- export const ALL_ARRAY_CHECKS = ['min', 'max', 'length'] as const;
29
-
30
- export const isOptional = (v: ZodTypeAny): v is ZodOptional<any> => v instanceof ZodOptional;
31
- export const isObj = (v: ZodTypeAny): v is ZodObject<any, any, any> => v instanceof ZodObject;
32
- export const isNull = (v: ZodTypeAny): v is ZodNull => v instanceof ZodNull;
33
- export const isArr = (v: ZodTypeAny): v is ZodArray<any, any> => v instanceof ZodArray;
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;
@@ -141,56 +61,23 @@ 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
- /**
146
- * Abstract base class for creating schema compatibility layers for different AI model providers.
147
- *
148
- * This class provides a framework for transforming Zod schemas to work with specific AI model
149
- * provider requirements and limitations. Each provider may have different support levels for
150
- * JSON Schema features, validation constraints, and data types.
151
- *
152
- * @abstract
153
- *
154
- * @example
155
- * ```typescript
156
- * import { SchemaCompatLayer } from '@mastra/schema-compat';
157
- * import type { LanguageModelV1 } from 'ai';
158
- *
159
- * class CustomProviderCompat extends SchemaCompatLayer {
160
- * constructor(model: LanguageModelV1) {
161
- * super(model);
162
- * }
163
- *
164
- * shouldApply(): boolean {
165
- * return this.getModel().provider === 'custom-provider';
166
- * }
167
- *
168
- * getSchemaTarget() {
169
- * return 'jsonSchema7';
170
- * }
171
- *
172
- * processZodType<T extends z.AnyZodObject>(value: z.ZodTypeAny): ShapeValue<T> {
173
- * // Custom processing logic for this provider
174
- * switch (value._def.typeName) {
175
- * case 'ZodString':
176
- * return this.defaultZodStringHandler(value, ['email', 'url']);
177
- * default:
178
- * return this.defaultUnsupportedZodTypeHandler(value);
179
- * }
180
- * }
181
- * }
182
- * ```
183
- */
184
67
  export abstract class SchemaCompatLayer {
185
- private model: LanguageModelV1;
68
+ private model: ModelInformation;
69
+ private v3Layer: SchemaCompatLayerV3;
70
+ private v4Layer: SchemaCompatLayerV4;
186
71
 
187
72
  /**
188
73
  * Creates a new schema compatibility instance.
189
74
  *
190
75
  * @param model - The language model this compatibility layer applies to
191
76
  */
192
- constructor(model: LanguageModelV1) {
77
+ constructor(model: ModelInformation) {
193
78
  this.model = model;
79
+ this.v3Layer = new SchemaCompatLayerV3(model, this);
80
+ this.v4Layer = new SchemaCompatLayerV4(model, this);
194
81
  }
195
82
 
196
83
  /**
@@ -198,10 +85,144 @@ export abstract class SchemaCompatLayer {
198
85
  *
199
86
  * @returns The language model instance
200
87
  */
201
- getModel(): LanguageModelV1 {
88
+ getModel(): ModelInformation {
202
89
  return this.model;
203
90
  }
204
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
+
205
226
  /**
206
227
  * Determines whether this compatibility layer should be applied for the current model.
207
228
  *
@@ -225,7 +246,9 @@ export abstract class SchemaCompatLayer {
225
246
  * @returns The processed Zod type
226
247
  * @abstract
227
248
  */
228
- abstract processZodType(value: ZodTypeAny): ZodTypeAny;
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;
229
252
 
230
253
  /**
231
254
  * Default handler for Zod object types. Recursively processes all properties in the object.
@@ -234,32 +257,22 @@ export abstract class SchemaCompatLayer {
234
257
  * @returns The processed Zod object
235
258
  */
236
259
  public defaultZodObjectHandler(
237
- value: ZodObject<any, 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>,
238
269
  options: { passthrough?: boolean } = { passthrough: true },
239
- ): ZodObject<any, any, any> {
240
- const processedShape = Object.entries(value.shape).reduce<Record<string, ZodTypeAny>>((acc, [key, propValue]) => {
241
- acc[key] = this.processZodType(propValue as ZodTypeAny);
242
- return acc;
243
- }, {});
244
-
245
- let result: ZodObject<any, any, any> = z.object(processedShape);
246
-
247
- if (value._def.unknownKeys === 'strict') {
248
- result = result.strict();
249
- }
250
- if (value._def.catchall && !(value._def.catchall instanceof z.ZodNever)) {
251
- result = result.catchall(value._def.catchall);
252
- }
253
-
254
- if (value.description) {
255
- result = result.describe(value.description);
256
- }
257
-
258
- if (options.passthrough && value._def.unknownKeys === 'passthrough') {
259
- 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);
260
275
  }
261
-
262
- return result;
263
276
  }
264
277
 
265
278
  /**
@@ -281,11 +294,8 @@ export abstract class SchemaCompatLayer {
281
294
  | DateConstraints
282
295
  | { defaultValue?: unknown },
283
296
  ): string | undefined {
284
- if (Object.keys(constraints).length > 0) {
285
- return (description ? description + '\n' : '') + JSON.stringify(constraints);
286
- } else {
287
- return description;
288
- }
297
+ // This method doesn't depend on Zod version, so we can use either layer
298
+ return this.v3Layer.mergeParameterDescription(description, constraints);
289
299
  }
290
300
 
291
301
  /**
@@ -296,14 +306,26 @@ export abstract class SchemaCompatLayer {
296
306
  * @returns The original value if not in the throw list
297
307
  * @throws Error if the type is in the unsupported list
298
308
  */
299
- public defaultUnsupportedZodTypeHandler<T extends z.AnyZodObject>(
300
- value: z.ZodTypeAny,
301
- throwOnTypes: readonly UnsupportedZodType[] = UNSUPPORTED_ZOD_TYPES,
302
- ): ShapeValue<T> {
303
- if (throwOnTypes.includes(value._def?.typeName as UnsupportedZodType)) {
304
- throw new Error(`${this.model.modelId} does not support zod type: ${value._def?.typeName}`);
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
+ );
305
328
  }
306
- return value as ShapeValue<T>;
307
329
  }
308
330
 
309
331
  /**
@@ -313,46 +335,20 @@ export abstract class SchemaCompatLayer {
313
335
  * @param handleChecks - Array constraints to convert to descriptions vs keep as validation
314
336
  * @returns The processed Zod array
315
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>;
316
343
  public defaultZodArrayHandler(
317
- value: ZodArray<any, any>,
344
+ value: zV4.ZodArray<any> | zV3.ZodArray<any, any>,
318
345
  handleChecks: readonly ArrayCheckType[] = ALL_ARRAY_CHECKS,
319
- ): ZodArray<any, any> {
320
- const zodArrayDef = value._def;
321
- const processedType = this.processZodType(zodArrayDef.type);
322
-
323
- let result = z.array(processedType);
324
-
325
- const constraints: ArrayConstraints = {};
326
-
327
- if (zodArrayDef.minLength?.value !== undefined) {
328
- if (handleChecks.includes('min')) {
329
- constraints.minLength = zodArrayDef.minLength.value;
330
- } else {
331
- result = result.min(zodArrayDef.minLength.value);
332
- }
333
- }
334
-
335
- if (zodArrayDef.maxLength?.value !== undefined) {
336
- if (handleChecks.includes('max')) {
337
- constraints.maxLength = zodArrayDef.maxLength.value;
338
- } else {
339
- result = result.max(zodArrayDef.maxLength.value);
340
- }
341
- }
342
-
343
- if (zodArrayDef.exactLength?.value !== undefined) {
344
- if (handleChecks.includes('length')) {
345
- constraints.exactLength = zodArrayDef.exactLength.value;
346
- } else {
347
- result = result.length(zodArrayDef.exactLength.value);
348
- }
349
- }
350
-
351
- const description = this.mergeParameterDescription(value.description, constraints);
352
- if (description) {
353
- 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);
354
351
  }
355
- return result;
356
352
  }
357
353
 
358
354
  /**
@@ -362,14 +358,17 @@ export abstract class SchemaCompatLayer {
362
358
  * @returns The processed Zod union
363
359
  * @throws Error if union has fewer than 2 options
364
360
  */
365
- public defaultZodUnionHandler(value: ZodUnion<[ZodTypeAny, ...ZodTypeAny[]]>): ZodTypeAny {
366
- const processedOptions = value._def.options.map((option: ZodTypeAny) => this.processZodType(option));
367
- if (processedOptions.length < 2) throw new Error('Union must have at least 2 options');
368
- let result = z.union(processedOptions as [ZodTypeAny, ZodTypeAny, ...ZodTypeAny[]]);
369
- if (value.description) {
370
- result = result.describe(value.description);
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);
371
371
  }
372
- return result;
373
372
  }
374
373
 
375
374
  /**
@@ -379,68 +378,17 @@ export abstract class SchemaCompatLayer {
379
378
  * @param handleChecks - String constraints to convert to descriptions vs keep as validation
380
379
  * @returns The processed Zod string
381
380
  */
381
+ public defaultZodStringHandler(value: zV4.ZodString, handleChecks?: readonly StringCheckType[]): zV4.ZodString;
382
+ public defaultZodStringHandler(value: zV3.ZodString, handleChecks?: readonly StringCheckType[]): zV3.ZodString;
382
383
  public defaultZodStringHandler(
383
- value: ZodString,
384
+ value: zV4.ZodString | zV3.ZodString,
384
385
  handleChecks: readonly StringCheckType[] = ALL_STRING_CHECKS,
385
- ): ZodString {
386
- const constraints: StringConstraints = {};
387
- const checks = value._def.checks || [];
388
- type ZodStringCheck = (typeof checks)[number];
389
- const newChecks: ZodStringCheck[] = [];
390
- for (const check of checks) {
391
- if ('kind' in check) {
392
- if (handleChecks.includes(check.kind as StringCheckType)) {
393
- switch (check.kind) {
394
- case 'regex': {
395
- constraints.regex = {
396
- pattern: check.regex.source,
397
- flags: check.regex.flags,
398
- };
399
- break;
400
- }
401
- case 'emoji': {
402
- constraints.emoji = true;
403
- break;
404
- }
405
- case 'email': {
406
- constraints.email = true;
407
- break;
408
- }
409
- case 'url': {
410
- constraints.url = true;
411
- break;
412
- }
413
- case 'uuid': {
414
- constraints.uuid = true;
415
- break;
416
- }
417
- case 'cuid': {
418
- constraints.cuid = true;
419
- break;
420
- }
421
- case 'min': {
422
- constraints.minLength = check.value;
423
- break;
424
- }
425
- case 'max': {
426
- constraints.maxLength = check.value;
427
- break;
428
- }
429
- }
430
- } else {
431
- newChecks.push(check);
432
- }
433
- }
434
- }
435
- let result = z.string();
436
- for (const check of newChecks) {
437
- result = result._addCheck(check);
438
- }
439
- const description = this.mergeParameterDescription(value.description, constraints);
440
- if (description) {
441
- 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);
442
391
  }
443
- return result;
444
392
  }
445
393
 
446
394
  /**
@@ -450,60 +398,17 @@ export abstract class SchemaCompatLayer {
450
398
  * @param handleChecks - Number constraints to convert to descriptions vs keep as validation
451
399
  * @returns The processed Zod number
452
400
  */
401
+ public defaultZodNumberHandler(value: zV4.ZodNumber, handleChecks?: readonly NumberCheckType[]): zV4.ZodNumber;
402
+ public defaultZodNumberHandler(value: zV3.ZodNumber, handleChecks?: readonly NumberCheckType[]): zV3.ZodNumber;
453
403
  public defaultZodNumberHandler(
454
- value: ZodNumber,
404
+ value: zV4.ZodNumber | zV3.ZodNumber,
455
405
  handleChecks: readonly NumberCheckType[] = ALL_NUMBER_CHECKS,
456
- ): ZodNumber {
457
- const constraints: NumberConstraints = {};
458
- const checks = value._def.checks || [];
459
- type ZodNumberCheck = (typeof checks)[number];
460
- const newChecks: ZodNumberCheck[] = [];
461
- for (const check of checks) {
462
- if ('kind' in check) {
463
- if (handleChecks.includes(check.kind as NumberCheckType)) {
464
- switch (check.kind) {
465
- case 'min':
466
- if (check.inclusive) {
467
- constraints.gte = check.value;
468
- } else {
469
- constraints.gt = check.value;
470
- }
471
- break;
472
- case 'max':
473
- if (check.inclusive) {
474
- constraints.lte = check.value;
475
- } else {
476
- constraints.lt = check.value;
477
- }
478
- break;
479
- case 'multipleOf': {
480
- constraints.multipleOf = check.value;
481
- break;
482
- }
483
- }
484
- } else {
485
- newChecks.push(check);
486
- }
487
- }
488
- }
489
- let result = z.number();
490
- for (const check of newChecks) {
491
- switch (check.kind) {
492
- case 'int':
493
- result = result.int();
494
- break;
495
- case 'finite':
496
- result = result.finite();
497
- break;
498
- default:
499
- result = result._addCheck(check);
500
- }
501
- }
502
- const description = this.mergeParameterDescription(value.description, constraints);
503
- if (description) {
504
- 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);
505
411
  }
506
- return result;
507
412
  }
508
413
 
509
414
  /**
@@ -512,38 +417,14 @@ export abstract class SchemaCompatLayer {
512
417
  * @param value - The Zod date to process
513
418
  * @returns A Zod string schema representing the date in ISO format
514
419
  */
515
- public defaultZodDateHandler(value: ZodDate): ZodString {
516
- const constraints: DateConstraints = {};
517
- const checks = value._def.checks || [];
518
- type ZodDateCheck = (typeof checks)[number];
519
- const newChecks: ZodDateCheck[] = [];
520
- for (const check of checks) {
521
- if ('kind' in check) {
522
- switch (check.kind) {
523
- case 'min':
524
- const minDate = new Date(check.value);
525
- if (!isNaN(minDate.getTime())) {
526
- constraints.minDate = minDate.toISOString();
527
- }
528
- break;
529
- case 'max':
530
- const maxDate = new Date(check.value);
531
- if (!isNaN(maxDate.getTime())) {
532
- constraints.maxDate = maxDate.toISOString();
533
- }
534
- break;
535
- default:
536
- newChecks.push(check);
537
- }
538
- }
539
- }
540
- constraints.dateFormat = 'date-time';
541
- let result = z.string().describe('date-time');
542
- const description = this.mergeParameterDescription(value.description, constraints);
543
- if (description) {
544
- 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);
545
427
  }
546
- return result;
547
428
  }
548
429
 
549
430
  /**
@@ -553,14 +434,16 @@ export abstract class SchemaCompatLayer {
553
434
  * @param handleTypes - Types that should be processed vs passed through
554
435
  * @returns The processed Zod optional
555
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;
556
439
  public defaultZodOptionalHandler(
557
- value: ZodOptional<any>,
558
- handleTypes: readonly AllZodType[] = SUPPORTED_ZOD_TYPES,
559
- ): ZodTypeAny {
560
- if (handleTypes.includes(value._def.innerType._def.typeName as AllZodType)) {
561
- return this.processZodType(value._def.innerType).optional();
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);
562
445
  } else {
563
- return value;
446
+ return this.v3Layer.defaultZodOptionalHandler(value, handleTypes ?? SUPPORTED_ZOD_TYPES_V3);
564
447
  }
565
448
  }
566
449
 
@@ -570,7 +453,7 @@ export abstract class SchemaCompatLayer {
570
453
  * @param zodSchema - The Zod object schema to process
571
454
  * @returns An AI SDK Schema with provider-specific compatibility applied
572
455
  */
573
- public processToAISDKSchema(zodSchema: z.ZodSchema): Schema {
456
+ public processToAISDKSchema(zodSchema: zV3.ZodSchema | zV4.ZodType): Schema {
574
457
  const processedSchema = this.processZodType(zodSchema);
575
458
 
576
459
  return convertZodSchemaToAISDKSchema(processedSchema, this.getSchemaTarget());
@@ -582,7 +465,7 @@ export abstract class SchemaCompatLayer {
582
465
  * @param zodSchema - The Zod object schema to process
583
466
  * @returns A JSONSchema7 object with provider-specific compatibility applied
584
467
  */
585
- public processToJSONSchema(zodSchema: z.ZodSchema): JSONSchema7 {
468
+ public processToJSONSchema(zodSchema: zV3.ZodSchema | zV4.ZodType): JSONSchema7 {
586
469
  return this.processToAISDKSchema(zodSchema).jsonSchema;
587
470
  }
588
471
  }