@composio/json-schema-to-zod 0.1.19 → 0.1.20
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/index.cjs +593 -767
- package/dist/index.d.cts +35 -15
- package/dist/index.d.mts +47 -0
- package/dist/index.mjs +628 -0
- package/package.json +18 -10
- package/dist/index.d.ts +0 -27
- package/dist/index.js +0 -793
package/dist/index.cjs
CHANGED
|
@@ -1,830 +1,656 @@
|
|
|
1
|
-
|
|
1
|
+
//#region rolldown:runtime
|
|
2
2
|
var __create = Object.create;
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
8
|
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
19
21
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var index_exports = {};
|
|
32
|
-
__export(index_exports, {
|
|
33
|
-
jsonSchemaToZod: () => jsonSchemaToZod
|
|
34
|
-
});
|
|
35
|
-
module.exports = __toCommonJS(index_exports);
|
|
36
|
-
|
|
37
|
-
// src/parsers/parse-schema.ts
|
|
38
|
-
var z16 = __toESM(require("zod/v3"), 1);
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
39
26
|
|
|
40
|
-
|
|
41
|
-
|
|
27
|
+
//#endregion
|
|
28
|
+
let zod_v3 = require("zod/v3");
|
|
29
|
+
zod_v3 = __toESM(zod_v3);
|
|
42
30
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
31
|
+
//#region src/utils/half.ts
|
|
32
|
+
const half = (arr) => {
|
|
33
|
+
return [arr.slice(0, arr.length / 2), arr.slice(arr.length / 2)];
|
|
46
34
|
};
|
|
47
35
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/parsers/parse-all-of.ts
|
|
38
|
+
const originalIndex = Symbol("Original index");
|
|
39
|
+
const ensureOriginalIndex = (arr) => {
|
|
40
|
+
const newArr = [];
|
|
41
|
+
for (let i = 0; i < arr.length; i++) {
|
|
42
|
+
const item = arr[i];
|
|
43
|
+
if (typeof item === "boolean") newArr.push(item ? { [originalIndex]: i } : {
|
|
44
|
+
[originalIndex]: i,
|
|
45
|
+
not: {}
|
|
46
|
+
});
|
|
47
|
+
else if (originalIndex in item) return arr;
|
|
48
|
+
else newArr.push({
|
|
49
|
+
...item,
|
|
50
|
+
[originalIndex]: i
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return newArr;
|
|
63
54
|
};
|
|
64
55
|
function parseAllOf(jsonSchema, refs) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
56
|
+
if (jsonSchema.allOf.length === 0) return zod_v3.z.never();
|
|
57
|
+
if (jsonSchema.allOf.length === 1) {
|
|
58
|
+
const item = jsonSchema.allOf[0];
|
|
59
|
+
return parseSchema(item, {
|
|
60
|
+
...refs,
|
|
61
|
+
path: [
|
|
62
|
+
...refs.path,
|
|
63
|
+
"allOf",
|
|
64
|
+
item[originalIndex]
|
|
65
|
+
]
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
const [left, right] = half(ensureOriginalIndex(jsonSchema.allOf));
|
|
69
|
+
return zod_v3.z.intersection(parseAllOf({ allOf: left }, refs), parseAllOf({ allOf: right }, refs));
|
|
77
70
|
}
|
|
78
71
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/parsers/parse-any-of.ts
|
|
74
|
+
const parseAnyOf = (jsonSchema, refs) => {
|
|
75
|
+
return jsonSchema.anyOf.length ? jsonSchema.anyOf.length === 1 ? parseSchema(jsonSchema.anyOf[0], {
|
|
76
|
+
...refs,
|
|
77
|
+
path: [
|
|
78
|
+
...refs.path,
|
|
79
|
+
"anyOf",
|
|
80
|
+
0
|
|
81
|
+
]
|
|
82
|
+
}) : zod_v3.z.union(jsonSchema.anyOf.map((schema, i) => parseSchema(schema, {
|
|
83
|
+
...refs,
|
|
84
|
+
path: [
|
|
85
|
+
...refs.path,
|
|
86
|
+
"anyOf",
|
|
87
|
+
i
|
|
88
|
+
]
|
|
89
|
+
}))) : zod_v3.z.any();
|
|
90
90
|
};
|
|
91
91
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// src/utils/extend-schema.ts
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/utils/extend-schema.ts
|
|
96
94
|
function extendSchemaWithMessage(zodSchema, jsonSchema, key, extend) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
95
|
+
const value = jsonSchema[key];
|
|
96
|
+
if (value !== void 0) {
|
|
97
|
+
const errorMessage = jsonSchema.errorMessage?.[key];
|
|
98
|
+
return extend(zodSchema, value, errorMessage);
|
|
99
|
+
}
|
|
100
|
+
return zodSchema;
|
|
103
101
|
}
|
|
104
102
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region src/utils/its.ts
|
|
105
|
+
const its = {
|
|
106
|
+
an: {
|
|
107
|
+
object: (x) => x.type === "object" || !x.type && (x.properties !== void 0 || x.additionalProperties !== void 0 || x.patternProperties !== void 0),
|
|
108
|
+
array: (x) => x.type === "array",
|
|
109
|
+
anyOf: (x) => x.anyOf !== void 0,
|
|
110
|
+
allOf: (x) => x.allOf !== void 0,
|
|
111
|
+
enum: (x) => x.enum !== void 0
|
|
112
|
+
},
|
|
113
|
+
a: {
|
|
114
|
+
nullable: (x) => x.nullable === true,
|
|
115
|
+
multipleType: (x) => Array.isArray(x.type),
|
|
116
|
+
not: (x) => x.not !== void 0,
|
|
117
|
+
const: (x) => x.const !== void 0,
|
|
118
|
+
primitive: (x, p) => x.type === p,
|
|
119
|
+
conditional: (x) => Boolean("if" in x && x.if && "then" in x && "else" in x && x.then && x.else),
|
|
120
|
+
oneOf: (x) => x.oneOf !== void 0
|
|
121
|
+
}
|
|
123
122
|
};
|
|
124
123
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
);
|
|
180
|
-
if (typeof jsonSchema.min === "number" && typeof jsonSchema.minItems !== "number") {
|
|
181
|
-
zodSchema = extendSchemaWithMessage(
|
|
182
|
-
zodSchema,
|
|
183
|
-
{ ...jsonSchema, minItems: jsonSchema.min },
|
|
184
|
-
"minItems",
|
|
185
|
-
(zs, minItems, errorMessage) => zs.min(minItems, errorMessage)
|
|
186
|
-
);
|
|
187
|
-
}
|
|
188
|
-
if (typeof jsonSchema.max === "number" && typeof jsonSchema.maxItems !== "number") {
|
|
189
|
-
zodSchema = extendSchemaWithMessage(
|
|
190
|
-
zodSchema,
|
|
191
|
-
{ ...jsonSchema, maxItems: jsonSchema.max },
|
|
192
|
-
"maxItems",
|
|
193
|
-
(zs, maxItems, errorMessage) => zs.max(maxItems, errorMessage)
|
|
194
|
-
);
|
|
195
|
-
}
|
|
196
|
-
return zodSchema;
|
|
124
|
+
//#endregion
|
|
125
|
+
//#region src/parsers/parse-array.ts
|
|
126
|
+
const parseArray = (jsonSchema, refs) => {
|
|
127
|
+
if (its.an.anyOf(jsonSchema)) {
|
|
128
|
+
const types = /* @__PURE__ */ new Set();
|
|
129
|
+
const itemsSchemas = [];
|
|
130
|
+
jsonSchema.anyOf.forEach((option) => {
|
|
131
|
+
if (typeof option === "object" && option.type) types.add(typeof option.type === "string" ? option.type : option.type[0]);
|
|
132
|
+
if (typeof option === "object" && option.items) {
|
|
133
|
+
const optionItems = option.items;
|
|
134
|
+
if (!Array.isArray(optionItems) && typeof optionItems === "object") itemsSchemas.push(optionItems);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
let finalItems;
|
|
138
|
+
if (itemsSchemas.length === 1) finalItems = itemsSchemas[0];
|
|
139
|
+
else if (itemsSchemas.length > 1) finalItems = { anyOf: itemsSchemas };
|
|
140
|
+
const newSchema = {
|
|
141
|
+
...types.size > 0 ? { type: Array.from(types) } : { type: "array" },
|
|
142
|
+
...finalItems && { items: finalItems }
|
|
143
|
+
};
|
|
144
|
+
[
|
|
145
|
+
"default",
|
|
146
|
+
"description",
|
|
147
|
+
"examples",
|
|
148
|
+
"title"
|
|
149
|
+
].forEach((field) => {
|
|
150
|
+
const value = jsonSchema[field];
|
|
151
|
+
if (value !== void 0) newSchema[field] = value;
|
|
152
|
+
});
|
|
153
|
+
return parseSchema(newSchema, refs);
|
|
154
|
+
}
|
|
155
|
+
if (Array.isArray(jsonSchema.items)) return zod_v3.z.tuple(jsonSchema.items.map((v, i) => parseSchema(v, {
|
|
156
|
+
...refs,
|
|
157
|
+
path: [
|
|
158
|
+
...refs.path,
|
|
159
|
+
"items",
|
|
160
|
+
i
|
|
161
|
+
]
|
|
162
|
+
})));
|
|
163
|
+
let zodSchema = !jsonSchema.items ? zod_v3.z.array(zod_v3.z.any()) : zod_v3.z.array(parseSchema(jsonSchema.items, {
|
|
164
|
+
...refs,
|
|
165
|
+
path: [...refs.path, "items"]
|
|
166
|
+
}));
|
|
167
|
+
zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "minItems", (zs, minItems, errorMessage) => zs.min(minItems, errorMessage));
|
|
168
|
+
zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "maxItems", (zs, maxItems, errorMessage) => zs.max(maxItems, errorMessage));
|
|
169
|
+
if (typeof jsonSchema.min === "number" && typeof jsonSchema.minItems !== "number") zodSchema = extendSchemaWithMessage(zodSchema, {
|
|
170
|
+
...jsonSchema,
|
|
171
|
+
minItems: jsonSchema.min
|
|
172
|
+
}, "minItems", (zs, minItems, errorMessage) => zs.min(minItems, errorMessage));
|
|
173
|
+
if (typeof jsonSchema.max === "number" && typeof jsonSchema.maxItems !== "number") zodSchema = extendSchemaWithMessage(zodSchema, {
|
|
174
|
+
...jsonSchema,
|
|
175
|
+
maxItems: jsonSchema.max
|
|
176
|
+
}, "maxItems", (zs, maxItems, errorMessage) => zs.max(maxItems, errorMessage));
|
|
177
|
+
return zodSchema;
|
|
197
178
|
};
|
|
198
179
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region src/parsers/parse-boolean.ts
|
|
182
|
+
const parseBoolean = (_jsonSchema) => {
|
|
183
|
+
return zod_v3.z.boolean();
|
|
203
184
|
};
|
|
204
185
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
186
|
+
//#endregion
|
|
187
|
+
//#region src/parsers/parse-const.ts
|
|
188
|
+
const parseConst = (jsonSchema) => {
|
|
189
|
+
return zod_v3.z.literal(jsonSchema.const);
|
|
209
190
|
};
|
|
210
191
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
192
|
+
//#endregion
|
|
193
|
+
//#region src/parsers/parse-default.ts
|
|
194
|
+
const parseDefault = (_jsonSchema) => {
|
|
195
|
+
return zod_v3.z.any();
|
|
215
196
|
};
|
|
216
197
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
return import_v37.z.literal(jsonSchema.enum[0]);
|
|
225
|
-
}
|
|
226
|
-
if (jsonSchema.enum.every((x) => typeof x === "string")) {
|
|
227
|
-
return import_v37.z.enum(jsonSchema.enum);
|
|
228
|
-
}
|
|
229
|
-
return import_v37.z.union(
|
|
230
|
-
jsonSchema.enum.map((x) => import_v37.z.literal(x))
|
|
231
|
-
);
|
|
198
|
+
//#endregion
|
|
199
|
+
//#region src/parsers/parse-enum.ts
|
|
200
|
+
const parseEnum = (jsonSchema) => {
|
|
201
|
+
if (jsonSchema.enum.length === 0) return zod_v3.z.never();
|
|
202
|
+
if (jsonSchema.enum.length === 1) return zod_v3.z.literal(jsonSchema.enum[0]);
|
|
203
|
+
if (jsonSchema.enum.every((x) => typeof x === "string")) return zod_v3.z.enum(jsonSchema.enum);
|
|
204
|
+
return zod_v3.z.union(jsonSchema.enum.map((x) => zod_v3.z.literal(x)));
|
|
232
205
|
};
|
|
233
206
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
207
|
+
//#endregion
|
|
208
|
+
//#region src/parsers/parse-if-then-else.ts
|
|
209
|
+
const parseIfThenElse = (jsonSchema, refs) => {
|
|
210
|
+
const $if = parseSchema(jsonSchema.if, {
|
|
211
|
+
...refs,
|
|
212
|
+
path: [...refs.path, "if"]
|
|
213
|
+
});
|
|
214
|
+
const $then = parseSchema(jsonSchema.then, {
|
|
215
|
+
...refs,
|
|
216
|
+
path: [...refs.path, "then"]
|
|
217
|
+
});
|
|
218
|
+
const $else = parseSchema(jsonSchema.else, {
|
|
219
|
+
...refs,
|
|
220
|
+
path: [...refs.path, "else"]
|
|
221
|
+
});
|
|
222
|
+
return zod_v3.z.union([$then, $else]).superRefine((value, ctx) => {
|
|
223
|
+
const result = $if.safeParse(value).success ? $then.safeParse(value) : $else.safeParse(value);
|
|
224
|
+
if (!result.success) result.error.errors.forEach((error) => ctx.addIssue(error));
|
|
225
|
+
});
|
|
252
226
|
};
|
|
253
227
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
228
|
+
//#endregion
|
|
229
|
+
//#region src/parsers/parse-multiple-type.ts
|
|
230
|
+
const parseMultipleType = (jsonSchema, refs) => {
|
|
231
|
+
return zod_v3.z.union(jsonSchema.type.map((type) => parseSchema({
|
|
232
|
+
...jsonSchema,
|
|
233
|
+
type
|
|
234
|
+
}, refs)));
|
|
260
235
|
};
|
|
261
236
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
}).safeParse(value).success,
|
|
270
|
-
"Invalid input: Should NOT be valid against schema"
|
|
271
|
-
);
|
|
237
|
+
//#endregion
|
|
238
|
+
//#region src/parsers/parse-not.ts
|
|
239
|
+
const parseNot = (jsonSchema, refs) => {
|
|
240
|
+
return zod_v3.z.any().refine((value) => !parseSchema(jsonSchema.not, {
|
|
241
|
+
...refs,
|
|
242
|
+
path: [...refs.path, "not"]
|
|
243
|
+
}).safeParse(value).success, "Invalid input: Should NOT be valid against schema");
|
|
272
244
|
};
|
|
273
245
|
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
246
|
+
//#endregion
|
|
247
|
+
//#region src/parsers/parse-null.ts
|
|
248
|
+
const parseNull = (_jsonSchema) => {
|
|
249
|
+
return zod_v3.z.null();
|
|
278
250
|
};
|
|
279
251
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
return acc;
|
|
252
|
+
//#endregion
|
|
253
|
+
//#region src/utils/omit.ts
|
|
254
|
+
const omit = (obj, ...keys) => Object.keys(obj).reduce((acc, key) => {
|
|
255
|
+
if (!keys.includes(key)) acc[key] = obj[key];
|
|
256
|
+
return acc;
|
|
286
257
|
}, {});
|
|
287
258
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
259
|
+
//#endregion
|
|
260
|
+
//#region src/parsers/parse-nullable.ts
|
|
261
|
+
/**
|
|
262
|
+
* For compatibility with open api 3.0 nullable
|
|
263
|
+
*/
|
|
264
|
+
const parseNullable = (jsonSchema, refs) => {
|
|
265
|
+
const hasNullDefault = jsonSchema.default === null;
|
|
266
|
+
const zodSchema = parseSchema(hasNullDefault ? omit(omit(jsonSchema, "nullable"), "default") : omit(jsonSchema, "nullable"), refs, true).nullable();
|
|
267
|
+
return hasNullDefault ? zodSchema.default(null) : zodSchema;
|
|
294
268
|
};
|
|
295
269
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
if (typeof jsonSchema.minimum === "number") {
|
|
331
|
-
if (jsonSchema.exclusiveMinimum === true) {
|
|
332
|
-
zodSchema = extendSchemaWithMessage(
|
|
333
|
-
zodSchema,
|
|
334
|
-
jsonSchema,
|
|
335
|
-
"minimum",
|
|
336
|
-
(zs, minimum, errorMsg) => zs.gt(minimum, errorMsg)
|
|
337
|
-
);
|
|
338
|
-
} else {
|
|
339
|
-
zodSchema = extendSchemaWithMessage(
|
|
340
|
-
zodSchema,
|
|
341
|
-
jsonSchema,
|
|
342
|
-
"minimum",
|
|
343
|
-
(zs, minimum, errorMsg) => zs.gte(minimum, errorMsg)
|
|
344
|
-
);
|
|
345
|
-
}
|
|
346
|
-
} else if (typeof jsonSchema.exclusiveMinimum === "number") {
|
|
347
|
-
zodSchema = extendSchemaWithMessage(
|
|
348
|
-
zodSchema,
|
|
349
|
-
jsonSchema,
|
|
350
|
-
"exclusiveMinimum",
|
|
351
|
-
(zs, exclusiveMinimum, errorMsg) => zs.gt(exclusiveMinimum, errorMsg)
|
|
352
|
-
);
|
|
353
|
-
}
|
|
354
|
-
if (typeof jsonSchema.maximum === "number") {
|
|
355
|
-
if (jsonSchema.exclusiveMaximum === true) {
|
|
356
|
-
zodSchema = extendSchemaWithMessage(
|
|
357
|
-
zodSchema,
|
|
358
|
-
jsonSchema,
|
|
359
|
-
"maximum",
|
|
360
|
-
(zs, maximum, errorMsg) => zs.lt(maximum, errorMsg)
|
|
361
|
-
);
|
|
362
|
-
} else {
|
|
363
|
-
zodSchema = extendSchemaWithMessage(
|
|
364
|
-
zodSchema,
|
|
365
|
-
jsonSchema,
|
|
366
|
-
"maximum",
|
|
367
|
-
(zs, maximum, errorMsg) => zs.lte(maximum, errorMsg)
|
|
368
|
-
);
|
|
369
|
-
}
|
|
370
|
-
} else if (typeof jsonSchema.exclusiveMaximum === "number") {
|
|
371
|
-
zodSchema = extendSchemaWithMessage(
|
|
372
|
-
zodSchema,
|
|
373
|
-
jsonSchema,
|
|
374
|
-
"exclusiveMaximum",
|
|
375
|
-
(zs, exclusiveMaximum, errorMsg) => zs.lt(exclusiveMaximum, errorMsg)
|
|
376
|
-
);
|
|
377
|
-
}
|
|
378
|
-
if (typeof jsonSchema.min === "number" && typeof jsonSchema.minimum !== "number") {
|
|
379
|
-
zodSchema = extendSchemaWithMessage(
|
|
380
|
-
zodSchema,
|
|
381
|
-
{ ...jsonSchema, minimum: jsonSchema.min },
|
|
382
|
-
"minimum",
|
|
383
|
-
(zs, minimum, errorMsg) => zs.gte(minimum, errorMsg)
|
|
384
|
-
);
|
|
385
|
-
}
|
|
386
|
-
if (typeof jsonSchema.max === "number" && typeof jsonSchema.maximum !== "number") {
|
|
387
|
-
zodSchema = extendSchemaWithMessage(
|
|
388
|
-
zodSchema,
|
|
389
|
-
{ ...jsonSchema, maximum: jsonSchema.max },
|
|
390
|
-
"maximum",
|
|
391
|
-
(zs, maximum, errorMsg) => zs.lte(maximum, errorMsg)
|
|
392
|
-
);
|
|
393
|
-
}
|
|
394
|
-
return zodSchema;
|
|
270
|
+
//#endregion
|
|
271
|
+
//#region src/parsers/parse-number.ts
|
|
272
|
+
const parseNumber = (jsonSchema) => {
|
|
273
|
+
let zodSchema = zod_v3.z.number();
|
|
274
|
+
let isInteger = false;
|
|
275
|
+
if (jsonSchema.type === "integer") {
|
|
276
|
+
isInteger = true;
|
|
277
|
+
zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "type", (zs, _, errorMsg) => zs.int(errorMsg));
|
|
278
|
+
} else if (jsonSchema.format === "int64") {
|
|
279
|
+
isInteger = true;
|
|
280
|
+
zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "format", (zs, _, errorMsg) => zs.int(errorMsg));
|
|
281
|
+
}
|
|
282
|
+
zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "multipleOf", (zs, multipleOf, errorMsg) => {
|
|
283
|
+
if (multipleOf === 1) {
|
|
284
|
+
if (isInteger) return zs;
|
|
285
|
+
return zs.int(errorMsg);
|
|
286
|
+
}
|
|
287
|
+
return zs.multipleOf(multipleOf, errorMsg);
|
|
288
|
+
});
|
|
289
|
+
if (typeof jsonSchema.minimum === "number") if (jsonSchema.exclusiveMinimum === true) zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "minimum", (zs, minimum, errorMsg) => zs.gt(minimum, errorMsg));
|
|
290
|
+
else zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "minimum", (zs, minimum, errorMsg) => zs.gte(minimum, errorMsg));
|
|
291
|
+
else if (typeof jsonSchema.exclusiveMinimum === "number") zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "exclusiveMinimum", (zs, exclusiveMinimum, errorMsg) => zs.gt(exclusiveMinimum, errorMsg));
|
|
292
|
+
if (typeof jsonSchema.maximum === "number") if (jsonSchema.exclusiveMaximum === true) zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "maximum", (zs, maximum, errorMsg) => zs.lt(maximum, errorMsg));
|
|
293
|
+
else zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "maximum", (zs, maximum, errorMsg) => zs.lte(maximum, errorMsg));
|
|
294
|
+
else if (typeof jsonSchema.exclusiveMaximum === "number") zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "exclusiveMaximum", (zs, exclusiveMaximum, errorMsg) => zs.lt(exclusiveMaximum, errorMsg));
|
|
295
|
+
if (typeof jsonSchema.min === "number" && typeof jsonSchema.minimum !== "number") zodSchema = extendSchemaWithMessage(zodSchema, {
|
|
296
|
+
...jsonSchema,
|
|
297
|
+
minimum: jsonSchema.min
|
|
298
|
+
}, "minimum", (zs, minimum, errorMsg) => zs.gte(minimum, errorMsg));
|
|
299
|
+
if (typeof jsonSchema.max === "number" && typeof jsonSchema.maximum !== "number") zodSchema = extendSchemaWithMessage(zodSchema, {
|
|
300
|
+
...jsonSchema,
|
|
301
|
+
maximum: jsonSchema.max
|
|
302
|
+
}, "maximum", (zs, maximum, errorMsg) => zs.lte(maximum, errorMsg));
|
|
303
|
+
return zodSchema;
|
|
395
304
|
};
|
|
396
305
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
code: "invalid_union",
|
|
427
|
-
unionErrors,
|
|
428
|
-
message: "Invalid input: Should pass single schema"
|
|
429
|
-
});
|
|
430
|
-
}
|
|
431
|
-
});
|
|
306
|
+
//#endregion
|
|
307
|
+
//#region src/parsers/parse-one-of.ts
|
|
308
|
+
const parseOneOf = (jsonSchema, refs) => {
|
|
309
|
+
if (!jsonSchema.oneOf.length) return zod_v3.z.any();
|
|
310
|
+
if (jsonSchema.oneOf.length === 1) return parseSchema(jsonSchema.oneOf[0], {
|
|
311
|
+
...refs,
|
|
312
|
+
path: [
|
|
313
|
+
...refs.path,
|
|
314
|
+
"oneOf",
|
|
315
|
+
0
|
|
316
|
+
]
|
|
317
|
+
});
|
|
318
|
+
return zod_v3.z.any().superRefine((x, ctx) => {
|
|
319
|
+
const schemas = jsonSchema.oneOf.map((schema, i) => parseSchema(schema, {
|
|
320
|
+
...refs,
|
|
321
|
+
path: [
|
|
322
|
+
...refs.path,
|
|
323
|
+
"oneOf",
|
|
324
|
+
i
|
|
325
|
+
]
|
|
326
|
+
}));
|
|
327
|
+
const unionErrors = schemas.reduce((errors, schema) => ((result) => result.error ? [...errors, result.error] : errors)(schema.safeParse(x)), []);
|
|
328
|
+
if (schemas.length - unionErrors.length !== 1) ctx.addIssue({
|
|
329
|
+
path: ctx.path,
|
|
330
|
+
code: "invalid_union",
|
|
331
|
+
unionErrors,
|
|
332
|
+
message: "Invalid input: Should pass single schema"
|
|
333
|
+
});
|
|
334
|
+
});
|
|
432
335
|
};
|
|
433
336
|
|
|
434
|
-
|
|
337
|
+
//#endregion
|
|
338
|
+
//#region src/parsers/parse-object.ts
|
|
435
339
|
function parseObjectProperties(objectSchema, refs) {
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
properties[key] = propZodSchema.optional().default(null);
|
|
462
|
-
} else {
|
|
463
|
-
properties[key] = propZodSchema.nullable().optional().default(null);
|
|
464
|
-
}
|
|
465
|
-
} else {
|
|
466
|
-
properties[key] = propZodSchema.optional().default(propJsonSchema.default);
|
|
467
|
-
}
|
|
468
|
-
} else {
|
|
469
|
-
properties[key] = required ? propZodSchema : propZodSchema.optional();
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
return z14.object(properties);
|
|
340
|
+
if (!objectSchema.properties) return zod_v3.object({});
|
|
341
|
+
const propertyKeys = Object.keys(objectSchema.properties);
|
|
342
|
+
if (propertyKeys.length === 0) return zod_v3.object({});
|
|
343
|
+
const properties = {};
|
|
344
|
+
for (const key of propertyKeys) {
|
|
345
|
+
const propJsonSchema = objectSchema.properties[key];
|
|
346
|
+
const propZodSchema = parseSchema(propJsonSchema, {
|
|
347
|
+
...refs,
|
|
348
|
+
path: [
|
|
349
|
+
...refs.path,
|
|
350
|
+
"properties",
|
|
351
|
+
key
|
|
352
|
+
]
|
|
353
|
+
});
|
|
354
|
+
const required = Array.isArray(objectSchema.required) ? objectSchema.required.includes(key) : false;
|
|
355
|
+
if (!required && propJsonSchema && typeof propJsonSchema === "object" && "default" in propJsonSchema) if (propJsonSchema.default === null) {
|
|
356
|
+
const hasAnyOfWithNull = propJsonSchema.anyOf && Array.isArray(propJsonSchema.anyOf) && propJsonSchema.anyOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
|
|
357
|
+
const hasOneOfWithNull = propJsonSchema.oneOf && Array.isArray(propJsonSchema.oneOf) && propJsonSchema.oneOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
|
|
358
|
+
const isNullable = "nullable" in propJsonSchema && propJsonSchema.nullable === true;
|
|
359
|
+
if (hasAnyOfWithNull || hasOneOfWithNull || isNullable) properties[key] = propZodSchema.optional().default(null);
|
|
360
|
+
else properties[key] = propZodSchema.nullable().optional().default(null);
|
|
361
|
+
} else properties[key] = propZodSchema.optional().default(propJsonSchema.default);
|
|
362
|
+
else properties[key] = required ? propZodSchema : propZodSchema.optional();
|
|
363
|
+
}
|
|
364
|
+
return zod_v3.object(properties);
|
|
473
365
|
}
|
|
474
366
|
function parseObject(objectSchema, refs) {
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
output = propertiesSchema.catchall(additionalProperties);
|
|
568
|
-
}
|
|
569
|
-
} else {
|
|
570
|
-
output = propertiesSchema.strict();
|
|
571
|
-
}
|
|
572
|
-
} else {
|
|
573
|
-
if (hasPatternProperties) {
|
|
574
|
-
output = zodSchema;
|
|
575
|
-
} else if (additionalProperties) {
|
|
576
|
-
if (additionalProperties instanceof z14.ZodNever) {
|
|
577
|
-
output = z14.object({}).strict();
|
|
578
|
-
} else if (isAdditionalPropertiesTrue) {
|
|
579
|
-
output = z14.object({}).passthrough();
|
|
580
|
-
} else {
|
|
581
|
-
output = z14.record(additionalProperties);
|
|
582
|
-
}
|
|
583
|
-
} else {
|
|
584
|
-
output = z14.object({}).passthrough();
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
if (its.an.anyOf(objectSchema)) {
|
|
588
|
-
output = output.and(
|
|
589
|
-
parseAnyOf(
|
|
590
|
-
{
|
|
591
|
-
...objectSchema,
|
|
592
|
-
anyOf: objectSchema.anyOf.map(
|
|
593
|
-
(x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? { ...x, type: "object" } : x
|
|
594
|
-
)
|
|
595
|
-
},
|
|
596
|
-
refs
|
|
597
|
-
)
|
|
598
|
-
);
|
|
599
|
-
}
|
|
600
|
-
if (its.a.oneOf(objectSchema)) {
|
|
601
|
-
output = output.and(
|
|
602
|
-
parseOneOf(
|
|
603
|
-
{
|
|
604
|
-
...objectSchema,
|
|
605
|
-
oneOf: objectSchema.oneOf.map(
|
|
606
|
-
(x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? { ...x, type: "object" } : x
|
|
607
|
-
)
|
|
608
|
-
},
|
|
609
|
-
refs
|
|
610
|
-
)
|
|
611
|
-
);
|
|
612
|
-
}
|
|
613
|
-
if (its.an.allOf(objectSchema)) {
|
|
614
|
-
output = output.and(
|
|
615
|
-
parseAllOf(
|
|
616
|
-
{
|
|
617
|
-
...objectSchema,
|
|
618
|
-
allOf: objectSchema.allOf.map(
|
|
619
|
-
(x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? { ...x, type: "object" } : x
|
|
620
|
-
)
|
|
621
|
-
},
|
|
622
|
-
refs
|
|
623
|
-
)
|
|
624
|
-
);
|
|
625
|
-
}
|
|
626
|
-
return output;
|
|
367
|
+
const hasPatternProperties = Object.keys(objectSchema.patternProperties ?? {}).length > 0;
|
|
368
|
+
const normalizedSchema = objectSchema.type === "object" ? objectSchema : {
|
|
369
|
+
...objectSchema,
|
|
370
|
+
type: "object"
|
|
371
|
+
};
|
|
372
|
+
const propertiesSchema = parseObjectProperties(normalizedSchema, refs);
|
|
373
|
+
let zodSchema = propertiesSchema;
|
|
374
|
+
const additionalProperties = normalizedSchema.additionalProperties !== void 0 ? parseSchema(normalizedSchema.additionalProperties, {
|
|
375
|
+
...refs,
|
|
376
|
+
path: [...refs.path, "additionalProperties"]
|
|
377
|
+
}) : void 0;
|
|
378
|
+
const isAdditionalPropertiesTrue = normalizedSchema.additionalProperties === true;
|
|
379
|
+
if (normalizedSchema.patternProperties) {
|
|
380
|
+
const parsedPatternProperties = Object.fromEntries(Object.entries(normalizedSchema.patternProperties).map(([key, value]) => {
|
|
381
|
+
return [key, parseSchema(value, {
|
|
382
|
+
...refs,
|
|
383
|
+
path: [
|
|
384
|
+
...refs.path,
|
|
385
|
+
"patternProperties",
|
|
386
|
+
key
|
|
387
|
+
]
|
|
388
|
+
})];
|
|
389
|
+
}));
|
|
390
|
+
const patternPropertyValues = Object.values(parsedPatternProperties);
|
|
391
|
+
if (propertiesSchema) if (additionalProperties) zodSchema = propertiesSchema.catchall(zod_v3.union([...patternPropertyValues, additionalProperties]));
|
|
392
|
+
else if (Object.keys(parsedPatternProperties).length > 1) zodSchema = propertiesSchema.catchall(zod_v3.union(patternPropertyValues));
|
|
393
|
+
else zodSchema = propertiesSchema.catchall(patternPropertyValues[0]);
|
|
394
|
+
else if (additionalProperties) zodSchema = zod_v3.record(zod_v3.union([...patternPropertyValues, additionalProperties]));
|
|
395
|
+
else if (patternPropertyValues.length > 1) zodSchema = zod_v3.record(zod_v3.union(patternPropertyValues));
|
|
396
|
+
else zodSchema = zod_v3.record(patternPropertyValues[0]);
|
|
397
|
+
const objectPropertyKeys = new Set(Object.keys(normalizedSchema.properties ?? {}));
|
|
398
|
+
zodSchema = zodSchema.superRefine((value, ctx) => {
|
|
399
|
+
for (const key in value) {
|
|
400
|
+
let wasMatched = objectPropertyKeys.has(key);
|
|
401
|
+
for (const patternPropertyKey in normalizedSchema.patternProperties) {
|
|
402
|
+
const regex = new RegExp(patternPropertyKey);
|
|
403
|
+
if (key.match(regex)) {
|
|
404
|
+
wasMatched = true;
|
|
405
|
+
const result = parsedPatternProperties[patternPropertyKey].safeParse(value[key]);
|
|
406
|
+
if (!result.success) ctx.addIssue({
|
|
407
|
+
path: [...ctx.path, key],
|
|
408
|
+
code: "custom",
|
|
409
|
+
message: `Invalid input: Key matching regex /${key}/ must match schema`,
|
|
410
|
+
params: { issues: result.error.issues }
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
if (!wasMatched && additionalProperties) {
|
|
415
|
+
const result = additionalProperties.safeParse(value[key]);
|
|
416
|
+
if (!result.success) ctx.addIssue({
|
|
417
|
+
path: [...ctx.path, key],
|
|
418
|
+
code: "custom",
|
|
419
|
+
message: "Invalid input: must match catchall schema",
|
|
420
|
+
params: { issues: result.error.issues }
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
let output;
|
|
427
|
+
if (propertiesSchema) if (hasPatternProperties) output = zodSchema;
|
|
428
|
+
else if (additionalProperties) if (additionalProperties instanceof zod_v3.ZodNever) output = propertiesSchema.strict();
|
|
429
|
+
else if (isAdditionalPropertiesTrue) output = propertiesSchema.passthrough();
|
|
430
|
+
else output = propertiesSchema.catchall(additionalProperties);
|
|
431
|
+
else output = propertiesSchema.strict();
|
|
432
|
+
else if (hasPatternProperties) output = zodSchema;
|
|
433
|
+
else if (additionalProperties) if (additionalProperties instanceof zod_v3.ZodNever) output = zod_v3.object({}).strict();
|
|
434
|
+
else if (isAdditionalPropertiesTrue) output = zod_v3.object({}).passthrough();
|
|
435
|
+
else output = zod_v3.record(additionalProperties);
|
|
436
|
+
else output = zod_v3.object({}).passthrough();
|
|
437
|
+
if (its.an.anyOf(objectSchema)) output = output.and(parseAnyOf({
|
|
438
|
+
...objectSchema,
|
|
439
|
+
anyOf: objectSchema.anyOf.map((x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? {
|
|
440
|
+
...x,
|
|
441
|
+
type: "object"
|
|
442
|
+
} : x)
|
|
443
|
+
}, refs));
|
|
444
|
+
if (its.a.oneOf(objectSchema)) output = output.and(parseOneOf({
|
|
445
|
+
...objectSchema,
|
|
446
|
+
oneOf: objectSchema.oneOf.map((x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? {
|
|
447
|
+
...x,
|
|
448
|
+
type: "object"
|
|
449
|
+
} : x)
|
|
450
|
+
}, refs));
|
|
451
|
+
if (its.an.allOf(objectSchema)) output = output.and(parseAllOf({
|
|
452
|
+
...objectSchema,
|
|
453
|
+
allOf: objectSchema.allOf.map((x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? {
|
|
454
|
+
...x,
|
|
455
|
+
type: "object"
|
|
456
|
+
} : x)
|
|
457
|
+
}, refs));
|
|
458
|
+
return output;
|
|
627
459
|
}
|
|
628
460
|
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
(zs, pattern, errorMsg) => zs.regex(new RegExp(pattern), errorMsg)
|
|
672
|
-
);
|
|
673
|
-
zodSchema = extendSchemaWithMessage(
|
|
674
|
-
zodSchema,
|
|
675
|
-
jsonSchema,
|
|
676
|
-
"minLength",
|
|
677
|
-
(zs, minLength, errorMsg) => zs.min(minLength, errorMsg)
|
|
678
|
-
);
|
|
679
|
-
zodSchema = extendSchemaWithMessage(
|
|
680
|
-
zodSchema,
|
|
681
|
-
jsonSchema,
|
|
682
|
-
"maxLength",
|
|
683
|
-
(zs, maxLength, errorMsg) => zs.max(maxLength, errorMsg)
|
|
684
|
-
);
|
|
685
|
-
if (typeof jsonSchema.min === "number" && typeof jsonSchema.minLength !== "number") {
|
|
686
|
-
zodSchema = extendSchemaWithMessage(
|
|
687
|
-
zodSchema,
|
|
688
|
-
{ ...jsonSchema, minLength: jsonSchema.min },
|
|
689
|
-
"minLength",
|
|
690
|
-
(zs, minLength, errorMsg) => zs.min(minLength, errorMsg)
|
|
691
|
-
);
|
|
692
|
-
}
|
|
693
|
-
if (typeof jsonSchema.max === "number" && typeof jsonSchema.maxLength !== "number") {
|
|
694
|
-
zodSchema = extendSchemaWithMessage(
|
|
695
|
-
zodSchema,
|
|
696
|
-
{ ...jsonSchema, maxLength: jsonSchema.max },
|
|
697
|
-
"maxLength",
|
|
698
|
-
(zs, maxLength, errorMsg) => zs.max(maxLength, errorMsg)
|
|
699
|
-
);
|
|
700
|
-
}
|
|
701
|
-
return zodSchema;
|
|
461
|
+
//#endregion
|
|
462
|
+
//#region src/parsers/parse-string.ts
|
|
463
|
+
const parseString = (jsonSchema) => {
|
|
464
|
+
let zodSchema = zod_v3.z.string();
|
|
465
|
+
zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "format", (zs, format, errorMsg) => {
|
|
466
|
+
switch (format) {
|
|
467
|
+
case "email": return zs.email(errorMsg);
|
|
468
|
+
case "ip": return zs.ip(errorMsg);
|
|
469
|
+
case "ipv4": return zs.ip({
|
|
470
|
+
version: "v4",
|
|
471
|
+
message: errorMsg
|
|
472
|
+
});
|
|
473
|
+
case "ipv6": return zs.ip({
|
|
474
|
+
version: "v6",
|
|
475
|
+
message: errorMsg
|
|
476
|
+
});
|
|
477
|
+
case "uri": return zs.url(errorMsg);
|
|
478
|
+
case "uuid": return zs.uuid(errorMsg);
|
|
479
|
+
case "date-time": return zs.datetime({
|
|
480
|
+
offset: true,
|
|
481
|
+
message: errorMsg
|
|
482
|
+
});
|
|
483
|
+
case "time": return zs.time(errorMsg);
|
|
484
|
+
case "date": return zs.date(errorMsg);
|
|
485
|
+
case "binary": return zs.base64(errorMsg);
|
|
486
|
+
case "duration": return zs.duration(errorMsg);
|
|
487
|
+
default: return zs;
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "contentEncoding", (zs, _, errorMsg) => zs.base64(errorMsg));
|
|
491
|
+
zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "pattern", (zs, pattern, errorMsg) => zs.regex(new RegExp(pattern), errorMsg));
|
|
492
|
+
zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "minLength", (zs, minLength, errorMsg) => zs.min(minLength, errorMsg));
|
|
493
|
+
zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "maxLength", (zs, maxLength, errorMsg) => zs.max(maxLength, errorMsg));
|
|
494
|
+
if (typeof jsonSchema.min === "number" && typeof jsonSchema.minLength !== "number") zodSchema = extendSchemaWithMessage(zodSchema, {
|
|
495
|
+
...jsonSchema,
|
|
496
|
+
minLength: jsonSchema.min
|
|
497
|
+
}, "minLength", (zs, minLength, errorMsg) => zs.min(minLength, errorMsg));
|
|
498
|
+
if (typeof jsonSchema.max === "number" && typeof jsonSchema.maxLength !== "number") zodSchema = extendSchemaWithMessage(zodSchema, {
|
|
499
|
+
...jsonSchema,
|
|
500
|
+
maxLength: jsonSchema.max
|
|
501
|
+
}, "maxLength", (zs, maxLength, errorMsg) => zs.max(maxLength, errorMsg));
|
|
502
|
+
return zodSchema;
|
|
702
503
|
};
|
|
703
504
|
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
${exampleText}` : exampleText;
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
if (description) {
|
|
726
|
-
zodSchema = zodSchema.describe(description);
|
|
727
|
-
}
|
|
728
|
-
return zodSchema;
|
|
505
|
+
//#endregion
|
|
506
|
+
//#region src/parsers/parse-schema.ts
|
|
507
|
+
const addDescribes = (jsonSchema, zodSchema) => {
|
|
508
|
+
let description = "";
|
|
509
|
+
if (jsonSchema.description) description = jsonSchema.description;
|
|
510
|
+
else if (jsonSchema.title) description = jsonSchema.title;
|
|
511
|
+
if (jsonSchema.example !== void 0) {
|
|
512
|
+
const exampleText = `Example: ${JSON.stringify(jsonSchema.example)}`;
|
|
513
|
+
description = description ? `${description}\n${exampleText}` : exampleText;
|
|
514
|
+
} else if (jsonSchema.examples !== void 0 && Array.isArray(jsonSchema.examples)) {
|
|
515
|
+
const examples = jsonSchema.examples;
|
|
516
|
+
if (examples && examples.length && examples.length > 0) {
|
|
517
|
+
const exampleText = examples.length === 1 ? `Example: ${JSON.stringify(examples[0])}` : `Examples:\n${examples.map((ex) => ` ${JSON.stringify(ex)}`).join("\n")}`;
|
|
518
|
+
description = description ? `${description}\n${exampleText}` : exampleText;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
if (description) zodSchema = zodSchema.describe(description);
|
|
522
|
+
return zodSchema;
|
|
729
523
|
};
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
}
|
|
739
|
-
zodSchema = zodSchema.default(jsonSchema.default);
|
|
740
|
-
}
|
|
741
|
-
return zodSchema;
|
|
524
|
+
const addDefaults = (jsonSchema, zodSchema, refs) => {
|
|
525
|
+
if (jsonSchema.default !== void 0) {
|
|
526
|
+
if (jsonSchema.default === null) {
|
|
527
|
+
if (refs?.path.some((segment) => segment === "anyOf" || segment === "oneOf") && jsonSchema.type && jsonSchema.type !== "null" && !jsonSchema.nullable) return zodSchema;
|
|
528
|
+
}
|
|
529
|
+
zodSchema = zodSchema.default(jsonSchema.default);
|
|
530
|
+
}
|
|
531
|
+
return zodSchema;
|
|
742
532
|
};
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
}
|
|
747
|
-
return zodSchema;
|
|
533
|
+
const addAnnotations = (jsonSchema, zodSchema) => {
|
|
534
|
+
if (jsonSchema.readOnly) zodSchema = zodSchema.readonly();
|
|
535
|
+
return zodSchema;
|
|
748
536
|
};
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
} else if (its.a.const(schema)) {
|
|
767
|
-
return parseConst(schema);
|
|
768
|
-
} else if (its.a.multipleType(schema)) {
|
|
769
|
-
return parseMultipleType(schema, refs);
|
|
770
|
-
} else if (its.a.primitive(schema, "string")) {
|
|
771
|
-
return parseString(schema);
|
|
772
|
-
} else if (its.a.primitive(schema, "number") || its.a.primitive(schema, "integer")) {
|
|
773
|
-
return parseNumber(schema);
|
|
774
|
-
} else if (its.a.primitive(schema, "boolean")) {
|
|
775
|
-
return parseBoolean(schema);
|
|
776
|
-
} else if (its.a.primitive(schema, "null")) {
|
|
777
|
-
return parseNull(schema);
|
|
778
|
-
} else if (its.a.conditional(schema)) {
|
|
779
|
-
return parseIfThenElse(schema, refs);
|
|
780
|
-
} else {
|
|
781
|
-
return parseDefault(schema);
|
|
782
|
-
}
|
|
537
|
+
const selectParser = (schema, refs) => {
|
|
538
|
+
if (its.a.nullable(schema)) return parseNullable(schema, refs);
|
|
539
|
+
else if (its.an.object(schema)) return parseObject(schema, refs);
|
|
540
|
+
else if (its.an.array(schema)) return parseArray(schema, refs);
|
|
541
|
+
else if (its.an.anyOf(schema)) return parseAnyOf(schema, refs);
|
|
542
|
+
else if (its.an.allOf(schema)) return parseAllOf(schema, refs);
|
|
543
|
+
else if (its.a.oneOf(schema)) return parseOneOf(schema, refs);
|
|
544
|
+
else if (its.a.not(schema)) return parseNot(schema, refs);
|
|
545
|
+
else if (its.an.enum(schema)) return parseEnum(schema);
|
|
546
|
+
else if (its.a.const(schema)) return parseConst(schema);
|
|
547
|
+
else if (its.a.multipleType(schema)) return parseMultipleType(schema, refs);
|
|
548
|
+
else if (its.a.primitive(schema, "string")) return parseString(schema);
|
|
549
|
+
else if (its.a.primitive(schema, "number") || its.a.primitive(schema, "integer")) return parseNumber(schema);
|
|
550
|
+
else if (its.a.primitive(schema, "boolean")) return parseBoolean(schema);
|
|
551
|
+
else if (its.a.primitive(schema, "null")) return parseNull(schema);
|
|
552
|
+
else if (its.a.conditional(schema)) return parseIfThenElse(schema, refs);
|
|
553
|
+
else return parseDefault(schema);
|
|
783
554
|
};
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
parsedZodSchema = addAnnotations(jsonSchema, parsedZodSchema);
|
|
814
|
-
}
|
|
815
|
-
seen.r = parsedZodSchema;
|
|
816
|
-
return parsedZodSchema;
|
|
555
|
+
const parseSchema = (jsonSchema, refs = {
|
|
556
|
+
seen: /* @__PURE__ */ new Map(),
|
|
557
|
+
path: []
|
|
558
|
+
}, blockMeta) => {
|
|
559
|
+
if (typeof jsonSchema !== "object") return jsonSchema ? zod_v3.any() : zod_v3.never();
|
|
560
|
+
if (refs.parserOverride) {
|
|
561
|
+
const custom = refs.parserOverride(jsonSchema, refs);
|
|
562
|
+
if (custom instanceof zod_v3.ZodType) return custom;
|
|
563
|
+
}
|
|
564
|
+
let seen = refs.seen.get(jsonSchema);
|
|
565
|
+
if (seen) {
|
|
566
|
+
if (seen.r !== void 0) return seen.r;
|
|
567
|
+
if (refs.depth === void 0 || seen.n >= refs.depth) return zod_v3.any();
|
|
568
|
+
seen.n += 1;
|
|
569
|
+
} else {
|
|
570
|
+
seen = {
|
|
571
|
+
r: void 0,
|
|
572
|
+
n: 0
|
|
573
|
+
};
|
|
574
|
+
refs.seen.set(jsonSchema, seen);
|
|
575
|
+
}
|
|
576
|
+
let parsedZodSchema = selectParser(jsonSchema, refs);
|
|
577
|
+
if (!blockMeta) {
|
|
578
|
+
if (!refs.withoutDescribes) parsedZodSchema = addDescribes(jsonSchema, parsedZodSchema);
|
|
579
|
+
if (!refs.withoutDefaults) parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema, refs);
|
|
580
|
+
parsedZodSchema = addAnnotations(jsonSchema, parsedZodSchema);
|
|
581
|
+
}
|
|
582
|
+
seen.r = parsedZodSchema;
|
|
583
|
+
return parsedZodSchema;
|
|
817
584
|
};
|
|
818
585
|
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
586
|
+
//#endregion
|
|
587
|
+
//#region src/parsers/parse-object-shape.ts
|
|
588
|
+
/**
|
|
589
|
+
* Parses a JSON Schema object and returns the raw shape (property keys mapped to Zod types).
|
|
590
|
+
* This is useful when you need ZodRawShape instead of a complete ZodObject.
|
|
591
|
+
*
|
|
592
|
+
* @param objectSchema - The JSON Schema object to parse
|
|
593
|
+
* @param refs - Parser references for handling nested schemas
|
|
594
|
+
* @returns A Zod raw shape object
|
|
595
|
+
*/
|
|
596
|
+
function parseObjectShape(objectSchema, refs) {
|
|
597
|
+
if (!objectSchema.properties) return {};
|
|
598
|
+
const propertyKeys = Object.keys(objectSchema.properties);
|
|
599
|
+
if (propertyKeys.length === 0) return {};
|
|
600
|
+
const properties = {};
|
|
601
|
+
for (const key of propertyKeys) {
|
|
602
|
+
const propJsonSchema = objectSchema.properties[key];
|
|
603
|
+
const propZodSchema = parseSchema(propJsonSchema, {
|
|
604
|
+
...refs,
|
|
605
|
+
path: [
|
|
606
|
+
...refs.path,
|
|
607
|
+
"properties",
|
|
608
|
+
key
|
|
609
|
+
]
|
|
610
|
+
});
|
|
611
|
+
const required = Array.isArray(objectSchema.required) ? objectSchema.required.includes(key) : false;
|
|
612
|
+
if (!required && propJsonSchema && typeof propJsonSchema === "object" && "default" in propJsonSchema) if (propJsonSchema.default === null) {
|
|
613
|
+
const hasAnyOfWithNull = propJsonSchema.anyOf && Array.isArray(propJsonSchema.anyOf) && propJsonSchema.anyOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
|
|
614
|
+
const hasOneOfWithNull = propJsonSchema.oneOf && Array.isArray(propJsonSchema.oneOf) && propJsonSchema.oneOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
|
|
615
|
+
const isNullable = "nullable" in propJsonSchema && propJsonSchema.nullable === true;
|
|
616
|
+
if (hasAnyOfWithNull || hasOneOfWithNull || isNullable) properties[key] = propZodSchema.optional().default(null);
|
|
617
|
+
else properties[key] = propZodSchema.nullable().optional().default(null);
|
|
618
|
+
} else properties[key] = propZodSchema.optional().default(propJsonSchema.default);
|
|
619
|
+
else properties[key] = required ? propZodSchema : propZodSchema.optional();
|
|
620
|
+
}
|
|
621
|
+
return properties;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
//#endregion
|
|
625
|
+
//#region src/json-schema-to-zod.ts
|
|
626
|
+
const jsonSchemaToZod = (schema, options = {}) => {
|
|
627
|
+
return parseSchema(schema, {
|
|
628
|
+
path: [],
|
|
629
|
+
seen: /* @__PURE__ */ new Map(),
|
|
630
|
+
...options
|
|
631
|
+
});
|
|
826
632
|
};
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
633
|
+
/**
|
|
634
|
+
* Converts a JSON Schema object to a Zod raw shape.
|
|
635
|
+
* This is useful when you need the shape object (e.g., `{ name: z.string(), age: z.number() }`)
|
|
636
|
+
* rather than a complete ZodObject schema.
|
|
637
|
+
*
|
|
638
|
+
* This is particularly useful for APIs that expect ZodRawShape instead of ZodType,
|
|
639
|
+
* such as the Claude Agent SDK's `tool()` function.
|
|
640
|
+
*
|
|
641
|
+
* @param schema - The JSON Schema to convert (must be an object schema)
|
|
642
|
+
* @param options - Optional configuration for the conversion
|
|
643
|
+
* @returns A Zod raw shape object with property keys mapped to Zod types
|
|
644
|
+
*/
|
|
645
|
+
const jsonSchemaToZodShape = (schema, options = {}) => {
|
|
646
|
+
if (typeof schema !== "object" || schema === null) return {};
|
|
647
|
+
return parseObjectShape(schema, {
|
|
648
|
+
path: [],
|
|
649
|
+
seen: /* @__PURE__ */ new Map(),
|
|
650
|
+
...options
|
|
651
|
+
});
|
|
652
|
+
};
|
|
653
|
+
|
|
654
|
+
//#endregion
|
|
655
|
+
exports.jsonSchemaToZod = jsonSchemaToZod;
|
|
656
|
+
exports.jsonSchemaToZodShape = jsonSchemaToZodShape;
|