@langchain/core 0.3.57 → 0.3.58
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/dist/language_models/base.d.ts +14 -7
- package/dist/language_models/chat_models.cjs +5 -5
- package/dist/language_models/chat_models.d.ts +9 -3
- package/dist/language_models/chat_models.js +5 -5
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.d.ts +1 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs +3 -2
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.ts +19 -5
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.js +3 -2
- package/dist/output_parsers/structured.cjs +8 -7
- package/dist/output_parsers/structured.d.ts +10 -9
- package/dist/output_parsers/structured.js +6 -5
- package/dist/runnables/base.cjs +11 -12
- package/dist/runnables/base.d.ts +9 -9
- package/dist/runnables/base.js +8 -9
- package/dist/runnables/graph.cjs +2 -2
- package/dist/runnables/graph.js +2 -2
- package/dist/runnables/types.d.ts +2 -2
- package/dist/tools/index.cjs +9 -11
- package/dist/tools/index.d.ts +11 -8
- package/dist/tools/index.js +7 -9
- package/dist/tools/types.cjs +2 -2
- package/dist/tools/types.d.ts +11 -13
- package/dist/tools/types.js +2 -2
- package/dist/utils/env.cjs +1 -3
- package/dist/utils/env.js +1 -3
- package/dist/utils/function_calling.cjs +4 -6
- package/dist/utils/function_calling.d.ts +4 -6
- package/dist/utils/function_calling.js +4 -6
- package/dist/utils/json_schema.cjs +6 -2
- package/dist/utils/json_schema.d.ts +3 -2
- package/dist/utils/json_schema.js +6 -2
- package/dist/utils/testing/index.d.ts +8 -7
- package/dist/utils/types/index.cjs +1 -1
- package/dist/utils/types/index.d.ts +1 -1
- package/dist/utils/types/index.js +1 -1
- package/dist/utils/types/zod.cjs +424 -0
- package/dist/utils/types/zod.d.ts +173 -0
- package/dist/utils/types/zod.js +404 -0
- package/package.json +2 -2
- package/dist/utils/types/is_zod_schema.cjs +0 -38
- package/dist/utils/types/is_zod_schema.d.ts +0 -8
- package/dist/utils/types/is_zod_schema.js +0 -34
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getInteropZodDefaultGetter = exports.interopZodObjectPassthrough = exports.interopZodObjectPartial = exports.extendInteropZodObject = exports.getInteropZodObjectShape = exports.isInteropZodObject = exports.isSimpleStringZodSchema = exports.isShapelessZodSchema = exports.getSchemaDescription = exports.interopParse = exports.interopSafeParse = exports.interopParseAsync = exports.interopSafeParseAsync = exports.isInteropZodSchema = exports.isZodSchema = exports.isZodSchemaV3 = exports.isZodSchemaV4 = void 0;
|
|
4
|
+
const core_1 = require("zod/v4/core");
|
|
5
|
+
function isZodSchemaV4(schema) {
|
|
6
|
+
if (typeof schema !== "object" || schema === null) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
const obj = schema;
|
|
10
|
+
if (!("_zod" in obj)) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
const zod = obj._zod;
|
|
14
|
+
return (typeof zod === "object" &&
|
|
15
|
+
zod !== null &&
|
|
16
|
+
"def" in zod);
|
|
17
|
+
}
|
|
18
|
+
exports.isZodSchemaV4 = isZodSchemaV4;
|
|
19
|
+
function isZodSchemaV3(schema) {
|
|
20
|
+
if (typeof schema !== "object" || schema === null) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
const obj = schema;
|
|
24
|
+
if (!("_def" in obj) || "_zod" in obj) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
const def = obj._def;
|
|
28
|
+
return (typeof def === "object" &&
|
|
29
|
+
def != null &&
|
|
30
|
+
"typeName" in def);
|
|
31
|
+
}
|
|
32
|
+
exports.isZodSchemaV3 = isZodSchemaV3;
|
|
33
|
+
/** Backward compatible isZodSchema for Zod 3 */
|
|
34
|
+
function isZodSchema(schema) {
|
|
35
|
+
if (isZodSchemaV4(schema)) {
|
|
36
|
+
console.warn("[WARNING] Attempting to use Zod 4 schema in a context where Zod 3 schema is expected. This may cause unexpected behavior.");
|
|
37
|
+
}
|
|
38
|
+
return isZodSchemaV3(schema);
|
|
39
|
+
}
|
|
40
|
+
exports.isZodSchema = isZodSchema;
|
|
41
|
+
/**
|
|
42
|
+
* Given either a Zod schema, or plain object, determine if the input is a Zod schema.
|
|
43
|
+
*
|
|
44
|
+
* @param {unknown} input
|
|
45
|
+
* @returns {boolean} Whether or not the provided input is a Zod schema.
|
|
46
|
+
*/
|
|
47
|
+
function isInteropZodSchema(input) {
|
|
48
|
+
if (!input) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
if (typeof input !== "object") {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
if (Array.isArray(input)) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
if (isZodSchemaV4(input) ||
|
|
58
|
+
isZodSchemaV3(input)) {
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
exports.isInteropZodSchema = isInteropZodSchema;
|
|
64
|
+
/**
|
|
65
|
+
* Asynchronously parses the input using the provided Zod schema (v3 or v4) and returns a safe parse result.
|
|
66
|
+
* This function handles both Zod v3 and v4 schemas, returning a result object indicating success or failure.
|
|
67
|
+
*
|
|
68
|
+
* @template T - The expected output type of the schema.
|
|
69
|
+
* @param {InteropZodType<T>} schema - The Zod schema (v3 or v4) to use for parsing.
|
|
70
|
+
* @param {unknown} input - The input value to parse.
|
|
71
|
+
* @returns {Promise<InteropZodSafeParseResult<T>>} A promise that resolves to a safe parse result object.
|
|
72
|
+
* @throws {Error} If the schema is not a recognized Zod v3 or v4 schema.
|
|
73
|
+
*/
|
|
74
|
+
async function interopSafeParseAsync(schema, input) {
|
|
75
|
+
if (isZodSchemaV4(schema)) {
|
|
76
|
+
try {
|
|
77
|
+
const data = await (0, core_1.parseAsync)(schema, input);
|
|
78
|
+
return {
|
|
79
|
+
success: true,
|
|
80
|
+
data,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
return {
|
|
85
|
+
success: false,
|
|
86
|
+
error: error,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (isZodSchemaV3(schema)) {
|
|
91
|
+
return schema.safeParse(input);
|
|
92
|
+
}
|
|
93
|
+
throw new Error("Schema must be an instance of z3.ZodType or z4.$ZodType");
|
|
94
|
+
}
|
|
95
|
+
exports.interopSafeParseAsync = interopSafeParseAsync;
|
|
96
|
+
/**
|
|
97
|
+
* Asynchronously parses the input using the provided Zod schema (v3 or v4) and returns the parsed value.
|
|
98
|
+
* Throws an error if parsing fails or if the schema is not a recognized Zod v3 or v4 schema.
|
|
99
|
+
*
|
|
100
|
+
* @template T - The expected output type of the schema.
|
|
101
|
+
* @param {InteropZodType<T>} schema - The Zod schema (v3 or v4) to use for parsing.
|
|
102
|
+
* @param {unknown} input - The input value to parse.
|
|
103
|
+
* @returns {Promise<T>} A promise that resolves to the parsed value.
|
|
104
|
+
* @throws {Error} If parsing fails or the schema is not a recognized Zod v3 or v4 schema.
|
|
105
|
+
*/
|
|
106
|
+
async function interopParseAsync(schema, input) {
|
|
107
|
+
if (isZodSchemaV4(schema)) {
|
|
108
|
+
return (0, core_1.parse)(schema, input);
|
|
109
|
+
}
|
|
110
|
+
if (isZodSchemaV3(schema)) {
|
|
111
|
+
return schema.parse(input);
|
|
112
|
+
}
|
|
113
|
+
throw new Error("Schema must be an instance of z3.ZodType or z4.$ZodType");
|
|
114
|
+
}
|
|
115
|
+
exports.interopParseAsync = interopParseAsync;
|
|
116
|
+
/**
|
|
117
|
+
* Safely parses the input using the provided Zod schema (v3 or v4) and returns a result object
|
|
118
|
+
* indicating success or failure. This function is compatible with both Zod v3 and v4 schemas.
|
|
119
|
+
*
|
|
120
|
+
* @template T - The expected output type of the schema.
|
|
121
|
+
* @param {InteropZodType<T>} schema - The Zod schema (v3 or v4) to use for parsing.
|
|
122
|
+
* @param {unknown} input - The input value to parse.
|
|
123
|
+
* @returns {InteropZodSafeParseResult<T>} An object with either the parsed data (on success)
|
|
124
|
+
* or the error (on failure).
|
|
125
|
+
* @throws {Error} If the schema is not a recognized Zod v3 or v4 schema.
|
|
126
|
+
*/
|
|
127
|
+
function interopSafeParse(schema, input) {
|
|
128
|
+
if (isZodSchemaV4(schema)) {
|
|
129
|
+
try {
|
|
130
|
+
const data = (0, core_1.parse)(schema, input);
|
|
131
|
+
return {
|
|
132
|
+
success: true,
|
|
133
|
+
data,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
return {
|
|
138
|
+
success: false,
|
|
139
|
+
error: error,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (isZodSchemaV3(schema)) {
|
|
144
|
+
return schema.safeParse(input);
|
|
145
|
+
}
|
|
146
|
+
throw new Error("Schema must be an instance of z3.ZodType or z4.$ZodType");
|
|
147
|
+
}
|
|
148
|
+
exports.interopSafeParse = interopSafeParse;
|
|
149
|
+
/**
|
|
150
|
+
* Parses the input using the provided Zod schema (v3 or v4) and returns the parsed value.
|
|
151
|
+
* Throws an error if parsing fails or if the schema is not a recognized Zod v3 or v4 schema.
|
|
152
|
+
*
|
|
153
|
+
* @template T - The expected output type of the schema.
|
|
154
|
+
* @param {InteropZodType<T>} schema - The Zod schema (v3 or v4) to use for parsing.
|
|
155
|
+
* @param {unknown} input - The input value to parse.
|
|
156
|
+
* @returns {T} The parsed value.
|
|
157
|
+
* @throws {Error} If parsing fails or the schema is not a recognized Zod v3 or v4 schema.
|
|
158
|
+
*/
|
|
159
|
+
function interopParse(schema, input) {
|
|
160
|
+
if (isZodSchemaV4(schema)) {
|
|
161
|
+
return (0, core_1.parse)(schema, input);
|
|
162
|
+
}
|
|
163
|
+
if (isZodSchemaV3(schema)) {
|
|
164
|
+
return schema.parse(input);
|
|
165
|
+
}
|
|
166
|
+
throw new Error("Schema must be an instance of z3.ZodType or z4.$ZodType");
|
|
167
|
+
}
|
|
168
|
+
exports.interopParse = interopParse;
|
|
169
|
+
/**
|
|
170
|
+
* Retrieves the description from a schema definition (v3, v4, or plain object), if available.
|
|
171
|
+
*
|
|
172
|
+
* @param {unknown} schema - The schema to extract the description from.
|
|
173
|
+
* @returns {string | undefined} The description of the schema, or undefined if not present.
|
|
174
|
+
*/
|
|
175
|
+
function getSchemaDescription(schema) {
|
|
176
|
+
if (isZodSchemaV4(schema)) {
|
|
177
|
+
return core_1.globalRegistry.get(schema)?.description;
|
|
178
|
+
}
|
|
179
|
+
if (isZodSchemaV3(schema)) {
|
|
180
|
+
return schema.description;
|
|
181
|
+
}
|
|
182
|
+
if ("description" in schema && typeof schema.description === "string") {
|
|
183
|
+
return schema.description;
|
|
184
|
+
}
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
exports.getSchemaDescription = getSchemaDescription;
|
|
188
|
+
/**
|
|
189
|
+
* Determines if the provided Zod schema is "shapeless".
|
|
190
|
+
* A shapeless schema is one that does not define any object shape,
|
|
191
|
+
* such as ZodString, ZodNumber, ZodBoolean, ZodAny, etc.
|
|
192
|
+
* For ZodObject, it must have no shape keys to be considered shapeless.
|
|
193
|
+
* ZodRecord schemas are considered shapeless since they define dynamic
|
|
194
|
+
* key-value mappings without fixed keys.
|
|
195
|
+
*
|
|
196
|
+
* @param schema The Zod schema to check.
|
|
197
|
+
* @returns {boolean} True if the schema is shapeless, false otherwise.
|
|
198
|
+
*/
|
|
199
|
+
function isShapelessZodSchema(schema) {
|
|
200
|
+
if (!isInteropZodSchema(schema)) {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
// Check for v3 schemas
|
|
204
|
+
if (isZodSchemaV3(schema)) {
|
|
205
|
+
// @ts-expect-error - zod v3 types are not compatible with zod v4 types
|
|
206
|
+
const def = schema._def;
|
|
207
|
+
// ZodObject is only shaped if it has actual shape keys
|
|
208
|
+
if (def.typeName === "ZodObject") {
|
|
209
|
+
const obj = schema;
|
|
210
|
+
return !obj.shape || Object.keys(obj.shape).length === 0;
|
|
211
|
+
}
|
|
212
|
+
// ZodRecord is shapeless (dynamic key-value mapping)
|
|
213
|
+
if (def.typeName === "ZodRecord") {
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
// Check for v4 schemas
|
|
218
|
+
if (isZodSchemaV4(schema)) {
|
|
219
|
+
const def = schema._zod.def;
|
|
220
|
+
// Object type is only shaped if it has actual shape keys
|
|
221
|
+
if (def.type === "object") {
|
|
222
|
+
const obj = schema;
|
|
223
|
+
return !obj.shape || Object.keys(obj.shape).length === 0;
|
|
224
|
+
}
|
|
225
|
+
// Record type is shapeless (dynamic key-value mapping)
|
|
226
|
+
if (def.type === "record") {
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
// For other schemas, check if they have a `shape` property
|
|
231
|
+
// If they don't have shape, they're likely shapeless
|
|
232
|
+
if (typeof schema === "object" && schema !== null && !("shape" in schema)) {
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
exports.isShapelessZodSchema = isShapelessZodSchema;
|
|
238
|
+
/**
|
|
239
|
+
* Determines if the provided Zod schema should be treated as a simple string schema
|
|
240
|
+
* that maps to DynamicTool. This aligns with the type-level constraint of
|
|
241
|
+
* InteropZodType<string | undefined> which only matches basic string schemas.
|
|
242
|
+
* If the provided schema is just z.string(), we can make the determination that
|
|
243
|
+
* the tool is just a generic string tool that doesn't require any input validation.
|
|
244
|
+
*
|
|
245
|
+
* This function only returns true for basic ZodString schemas, including:
|
|
246
|
+
* - Basic string schemas (z.string())
|
|
247
|
+
* - String schemas with validations (z.string().min(1), z.string().email(), etc.)
|
|
248
|
+
*
|
|
249
|
+
* This function returns false for everything else, including:
|
|
250
|
+
* - String schemas with defaults (z.string().default("value"))
|
|
251
|
+
* - Branded string schemas (z.string().brand<"UserId">())
|
|
252
|
+
* - String schemas with catch operations (z.string().catch("default"))
|
|
253
|
+
* - Optional/nullable string schemas (z.string().optional())
|
|
254
|
+
* - Transformed schemas (z.string().transform() or z.object().transform())
|
|
255
|
+
* - Object or record schemas, even if they're empty
|
|
256
|
+
* - Any other schema type
|
|
257
|
+
*
|
|
258
|
+
* @param schema The Zod schema to check.
|
|
259
|
+
* @returns {boolean} True if the schema is a basic ZodString, false otherwise.
|
|
260
|
+
*/
|
|
261
|
+
function isSimpleStringZodSchema(schema) {
|
|
262
|
+
if (!isInteropZodSchema(schema)) {
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
// For v3 schemas
|
|
266
|
+
if (isZodSchemaV3(schema)) {
|
|
267
|
+
// @ts-expect-error - zod v3 types are not compatible with zod v4 types
|
|
268
|
+
const def = schema._def;
|
|
269
|
+
// Only accept basic ZodString
|
|
270
|
+
return def.typeName === "ZodString";
|
|
271
|
+
}
|
|
272
|
+
// For v4 schemas
|
|
273
|
+
if (isZodSchemaV4(schema)) {
|
|
274
|
+
const def = schema._zod.def;
|
|
275
|
+
// Only accept basic string type
|
|
276
|
+
return def.type === "string";
|
|
277
|
+
}
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
exports.isSimpleStringZodSchema = isSimpleStringZodSchema;
|
|
281
|
+
/**
|
|
282
|
+
* Determines if the provided value is an InteropZodObject (Zod v3 or v4 object schema).
|
|
283
|
+
*
|
|
284
|
+
* @param obj The value to check.
|
|
285
|
+
* @returns {boolean} True if the value is a Zod v3 or v4 object schema, false otherwise.
|
|
286
|
+
*/
|
|
287
|
+
function isInteropZodObject(obj) {
|
|
288
|
+
if (isZodSchemaV3(obj)) {
|
|
289
|
+
// Zod v3 object schemas have _def.typeName === "ZodObject"
|
|
290
|
+
if (typeof obj === "object" &&
|
|
291
|
+
obj !== null &&
|
|
292
|
+
"_def" in obj &&
|
|
293
|
+
typeof obj._def === "object" &&
|
|
294
|
+
obj._def !== null &&
|
|
295
|
+
"typeName" in obj._def &&
|
|
296
|
+
obj._def.typeName === "ZodObject") {
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (isZodSchemaV4(obj)) {
|
|
301
|
+
// Zod v4 object schemas have _zod.def.type === "object"
|
|
302
|
+
if (typeof obj === "object" &&
|
|
303
|
+
obj !== null &&
|
|
304
|
+
"_zod" in obj &&
|
|
305
|
+
typeof obj._zod === "object" &&
|
|
306
|
+
obj._zod !== null &&
|
|
307
|
+
"def" in obj._zod &&
|
|
308
|
+
typeof obj._zod.def === "object" &&
|
|
309
|
+
obj._zod.def !== null &&
|
|
310
|
+
"type" in obj._zod.def &&
|
|
311
|
+
obj._zod.def.type === "object") {
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
exports.isInteropZodObject = isInteropZodObject;
|
|
318
|
+
/**
|
|
319
|
+
* Retrieves the shape (fields) of a Zod object schema, supporting both Zod v3 and v4.
|
|
320
|
+
*
|
|
321
|
+
* @template T - The type of the Zod object schema.
|
|
322
|
+
* @param {T} schema - The Zod object schema instance (either v3 or v4).
|
|
323
|
+
* @returns {InteropZodObjectShape<T>} The shape of the object schema.
|
|
324
|
+
* @throws {Error} If the schema is not a Zod v3 or v4 object.
|
|
325
|
+
*/
|
|
326
|
+
function getInteropZodObjectShape(schema) {
|
|
327
|
+
if (isZodSchemaV3(schema)) {
|
|
328
|
+
return schema.shape;
|
|
329
|
+
}
|
|
330
|
+
if (isZodSchemaV4(schema)) {
|
|
331
|
+
return schema._zod.def.shape;
|
|
332
|
+
}
|
|
333
|
+
throw new Error("Schema must be an instance of z3.ZodObject or z4.$ZodObject");
|
|
334
|
+
}
|
|
335
|
+
exports.getInteropZodObjectShape = getInteropZodObjectShape;
|
|
336
|
+
/**
|
|
337
|
+
* Extends a Zod object schema with additional fields, supporting both Zod v3 and v4.
|
|
338
|
+
*
|
|
339
|
+
* @template T - The type of the Zod object schema.
|
|
340
|
+
* @param {T} schema - The Zod object schema instance (either v3 or v4).
|
|
341
|
+
* @param {InteropZodObjectShape} extension - The fields to add to the schema.
|
|
342
|
+
* @returns {InteropZodObject} The extended Zod object schema.
|
|
343
|
+
* @throws {Error} If the schema is not a Zod v3 or v4 object.
|
|
344
|
+
*/
|
|
345
|
+
function extendInteropZodObject(schema, extension) {
|
|
346
|
+
if (isZodSchemaV3(schema)) {
|
|
347
|
+
return schema.extend(extension);
|
|
348
|
+
}
|
|
349
|
+
if (isZodSchemaV4(schema)) {
|
|
350
|
+
return core_1.util.extend(schema, extension);
|
|
351
|
+
}
|
|
352
|
+
throw new Error("Schema must be an instance of z3.ZodObject or z4.$ZodObject");
|
|
353
|
+
}
|
|
354
|
+
exports.extendInteropZodObject = extendInteropZodObject;
|
|
355
|
+
/**
|
|
356
|
+
* Returns a partial version of a Zod object schema, making all fields optional.
|
|
357
|
+
* Supports both Zod v3 and v4.
|
|
358
|
+
*
|
|
359
|
+
* @template T - The type of the Zod object schema.
|
|
360
|
+
* @param {T} schema - The Zod object schema instance (either v3 or v4).
|
|
361
|
+
* @returns {InteropZodObject} The partial Zod object schema.
|
|
362
|
+
* @throws {Error} If the schema is not a Zod v3 or v4 object.
|
|
363
|
+
*/
|
|
364
|
+
function interopZodObjectPartial(schema) {
|
|
365
|
+
if (isZodSchemaV3(schema)) {
|
|
366
|
+
// z3: .partial() exists and works as expected
|
|
367
|
+
return schema.partial();
|
|
368
|
+
}
|
|
369
|
+
if (isZodSchemaV4(schema)) {
|
|
370
|
+
// z4: util.partial exists and works as expected
|
|
371
|
+
return core_1.util.partial(core_1.$ZodOptional, schema, undefined);
|
|
372
|
+
}
|
|
373
|
+
throw new Error("Schema must be an instance of z3.ZodObject or z4.$ZodObject");
|
|
374
|
+
}
|
|
375
|
+
exports.interopZodObjectPartial = interopZodObjectPartial;
|
|
376
|
+
function interopZodObjectPassthrough(schema) {
|
|
377
|
+
if (isInteropZodObject(schema)) {
|
|
378
|
+
if (isZodSchemaV3(schema)) {
|
|
379
|
+
return schema.passthrough();
|
|
380
|
+
}
|
|
381
|
+
if (isZodSchemaV4(schema)) {
|
|
382
|
+
// Type reassign since ZodObjectV4 assumes that generics should be washed
|
|
383
|
+
const objectSchema = schema;
|
|
384
|
+
return (0, core_1.clone)(objectSchema, {
|
|
385
|
+
...objectSchema._zod.def,
|
|
386
|
+
catchall: (0, core_1._unknown)(core_1.$ZodUnknown),
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
throw new Error("Schema must be an instance of z3.ZodObject or z4.$ZodObject");
|
|
391
|
+
}
|
|
392
|
+
exports.interopZodObjectPassthrough = interopZodObjectPassthrough;
|
|
393
|
+
/**
|
|
394
|
+
* Returns a getter function for the default value of a Zod schema, if one is defined.
|
|
395
|
+
* Supports both Zod v3 and v4 schemas. If the schema has a default value,
|
|
396
|
+
* the returned function will return that value when called. If no default is defined,
|
|
397
|
+
* returns undefined.
|
|
398
|
+
*
|
|
399
|
+
* @template T - The type of the Zod schema.
|
|
400
|
+
* @param {T} schema - The Zod schema instance (either v3 or v4).
|
|
401
|
+
* @returns {(() => InferInteropZodOutput<T>) | undefined} A function that returns the default value, or undefined if no default is set.
|
|
402
|
+
*/
|
|
403
|
+
function getInteropZodDefaultGetter(schema) {
|
|
404
|
+
if (isZodSchemaV3(schema)) {
|
|
405
|
+
try {
|
|
406
|
+
const defaultValue = schema.parse(undefined);
|
|
407
|
+
return () => defaultValue;
|
|
408
|
+
}
|
|
409
|
+
catch {
|
|
410
|
+
return undefined;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (isZodSchemaV4(schema)) {
|
|
414
|
+
try {
|
|
415
|
+
const defaultValue = (0, core_1.parse)(schema, undefined);
|
|
416
|
+
return () => defaultValue;
|
|
417
|
+
}
|
|
418
|
+
catch {
|
|
419
|
+
return undefined;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
return undefined;
|
|
423
|
+
}
|
|
424
|
+
exports.getInteropZodDefaultGetter = getInteropZodDefaultGetter;
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import type * as z3 from "zod/v3";
|
|
2
|
+
import type * as z4 from "zod/v4/core";
|
|
3
|
+
export type ZodStringV3 = z3.ZodString;
|
|
4
|
+
export type ZodStringV4 = z4.$ZodType<string, unknown>;
|
|
5
|
+
export type ZodObjectV3 = z3.ZodObject<any, any, any, any>;
|
|
6
|
+
export type ZodObjectV4 = z4.$ZodObject;
|
|
7
|
+
export type InteropZodType<Output = any, Input = Output> = z3.ZodType<Output, z3.ZodTypeDef, Input> | z4.$ZodType<Output, Input>;
|
|
8
|
+
export type InteropZodObject = ZodObjectV3 | ZodObjectV4;
|
|
9
|
+
export type InteropZodObjectShape<T extends InteropZodObject = InteropZodObject> = T extends z3.ZodObject<infer Shape> ? {
|
|
10
|
+
[K in keyof Shape]: Shape[K];
|
|
11
|
+
} : T extends z4.$ZodObject<infer Shape> ? {
|
|
12
|
+
[K in keyof Shape]: Shape[K];
|
|
13
|
+
} : never;
|
|
14
|
+
export type InteropZodIssue = z3.ZodIssue | z4.$ZodIssue;
|
|
15
|
+
export type InferInteropZodInput<T> = T extends z3.ZodType<unknown, z3.ZodTypeDef, infer Input> ? Input : T extends z4.$ZodType<unknown, infer Input> ? Input : T extends {
|
|
16
|
+
_zod: {
|
|
17
|
+
input: infer Input;
|
|
18
|
+
};
|
|
19
|
+
} ? Input : never;
|
|
20
|
+
export type InferInteropZodOutput<T> = T extends z3.ZodType<infer Output, z3.ZodTypeDef, unknown> ? Output : T extends z4.$ZodType<infer Output, unknown> ? Output : T extends {
|
|
21
|
+
_zod: {
|
|
22
|
+
output: infer Output;
|
|
23
|
+
};
|
|
24
|
+
} ? Output : never;
|
|
25
|
+
export declare function isZodSchemaV4(schema: unknown): schema is z4.$ZodType<unknown, unknown>;
|
|
26
|
+
export declare function isZodSchemaV3(schema: unknown): schema is z3.ZodType<unknown, z3.ZodTypeDef, unknown>;
|
|
27
|
+
/** Backward compatible isZodSchema for Zod 3 */
|
|
28
|
+
export declare function isZodSchema<RunOutput extends Record<string, unknown> = Record<string, unknown>>(schema: z3.ZodType<RunOutput> | Record<string, unknown>): schema is z3.ZodType<RunOutput>;
|
|
29
|
+
/**
|
|
30
|
+
* Given either a Zod schema, or plain object, determine if the input is a Zod schema.
|
|
31
|
+
*
|
|
32
|
+
* @param {unknown} input
|
|
33
|
+
* @returns {boolean} Whether or not the provided input is a Zod schema.
|
|
34
|
+
*/
|
|
35
|
+
export declare function isInteropZodSchema(input: unknown): input is InteropZodType;
|
|
36
|
+
type InteropZodSafeParseResult<T> = z3.SafeParseReturnType<T, T>;
|
|
37
|
+
/**
|
|
38
|
+
* Asynchronously parses the input using the provided Zod schema (v3 or v4) and returns a safe parse result.
|
|
39
|
+
* This function handles both Zod v3 and v4 schemas, returning a result object indicating success or failure.
|
|
40
|
+
*
|
|
41
|
+
* @template T - The expected output type of the schema.
|
|
42
|
+
* @param {InteropZodType<T>} schema - The Zod schema (v3 or v4) to use for parsing.
|
|
43
|
+
* @param {unknown} input - The input value to parse.
|
|
44
|
+
* @returns {Promise<InteropZodSafeParseResult<T>>} A promise that resolves to a safe parse result object.
|
|
45
|
+
* @throws {Error} If the schema is not a recognized Zod v3 or v4 schema.
|
|
46
|
+
*/
|
|
47
|
+
export declare function interopSafeParseAsync<T>(schema: InteropZodType<T>, input: unknown): Promise<InteropZodSafeParseResult<T>>;
|
|
48
|
+
/**
|
|
49
|
+
* Asynchronously parses the input using the provided Zod schema (v3 or v4) and returns the parsed value.
|
|
50
|
+
* Throws an error if parsing fails or if the schema is not a recognized Zod v3 or v4 schema.
|
|
51
|
+
*
|
|
52
|
+
* @template T - The expected output type of the schema.
|
|
53
|
+
* @param {InteropZodType<T>} schema - The Zod schema (v3 or v4) to use for parsing.
|
|
54
|
+
* @param {unknown} input - The input value to parse.
|
|
55
|
+
* @returns {Promise<T>} A promise that resolves to the parsed value.
|
|
56
|
+
* @throws {Error} If parsing fails or the schema is not a recognized Zod v3 or v4 schema.
|
|
57
|
+
*/
|
|
58
|
+
export declare function interopParseAsync<T>(schema: InteropZodType<T>, input: unknown): Promise<T>;
|
|
59
|
+
/**
|
|
60
|
+
* Safely parses the input using the provided Zod schema (v3 or v4) and returns a result object
|
|
61
|
+
* indicating success or failure. This function is compatible with both Zod v3 and v4 schemas.
|
|
62
|
+
*
|
|
63
|
+
* @template T - The expected output type of the schema.
|
|
64
|
+
* @param {InteropZodType<T>} schema - The Zod schema (v3 or v4) to use for parsing.
|
|
65
|
+
* @param {unknown} input - The input value to parse.
|
|
66
|
+
* @returns {InteropZodSafeParseResult<T>} An object with either the parsed data (on success)
|
|
67
|
+
* or the error (on failure).
|
|
68
|
+
* @throws {Error} If the schema is not a recognized Zod v3 or v4 schema.
|
|
69
|
+
*/
|
|
70
|
+
export declare function interopSafeParse<T>(schema: InteropZodType<T>, input: unknown): InteropZodSafeParseResult<T>;
|
|
71
|
+
/**
|
|
72
|
+
* Parses the input using the provided Zod schema (v3 or v4) and returns the parsed value.
|
|
73
|
+
* Throws an error if parsing fails or if the schema is not a recognized Zod v3 or v4 schema.
|
|
74
|
+
*
|
|
75
|
+
* @template T - The expected output type of the schema.
|
|
76
|
+
* @param {InteropZodType<T>} schema - The Zod schema (v3 or v4) to use for parsing.
|
|
77
|
+
* @param {unknown} input - The input value to parse.
|
|
78
|
+
* @returns {T} The parsed value.
|
|
79
|
+
* @throws {Error} If parsing fails or the schema is not a recognized Zod v3 or v4 schema.
|
|
80
|
+
*/
|
|
81
|
+
export declare function interopParse<T>(schema: InteropZodType<T>, input: unknown): T;
|
|
82
|
+
/**
|
|
83
|
+
* Retrieves the description from a schema definition (v3, v4, or plain object), if available.
|
|
84
|
+
*
|
|
85
|
+
* @param {unknown} schema - The schema to extract the description from.
|
|
86
|
+
* @returns {string | undefined} The description of the schema, or undefined if not present.
|
|
87
|
+
*/
|
|
88
|
+
export declare function getSchemaDescription(schema: InteropZodType<unknown> | Record<string, unknown>): string | undefined;
|
|
89
|
+
/**
|
|
90
|
+
* Determines if the provided Zod schema is "shapeless".
|
|
91
|
+
* A shapeless schema is one that does not define any object shape,
|
|
92
|
+
* such as ZodString, ZodNumber, ZodBoolean, ZodAny, etc.
|
|
93
|
+
* For ZodObject, it must have no shape keys to be considered shapeless.
|
|
94
|
+
* ZodRecord schemas are considered shapeless since they define dynamic
|
|
95
|
+
* key-value mappings without fixed keys.
|
|
96
|
+
*
|
|
97
|
+
* @param schema The Zod schema to check.
|
|
98
|
+
* @returns {boolean} True if the schema is shapeless, false otherwise.
|
|
99
|
+
*/
|
|
100
|
+
export declare function isShapelessZodSchema(schema: unknown): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Determines if the provided Zod schema should be treated as a simple string schema
|
|
103
|
+
* that maps to DynamicTool. This aligns with the type-level constraint of
|
|
104
|
+
* InteropZodType<string | undefined> which only matches basic string schemas.
|
|
105
|
+
* If the provided schema is just z.string(), we can make the determination that
|
|
106
|
+
* the tool is just a generic string tool that doesn't require any input validation.
|
|
107
|
+
*
|
|
108
|
+
* This function only returns true for basic ZodString schemas, including:
|
|
109
|
+
* - Basic string schemas (z.string())
|
|
110
|
+
* - String schemas with validations (z.string().min(1), z.string().email(), etc.)
|
|
111
|
+
*
|
|
112
|
+
* This function returns false for everything else, including:
|
|
113
|
+
* - String schemas with defaults (z.string().default("value"))
|
|
114
|
+
* - Branded string schemas (z.string().brand<"UserId">())
|
|
115
|
+
* - String schemas with catch operations (z.string().catch("default"))
|
|
116
|
+
* - Optional/nullable string schemas (z.string().optional())
|
|
117
|
+
* - Transformed schemas (z.string().transform() or z.object().transform())
|
|
118
|
+
* - Object or record schemas, even if they're empty
|
|
119
|
+
* - Any other schema type
|
|
120
|
+
*
|
|
121
|
+
* @param schema The Zod schema to check.
|
|
122
|
+
* @returns {boolean} True if the schema is a basic ZodString, false otherwise.
|
|
123
|
+
*/
|
|
124
|
+
export declare function isSimpleStringZodSchema(schema: unknown): schema is InteropZodType<string | undefined>;
|
|
125
|
+
/**
|
|
126
|
+
* Determines if the provided value is an InteropZodObject (Zod v3 or v4 object schema).
|
|
127
|
+
*
|
|
128
|
+
* @param obj The value to check.
|
|
129
|
+
* @returns {boolean} True if the value is a Zod v3 or v4 object schema, false otherwise.
|
|
130
|
+
*/
|
|
131
|
+
export declare function isInteropZodObject(obj: unknown): obj is InteropZodObject;
|
|
132
|
+
/**
|
|
133
|
+
* Retrieves the shape (fields) of a Zod object schema, supporting both Zod v3 and v4.
|
|
134
|
+
*
|
|
135
|
+
* @template T - The type of the Zod object schema.
|
|
136
|
+
* @param {T} schema - The Zod object schema instance (either v3 or v4).
|
|
137
|
+
* @returns {InteropZodObjectShape<T>} The shape of the object schema.
|
|
138
|
+
* @throws {Error} If the schema is not a Zod v3 or v4 object.
|
|
139
|
+
*/
|
|
140
|
+
export declare function getInteropZodObjectShape<T extends InteropZodObject>(schema: T): InteropZodObjectShape<T>;
|
|
141
|
+
/**
|
|
142
|
+
* Extends a Zod object schema with additional fields, supporting both Zod v3 and v4.
|
|
143
|
+
*
|
|
144
|
+
* @template T - The type of the Zod object schema.
|
|
145
|
+
* @param {T} schema - The Zod object schema instance (either v3 or v4).
|
|
146
|
+
* @param {InteropZodObjectShape} extension - The fields to add to the schema.
|
|
147
|
+
* @returns {InteropZodObject} The extended Zod object schema.
|
|
148
|
+
* @throws {Error} If the schema is not a Zod v3 or v4 object.
|
|
149
|
+
*/
|
|
150
|
+
export declare function extendInteropZodObject<T extends InteropZodObject>(schema: T, extension: InteropZodObjectShape): InteropZodObject;
|
|
151
|
+
/**
|
|
152
|
+
* Returns a partial version of a Zod object schema, making all fields optional.
|
|
153
|
+
* Supports both Zod v3 and v4.
|
|
154
|
+
*
|
|
155
|
+
* @template T - The type of the Zod object schema.
|
|
156
|
+
* @param {T} schema - The Zod object schema instance (either v3 or v4).
|
|
157
|
+
* @returns {InteropZodObject} The partial Zod object schema.
|
|
158
|
+
* @throws {Error} If the schema is not a Zod v3 or v4 object.
|
|
159
|
+
*/
|
|
160
|
+
export declare function interopZodObjectPartial<T extends InteropZodObject>(schema: T): InteropZodObject;
|
|
161
|
+
export declare function interopZodObjectPassthrough<T extends InteropZodObject>(schema: T): InteropZodObject;
|
|
162
|
+
/**
|
|
163
|
+
* Returns a getter function for the default value of a Zod schema, if one is defined.
|
|
164
|
+
* Supports both Zod v3 and v4 schemas. If the schema has a default value,
|
|
165
|
+
* the returned function will return that value when called. If no default is defined,
|
|
166
|
+
* returns undefined.
|
|
167
|
+
*
|
|
168
|
+
* @template T - The type of the Zod schema.
|
|
169
|
+
* @param {T} schema - The Zod schema instance (either v3 or v4).
|
|
170
|
+
* @returns {(() => InferInteropZodOutput<T>) | undefined} A function that returns the default value, or undefined if no default is set.
|
|
171
|
+
*/
|
|
172
|
+
export declare function getInteropZodDefaultGetter<T extends InteropZodType>(schema: T): (() => InferInteropZodOutput<T>) | undefined;
|
|
173
|
+
export {};
|