@mastra/schema-compat 0.0.0-fix-message-list-args-missing-20250807205055 → 0.0.0-zod-v4-stuff-20250825154219

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 (67) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +14 -1
  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 +842 -83
  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 +841 -84
  13. package/dist/index.js.map +1 -1
  14. package/dist/provider-compats/anthropic.d.ts +6 -4
  15. package/dist/provider-compats/anthropic.d.ts.map +1 -1
  16. package/dist/provider-compats/deepseek.d.ts +6 -4
  17. package/dist/provider-compats/deepseek.d.ts.map +1 -1
  18. package/dist/provider-compats/google.d.ts +6 -4
  19. package/dist/provider-compats/google.d.ts.map +1 -1
  20. package/dist/provider-compats/meta.d.ts +6 -4
  21. package/dist/provider-compats/meta.d.ts.map +1 -1
  22. package/dist/provider-compats/openai-reasoning.d.ts +6 -4
  23. package/dist/provider-compats/openai-reasoning.d.ts.map +1 -1
  24. package/dist/provider-compats/openai.d.ts +6 -4
  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 +76 -133
  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 +29 -11
  49. package/src/provider-compats/deepseek.ts +14 -9
  50. package/src/provider-compats/google.ts +18 -32
  51. package/src/provider-compats/meta.ts +15 -10
  52. package/src/provider-compats/openai-reasoning.ts +18 -24
  53. package/src/provider-compats/openai.ts +22 -12
  54. package/src/schema-compatibility-v3.ts +664 -0
  55. package/src/schema-compatibility-v4.test.ts +476 -0
  56. package/src/schema-compatibility-v4.ts +706 -0
  57. package/src/schema-compatibility.test.ts +12 -28
  58. package/src/schema-compatibility.ts +262 -385
  59. package/src/types.ts +5 -0
  60. package/src/utils-test-suite.ts +467 -0
  61. package/src/utils-v3.test.ts +9 -0
  62. package/src/utils-v4.test.ts +9 -0
  63. package/src/utils.ts +30 -24
  64. package/src/zod-to-json.ts +27 -0
  65. package/src/zodTypes.ts +56 -0
  66. package/tsup.config.ts +8 -3
  67. 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, 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,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: 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;
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, 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
- const processedShape = Object.entries(value.shape).reduce<Record<string, ZodTypeAny>>((acc, [key, propValue]) => {
247
- acc[key] = this.processZodType(propValue as ZodTypeAny);
248
- return acc;
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
- if (Object.keys(constraints).length > 0) {
291
- return (description ? description + '\n' : '') + JSON.stringify(constraints);
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 z.AnyZodObject>(
306
- value: z.ZodTypeAny,
307
- throwOnTypes: readonly UnsupportedZodType[] = UNSUPPORTED_ZOD_TYPES,
308
- ): ShapeValue<T> {
309
- if (throwOnTypes.includes(value._def?.typeName as UnsupportedZodType)) {
310
- 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
+ );
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
- const zodArrayDef = value._def;
327
- const processedType = this.processZodType(zodArrayDef.type);
328
-
329
- let result = z.array(processedType);
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<[ZodTypeAny, ...ZodTypeAny[]]>): ZodTypeAny {
372
- const processedOptions = value._def.options.map((option: ZodTypeAny) => this.processZodType(option));
373
- if (processedOptions.length < 2) throw new Error('Union must have at least 2 options');
374
- let result = z.union(processedOptions as [ZodTypeAny, ZodTypeAny, ...ZodTypeAny[]]);
375
- if (value.description) {
376
- 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);
377
371
  }
378
- return result;
379
372
  }
380
373
 
381
374
  /**
@@ -385,68 +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
- const constraints: StringConstraints = {};
393
- const checks = value._def.checks || [];
394
- type ZodStringCheck = (typeof checks)[number];
395
- const newChecks: ZodStringCheck[] = [];
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 = {
402
- pattern: check.regex.source,
403
- flags: check.regex.flags,
404
- };
405
- break;
406
- }
407
- case 'emoji': {
408
- constraints.emoji = true;
409
- break;
410
- }
411
- case 'email': {
412
- constraints.email = true;
413
- break;
414
- }
415
- case 'url': {
416
- constraints.url = true;
417
- break;
418
- }
419
- case 'uuid': {
420
- constraints.uuid = true;
421
- break;
422
- }
423
- case 'cuid': {
424
- constraints.cuid = true;
425
- break;
426
- }
427
- case 'min': {
428
- constraints.minLength = check.value;
429
- break;
430
- }
431
- case 'max': {
432
- constraints.maxLength = check.value;
433
- break;
434
- }
435
- }
436
- } else {
437
- newChecks.push(check);
438
- }
439
- }
440
- }
441
- let result = z.string();
442
- for (const check of newChecks) {
443
- result = result._addCheck(check);
444
- }
445
- const description = this.mergeParameterDescription(value.description, constraints);
446
- if (description) {
447
- 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);
448
391
  }
449
- return result;
450
392
  }
451
393
 
452
394
  /**
@@ -456,60 +398,17 @@ export abstract class SchemaCompatLayer {
456
398
  * @param handleChecks - Number constraints to convert to descriptions vs keep as validation
457
399
  * @returns The processed Zod number
458
400
  */
401
+ public defaultZodNumberHandler(value: zV4.ZodNumber, handleChecks?: readonly NumberCheckType[]): zV4.ZodNumber;
402
+ public defaultZodNumberHandler(value: zV3.ZodNumber, handleChecks?: readonly NumberCheckType[]): zV3.ZodNumber;
459
403
  public defaultZodNumberHandler(
460
- value: ZodNumber,
404
+ value: zV4.ZodNumber | zV3.ZodNumber,
461
405
  handleChecks: readonly NumberCheckType[] = ALL_NUMBER_CHECKS,
462
- ): ZodNumber {
463
- const constraints: NumberConstraints = {};
464
- const checks = value._def.checks || [];
465
- type ZodNumberCheck = (typeof checks)[number];
466
- const newChecks: ZodNumberCheck[] = [];
467
- for (const check of checks) {
468
- if ('kind' in check) {
469
- if (handleChecks.includes(check.kind as NumberCheckType)) {
470
- switch (check.kind) {
471
- case 'min':
472
- if (check.inclusive) {
473
- constraints.gte = check.value;
474
- } else {
475
- constraints.gt = check.value;
476
- }
477
- break;
478
- case 'max':
479
- if (check.inclusive) {
480
- constraints.lte = check.value;
481
- } else {
482
- constraints.lt = check.value;
483
- }
484
- break;
485
- case 'multipleOf': {
486
- constraints.multipleOf = check.value;
487
- break;
488
- }
489
- }
490
- } else {
491
- newChecks.push(check);
492
- }
493
- }
494
- }
495
- let result = z.number();
496
- for (const check of newChecks) {
497
- switch (check.kind) {
498
- case 'int':
499
- result = result.int();
500
- break;
501
- case 'finite':
502
- result = result.finite();
503
- break;
504
- default:
505
- result = result._addCheck(check);
506
- }
507
- }
508
- const description = this.mergeParameterDescription(value.description, constraints);
509
- if (description) {
510
- 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);
511
411
  }
512
- return result;
513
412
  }
514
413
 
515
414
  /**
@@ -518,38 +417,14 @@ export abstract class SchemaCompatLayer {
518
417
  * @param value - The Zod date to process
519
418
  * @returns A Zod string schema representing the date in ISO format
520
419
  */
521
- public defaultZodDateHandler(value: ZodDate): ZodString {
522
- const constraints: DateConstraints = {};
523
- const checks = value._def.checks || [];
524
- type ZodDateCheck = (typeof checks)[number];
525
- const newChecks: ZodDateCheck[] = [];
526
- for (const check of checks) {
527
- if ('kind' in check) {
528
- switch (check.kind) {
529
- case 'min':
530
- const minDate = new Date(check.value);
531
- if (!isNaN(minDate.getTime())) {
532
- constraints.minDate = minDate.toISOString();
533
- }
534
- break;
535
- case 'max':
536
- const maxDate = new Date(check.value);
537
- if (!isNaN(maxDate.getTime())) {
538
- constraints.maxDate = maxDate.toISOString();
539
- }
540
- break;
541
- default:
542
- newChecks.push(check);
543
- }
544
- }
545
- }
546
- constraints.dateFormat = 'date-time';
547
- let result = z.string().describe('date-time');
548
- const description = this.mergeParameterDescription(value.description, constraints);
549
- if (description) {
550
- 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);
551
427
  }
552
- return result;
553
428
  }
554
429
 
555
430
  /**
@@ -559,14 +434,16 @@ export abstract class SchemaCompatLayer {
559
434
  * @param handleTypes - Types that should be processed vs passed through
560
435
  * @returns The processed Zod optional
561
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;
562
439
  public defaultZodOptionalHandler(
563
- value: ZodOptional<any>,
564
- handleTypes: readonly AllZodType[] = SUPPORTED_ZOD_TYPES,
565
- ): ZodTypeAny {
566
- if (handleTypes.includes(value._def.innerType._def.typeName as AllZodType)) {
567
- 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);
568
445
  } else {
569
- return value;
446
+ return this.v3Layer.defaultZodOptionalHandler(value, handleTypes ?? SUPPORTED_ZOD_TYPES_V3);
570
447
  }
571
448
  }
572
449
 
@@ -576,7 +453,7 @@ export abstract class SchemaCompatLayer {
576
453
  * @param zodSchema - The Zod object schema to process
577
454
  * @returns An AI SDK Schema with provider-specific compatibility applied
578
455
  */
579
- public processToAISDKSchema(zodSchema: z.ZodSchema): Schema {
456
+ public processToAISDKSchema(zodSchema: zV3.ZodSchema | zV4.ZodType): Schema {
580
457
  const processedSchema = this.processZodType(zodSchema);
581
458
 
582
459
  return convertZodSchemaToAISDKSchema(processedSchema, this.getSchemaTarget());
@@ -588,7 +465,7 @@ export abstract class SchemaCompatLayer {
588
465
  * @param zodSchema - The Zod object schema to process
589
466
  * @returns A JSONSchema7 object with provider-specific compatibility applied
590
467
  */
591
- public processToJSONSchema(zodSchema: z.ZodSchema): JSONSchema7 {
468
+ public processToJSONSchema(zodSchema: zV3.ZodSchema | zV4.ZodType): JSONSchema7 {
592
469
  return this.processToAISDKSchema(zodSchema).jsonSchema;
593
470
  }
594
471
  }