@composio/json-schema-to-zod 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -0
- package/dist/index.cjs +695 -0
- package/dist/index.d.cts +72 -0
- package/dist/index.d.ts +72 -0
- package/dist/index.js +658 -0
- package/package.json +37 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,695 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
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
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
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"), 1);
|
|
39
|
+
|
|
40
|
+
// src/parsers/parse-all-of.ts
|
|
41
|
+
var import_zod = require("zod");
|
|
42
|
+
|
|
43
|
+
// src/utils/half.ts
|
|
44
|
+
var half = (arr) => {
|
|
45
|
+
return [arr.slice(0, arr.length / 2), arr.slice(arr.length / 2)];
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// src/parsers/parse-all-of.ts
|
|
49
|
+
var originalIndex = Symbol("Original index");
|
|
50
|
+
var ensureOriginalIndex = (arr) => {
|
|
51
|
+
const newArr = [];
|
|
52
|
+
for (let i = 0; i < arr.length; i++) {
|
|
53
|
+
const item = arr[i];
|
|
54
|
+
if (typeof item === "boolean") {
|
|
55
|
+
newArr.push(item ? { [originalIndex]: i } : { [originalIndex]: i, not: {} });
|
|
56
|
+
} else if (originalIndex in item) {
|
|
57
|
+
return arr;
|
|
58
|
+
} else {
|
|
59
|
+
newArr.push({ ...item, [originalIndex]: i });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return newArr;
|
|
63
|
+
};
|
|
64
|
+
function parseAllOf(jsonSchema, refs) {
|
|
65
|
+
if (jsonSchema.allOf.length === 0) {
|
|
66
|
+
return import_zod.z.never();
|
|
67
|
+
}
|
|
68
|
+
if (jsonSchema.allOf.length === 1) {
|
|
69
|
+
const item = jsonSchema.allOf[0];
|
|
70
|
+
return parseSchema(item, {
|
|
71
|
+
...refs,
|
|
72
|
+
path: [...refs.path, "allOf", item[originalIndex]]
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
const [left, right] = half(ensureOriginalIndex(jsonSchema.allOf));
|
|
76
|
+
return import_zod.z.intersection(parseAllOf({ allOf: left }, refs), parseAllOf({ allOf: right }, refs));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/parsers/parse-any-of.ts
|
|
80
|
+
var import_zod2 = require("zod");
|
|
81
|
+
var parseAnyOf = (jsonSchema, refs) => {
|
|
82
|
+
return jsonSchema.anyOf.length ? jsonSchema.anyOf.length === 1 ? parseSchema(jsonSchema.anyOf[0], {
|
|
83
|
+
...refs,
|
|
84
|
+
path: [...refs.path, "anyOf", 0]
|
|
85
|
+
}) : import_zod2.z.union(
|
|
86
|
+
jsonSchema.anyOf.map(
|
|
87
|
+
(schema, i) => parseSchema(schema, { ...refs, path: [...refs.path, "anyOf", i] })
|
|
88
|
+
)
|
|
89
|
+
) : import_zod2.z.any();
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// src/parsers/parse-array.ts
|
|
93
|
+
var import_zod3 = require("zod");
|
|
94
|
+
|
|
95
|
+
// src/utils/extend-schema.ts
|
|
96
|
+
function extendSchemaWithMessage(zodSchema, jsonSchema, key, extend) {
|
|
97
|
+
const value = jsonSchema[key];
|
|
98
|
+
if (value !== void 0) {
|
|
99
|
+
const errorMessage = jsonSchema.errorMessage?.[key];
|
|
100
|
+
return extend(zodSchema, value, errorMessage);
|
|
101
|
+
}
|
|
102
|
+
return zodSchema;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/parsers/parse-array.ts
|
|
106
|
+
var parseArray = (jsonSchema, refs) => {
|
|
107
|
+
if (Array.isArray(jsonSchema.items)) {
|
|
108
|
+
return import_zod3.z.tuple(
|
|
109
|
+
jsonSchema.items.map(
|
|
110
|
+
(v, i) => parseSchema(v, { ...refs, path: [...refs.path, "items", i] })
|
|
111
|
+
)
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
let zodSchema = !jsonSchema.items ? import_zod3.z.array(import_zod3.z.any()) : import_zod3.z.array(parseSchema(jsonSchema.items, { ...refs, path: [...refs.path, "items"] }));
|
|
115
|
+
zodSchema = extendSchemaWithMessage(
|
|
116
|
+
zodSchema,
|
|
117
|
+
jsonSchema,
|
|
118
|
+
"minItems",
|
|
119
|
+
(zs, minItems, errorMessage) => zs.min(minItems, errorMessage)
|
|
120
|
+
);
|
|
121
|
+
zodSchema = extendSchemaWithMessage(
|
|
122
|
+
zodSchema,
|
|
123
|
+
jsonSchema,
|
|
124
|
+
"maxItems",
|
|
125
|
+
(zs, maxItems, errorMessage) => zs.max(maxItems, errorMessage)
|
|
126
|
+
);
|
|
127
|
+
return zodSchema;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
// src/parsers/parse-boolean.ts
|
|
131
|
+
var import_zod4 = require("zod");
|
|
132
|
+
var parseBoolean = (_jsonSchema) => {
|
|
133
|
+
return import_zod4.z.boolean();
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// src/parsers/parse-const.ts
|
|
137
|
+
var import_zod5 = require("zod");
|
|
138
|
+
var parseConst = (jsonSchema) => {
|
|
139
|
+
return import_zod5.z.literal(jsonSchema.const);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// src/parsers/parse-default.ts
|
|
143
|
+
var import_zod6 = require("zod");
|
|
144
|
+
var parseDefault = (_jsonSchema) => {
|
|
145
|
+
return import_zod6.z.any();
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// src/parsers/parse-enum.ts
|
|
149
|
+
var import_zod7 = require("zod");
|
|
150
|
+
var parseEnum = (jsonSchema) => {
|
|
151
|
+
if (jsonSchema.enum.length === 0) {
|
|
152
|
+
return import_zod7.z.never();
|
|
153
|
+
}
|
|
154
|
+
if (jsonSchema.enum.length === 1) {
|
|
155
|
+
return import_zod7.z.literal(jsonSchema.enum[0]);
|
|
156
|
+
}
|
|
157
|
+
if (jsonSchema.enum.every((x) => typeof x === "string")) {
|
|
158
|
+
return import_zod7.z.enum(jsonSchema.enum);
|
|
159
|
+
}
|
|
160
|
+
return import_zod7.z.union(
|
|
161
|
+
jsonSchema.enum.map((x) => import_zod7.z.literal(x))
|
|
162
|
+
);
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// src/parsers/parse-if-then-else.ts
|
|
166
|
+
var import_zod8 = require("zod");
|
|
167
|
+
var parseIfThenElse = (jsonSchema, refs) => {
|
|
168
|
+
const $if = parseSchema(jsonSchema.if, { ...refs, path: [...refs.path, "if"] });
|
|
169
|
+
const $then = parseSchema(jsonSchema.then, {
|
|
170
|
+
...refs,
|
|
171
|
+
path: [...refs.path, "then"]
|
|
172
|
+
});
|
|
173
|
+
const $else = parseSchema(jsonSchema.else, {
|
|
174
|
+
...refs,
|
|
175
|
+
path: [...refs.path, "else"]
|
|
176
|
+
});
|
|
177
|
+
return import_zod8.z.union([$then, $else]).superRefine((value, ctx) => {
|
|
178
|
+
const result = $if.safeParse(value).success ? $then.safeParse(value) : $else.safeParse(value);
|
|
179
|
+
if (!result.success) {
|
|
180
|
+
result.error.errors.forEach((error) => ctx.addIssue(error));
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// src/parsers/parse-multiple-type.ts
|
|
186
|
+
var import_zod9 = require("zod");
|
|
187
|
+
var parseMultipleType = (jsonSchema, refs) => {
|
|
188
|
+
return import_zod9.z.union(
|
|
189
|
+
jsonSchema.type.map((type) => parseSchema({ ...jsonSchema, type }, refs))
|
|
190
|
+
);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
// src/parsers/parse-not.ts
|
|
194
|
+
var import_zod10 = require("zod");
|
|
195
|
+
var parseNot = (jsonSchema, refs) => {
|
|
196
|
+
return import_zod10.z.any().refine(
|
|
197
|
+
(value) => !parseSchema(jsonSchema.not, {
|
|
198
|
+
...refs,
|
|
199
|
+
path: [...refs.path, "not"]
|
|
200
|
+
}).safeParse(value).success,
|
|
201
|
+
"Invalid input: Should NOT be valid against schema"
|
|
202
|
+
);
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
// src/parsers/parse-null.ts
|
|
206
|
+
var import_zod11 = require("zod");
|
|
207
|
+
var parseNull = (_jsonSchema) => {
|
|
208
|
+
return import_zod11.z.null();
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// src/utils/omit.ts
|
|
212
|
+
var omit = (obj, ...keys) => Object.keys(obj).reduce((acc, key) => {
|
|
213
|
+
if (!keys.includes(key)) {
|
|
214
|
+
acc[key] = obj[key];
|
|
215
|
+
}
|
|
216
|
+
return acc;
|
|
217
|
+
}, {});
|
|
218
|
+
|
|
219
|
+
// src/parsers/parse-nullable.ts
|
|
220
|
+
var parseNullable = (jsonSchema, refs) => {
|
|
221
|
+
return parseSchema(omit(jsonSchema, "nullable"), refs, true).nullable();
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
// src/parsers/parse-number.ts
|
|
225
|
+
var import_zod12 = require("zod");
|
|
226
|
+
var parseNumber = (jsonSchema) => {
|
|
227
|
+
let zodSchema = import_zod12.z.number();
|
|
228
|
+
let isInteger = false;
|
|
229
|
+
if (jsonSchema.type === "integer") {
|
|
230
|
+
isInteger = true;
|
|
231
|
+
zodSchema = extendSchemaWithMessage(
|
|
232
|
+
zodSchema,
|
|
233
|
+
jsonSchema,
|
|
234
|
+
"type",
|
|
235
|
+
(zs, _, errorMsg) => zs.int(errorMsg)
|
|
236
|
+
);
|
|
237
|
+
} else if (jsonSchema.format === "int64") {
|
|
238
|
+
isInteger = true;
|
|
239
|
+
zodSchema = extendSchemaWithMessage(
|
|
240
|
+
zodSchema,
|
|
241
|
+
jsonSchema,
|
|
242
|
+
"format",
|
|
243
|
+
(zs, _, errorMsg) => zs.int(errorMsg)
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
zodSchema = extendSchemaWithMessage(
|
|
247
|
+
zodSchema,
|
|
248
|
+
jsonSchema,
|
|
249
|
+
"multipleOf",
|
|
250
|
+
(zs, multipleOf, errorMsg) => {
|
|
251
|
+
if (multipleOf === 1) {
|
|
252
|
+
if (isInteger) return zs;
|
|
253
|
+
return zs.int(errorMsg);
|
|
254
|
+
}
|
|
255
|
+
return zs.multipleOf(multipleOf, errorMsg);
|
|
256
|
+
}
|
|
257
|
+
);
|
|
258
|
+
if (typeof jsonSchema.minimum === "number") {
|
|
259
|
+
if (jsonSchema.exclusiveMinimum === true) {
|
|
260
|
+
zodSchema = extendSchemaWithMessage(
|
|
261
|
+
zodSchema,
|
|
262
|
+
jsonSchema,
|
|
263
|
+
"minimum",
|
|
264
|
+
(zs, minimum, errorMsg) => zs.gt(minimum, errorMsg)
|
|
265
|
+
);
|
|
266
|
+
} else {
|
|
267
|
+
zodSchema = extendSchemaWithMessage(
|
|
268
|
+
zodSchema,
|
|
269
|
+
jsonSchema,
|
|
270
|
+
"minimum",
|
|
271
|
+
(zs, minimum, errorMsg) => zs.gte(minimum, errorMsg)
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
} else if (typeof jsonSchema.exclusiveMinimum === "number") {
|
|
275
|
+
zodSchema = extendSchemaWithMessage(
|
|
276
|
+
zodSchema,
|
|
277
|
+
jsonSchema,
|
|
278
|
+
"exclusiveMinimum",
|
|
279
|
+
(zs, exclusiveMinimum, errorMsg) => zs.gt(exclusiveMinimum, errorMsg)
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
if (typeof jsonSchema.maximum === "number") {
|
|
283
|
+
if (jsonSchema.exclusiveMaximum === true) {
|
|
284
|
+
zodSchema = extendSchemaWithMessage(
|
|
285
|
+
zodSchema,
|
|
286
|
+
jsonSchema,
|
|
287
|
+
"maximum",
|
|
288
|
+
(zs, maximum, errorMsg) => zs.lt(maximum, errorMsg)
|
|
289
|
+
);
|
|
290
|
+
} else {
|
|
291
|
+
zodSchema = extendSchemaWithMessage(
|
|
292
|
+
zodSchema,
|
|
293
|
+
jsonSchema,
|
|
294
|
+
"maximum",
|
|
295
|
+
(zs, maximum, errorMsg) => zs.lte(maximum, errorMsg)
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
} else if (typeof jsonSchema.exclusiveMaximum === "number") {
|
|
299
|
+
zodSchema = extendSchemaWithMessage(
|
|
300
|
+
zodSchema,
|
|
301
|
+
jsonSchema,
|
|
302
|
+
"exclusiveMaximum",
|
|
303
|
+
(zs, exclusiveMaximum, errorMsg) => zs.lt(exclusiveMaximum, errorMsg)
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
return zodSchema;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
// src/parsers/parse-object.ts
|
|
310
|
+
var z14 = __toESM(require("zod"), 1);
|
|
311
|
+
|
|
312
|
+
// src/parsers/parse-one-of.ts
|
|
313
|
+
var import_zod13 = require("zod");
|
|
314
|
+
var parseOneOf = (jsonSchema, refs) => {
|
|
315
|
+
if (!jsonSchema.oneOf.length) {
|
|
316
|
+
return import_zod13.z.any();
|
|
317
|
+
}
|
|
318
|
+
if (jsonSchema.oneOf.length === 1) {
|
|
319
|
+
return parseSchema(jsonSchema.oneOf[0], {
|
|
320
|
+
...refs,
|
|
321
|
+
path: [...refs.path, "oneOf", 0]
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
return import_zod13.z.any().superRefine((x, ctx) => {
|
|
325
|
+
const schemas = jsonSchema.oneOf.map(
|
|
326
|
+
(schema, i) => parseSchema(schema, {
|
|
327
|
+
...refs,
|
|
328
|
+
path: [...refs.path, "oneOf", i]
|
|
329
|
+
})
|
|
330
|
+
);
|
|
331
|
+
const unionErrors = schemas.reduce(
|
|
332
|
+
(errors, schema) => ((result) => result.error ? [...errors, result.error] : errors)(schema.safeParse(x)),
|
|
333
|
+
[]
|
|
334
|
+
);
|
|
335
|
+
if (schemas.length - unionErrors.length !== 1) {
|
|
336
|
+
ctx.addIssue({
|
|
337
|
+
path: ctx.path,
|
|
338
|
+
code: "invalid_union",
|
|
339
|
+
unionErrors,
|
|
340
|
+
message: "Invalid input: Should pass single schema"
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
// src/utils/its.ts
|
|
347
|
+
var its = {
|
|
348
|
+
an: {
|
|
349
|
+
object: (x) => x.type === "object",
|
|
350
|
+
array: (x) => x.type === "array",
|
|
351
|
+
anyOf: (x) => x.anyOf !== void 0,
|
|
352
|
+
allOf: (x) => x.allOf !== void 0,
|
|
353
|
+
enum: (x) => x.enum !== void 0
|
|
354
|
+
},
|
|
355
|
+
a: {
|
|
356
|
+
nullable: (x) => x.nullable === true,
|
|
357
|
+
multipleType: (x) => Array.isArray(x.type),
|
|
358
|
+
not: (x) => x.not !== void 0,
|
|
359
|
+
const: (x) => x.const !== void 0,
|
|
360
|
+
primitive: (x, p) => x.type === p,
|
|
361
|
+
conditional: (x) => Boolean("if" in x && x.if && "then" in x && "else" in x && x.then && x.else),
|
|
362
|
+
oneOf: (x) => x.oneOf !== void 0
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
// src/parsers/parse-object.ts
|
|
367
|
+
function parseObjectProperties(objectSchema, refs) {
|
|
368
|
+
if (!objectSchema.properties) {
|
|
369
|
+
return void 0;
|
|
370
|
+
}
|
|
371
|
+
const propertyKeys = Object.keys(objectSchema.properties);
|
|
372
|
+
if (propertyKeys.length === 0) {
|
|
373
|
+
return z14.object({});
|
|
374
|
+
}
|
|
375
|
+
const properties = {};
|
|
376
|
+
for (const key of propertyKeys) {
|
|
377
|
+
const propJsonSchema = objectSchema.properties[key];
|
|
378
|
+
const propZodSchema = parseSchema(propJsonSchema, {
|
|
379
|
+
...refs,
|
|
380
|
+
path: [...refs.path, "properties", key]
|
|
381
|
+
});
|
|
382
|
+
const hasDefault = typeof propJsonSchema === "object" && propJsonSchema.default !== void 0;
|
|
383
|
+
const required = Array.isArray(objectSchema.required) ? objectSchema.required.includes(key) : typeof propJsonSchema === "object" && propJsonSchema.required === true;
|
|
384
|
+
const isOptional = !hasDefault && !required && !(typeof propJsonSchema === "object" && propJsonSchema.type === "object" && propJsonSchema.properties);
|
|
385
|
+
properties[key] = isOptional ? propZodSchema.optional() : propZodSchema;
|
|
386
|
+
}
|
|
387
|
+
return z14.object(properties);
|
|
388
|
+
}
|
|
389
|
+
function parseObject(objectSchema, refs) {
|
|
390
|
+
const hasPatternProperties = Object.keys(objectSchema.patternProperties ?? {}).length > 0;
|
|
391
|
+
const propertiesSchema = parseObjectProperties(objectSchema, refs);
|
|
392
|
+
let zodSchema = propertiesSchema;
|
|
393
|
+
const additionalProperties = objectSchema.additionalProperties !== void 0 ? parseSchema(objectSchema.additionalProperties, {
|
|
394
|
+
...refs,
|
|
395
|
+
path: [...refs.path, "additionalProperties"]
|
|
396
|
+
}) : void 0;
|
|
397
|
+
if (objectSchema.patternProperties) {
|
|
398
|
+
const parsedPatternProperties = Object.fromEntries(
|
|
399
|
+
Object.entries(objectSchema.patternProperties).map(([key, value]) => {
|
|
400
|
+
return [
|
|
401
|
+
key,
|
|
402
|
+
parseSchema(value, {
|
|
403
|
+
...refs,
|
|
404
|
+
path: [...refs.path, "patternProperties", key]
|
|
405
|
+
})
|
|
406
|
+
];
|
|
407
|
+
})
|
|
408
|
+
);
|
|
409
|
+
const patternPropertyValues = Object.values(parsedPatternProperties);
|
|
410
|
+
if (propertiesSchema) {
|
|
411
|
+
if (additionalProperties) {
|
|
412
|
+
zodSchema = propertiesSchema.catchall(
|
|
413
|
+
z14.union([...patternPropertyValues, additionalProperties])
|
|
414
|
+
);
|
|
415
|
+
} else if (Object.keys(parsedPatternProperties).length > 1) {
|
|
416
|
+
zodSchema = propertiesSchema.catchall(
|
|
417
|
+
z14.union(patternPropertyValues)
|
|
418
|
+
);
|
|
419
|
+
} else {
|
|
420
|
+
zodSchema = propertiesSchema.catchall(patternPropertyValues[0]);
|
|
421
|
+
}
|
|
422
|
+
} else {
|
|
423
|
+
if (additionalProperties) {
|
|
424
|
+
zodSchema = z14.record(
|
|
425
|
+
z14.union([...patternPropertyValues, additionalProperties])
|
|
426
|
+
);
|
|
427
|
+
} else if (patternPropertyValues.length > 1) {
|
|
428
|
+
zodSchema = z14.record(z14.union(patternPropertyValues));
|
|
429
|
+
} else {
|
|
430
|
+
zodSchema = z14.record(patternPropertyValues[0]);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
const objectPropertyKeys = new Set(Object.keys(objectSchema.properties ?? {}));
|
|
434
|
+
zodSchema = zodSchema.superRefine((value, ctx) => {
|
|
435
|
+
for (const key in value) {
|
|
436
|
+
let wasMatched = objectPropertyKeys.has(key);
|
|
437
|
+
for (const patternPropertyKey in objectSchema.patternProperties) {
|
|
438
|
+
const regex = new RegExp(patternPropertyKey);
|
|
439
|
+
if (key.match(regex)) {
|
|
440
|
+
wasMatched = true;
|
|
441
|
+
const result = parsedPatternProperties[patternPropertyKey].safeParse(value[key]);
|
|
442
|
+
if (!result.success) {
|
|
443
|
+
ctx.addIssue({
|
|
444
|
+
path: [...ctx.path, key],
|
|
445
|
+
code: "custom",
|
|
446
|
+
message: `Invalid input: Key matching regex /${key}/ must match schema`,
|
|
447
|
+
params: {
|
|
448
|
+
issues: result.error.issues
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
if (!wasMatched && additionalProperties) {
|
|
455
|
+
const result = additionalProperties.safeParse(value[key]);
|
|
456
|
+
if (!result.success) {
|
|
457
|
+
ctx.addIssue({
|
|
458
|
+
path: [...ctx.path, key],
|
|
459
|
+
code: "custom",
|
|
460
|
+
message: "Invalid input: must match catchall schema",
|
|
461
|
+
params: {
|
|
462
|
+
issues: result.error.issues
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
let output;
|
|
471
|
+
if (propertiesSchema) {
|
|
472
|
+
if (hasPatternProperties) {
|
|
473
|
+
output = zodSchema;
|
|
474
|
+
} else if (additionalProperties) {
|
|
475
|
+
if (additionalProperties instanceof z14.ZodNever) {
|
|
476
|
+
output = propertiesSchema.strict();
|
|
477
|
+
} else {
|
|
478
|
+
output = propertiesSchema.catchall(additionalProperties);
|
|
479
|
+
}
|
|
480
|
+
} else {
|
|
481
|
+
output = zodSchema;
|
|
482
|
+
}
|
|
483
|
+
} else {
|
|
484
|
+
if (hasPatternProperties) {
|
|
485
|
+
output = zodSchema;
|
|
486
|
+
} else if (additionalProperties) {
|
|
487
|
+
output = z14.record(additionalProperties);
|
|
488
|
+
} else {
|
|
489
|
+
output = z14.record(z14.any());
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
if (its.an.anyOf(objectSchema)) {
|
|
493
|
+
output = output.and(
|
|
494
|
+
parseAnyOf(
|
|
495
|
+
{
|
|
496
|
+
...objectSchema,
|
|
497
|
+
anyOf: objectSchema.anyOf.map(
|
|
498
|
+
(x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? { ...x, type: "object" } : x
|
|
499
|
+
)
|
|
500
|
+
},
|
|
501
|
+
refs
|
|
502
|
+
)
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
if (its.a.oneOf(objectSchema)) {
|
|
506
|
+
output = output.and(
|
|
507
|
+
parseOneOf(
|
|
508
|
+
{
|
|
509
|
+
...objectSchema,
|
|
510
|
+
oneOf: objectSchema.oneOf.map(
|
|
511
|
+
(x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? { ...x, type: "object" } : x
|
|
512
|
+
)
|
|
513
|
+
},
|
|
514
|
+
refs
|
|
515
|
+
)
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
if (its.an.allOf(objectSchema)) {
|
|
519
|
+
output = output.and(
|
|
520
|
+
parseAllOf(
|
|
521
|
+
{
|
|
522
|
+
...objectSchema,
|
|
523
|
+
allOf: objectSchema.allOf.map(
|
|
524
|
+
(x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? { ...x, type: "object" } : x
|
|
525
|
+
)
|
|
526
|
+
},
|
|
527
|
+
refs
|
|
528
|
+
)
|
|
529
|
+
);
|
|
530
|
+
}
|
|
531
|
+
return output;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// src/parsers/parse-string.ts
|
|
535
|
+
var import_zod14 = require("zod");
|
|
536
|
+
var parseString = (jsonSchema) => {
|
|
537
|
+
let zodSchema = import_zod14.z.string();
|
|
538
|
+
zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "format", (zs, format, errorMsg) => {
|
|
539
|
+
switch (format) {
|
|
540
|
+
case "email":
|
|
541
|
+
return zs.email(errorMsg);
|
|
542
|
+
case "ip":
|
|
543
|
+
return zs.ip(errorMsg);
|
|
544
|
+
case "ipv4":
|
|
545
|
+
return zs.ip({ version: "v4", message: errorMsg });
|
|
546
|
+
case "ipv6":
|
|
547
|
+
return zs.ip({ version: "v6", message: errorMsg });
|
|
548
|
+
case "uri":
|
|
549
|
+
return zs.url(errorMsg);
|
|
550
|
+
case "uuid":
|
|
551
|
+
return zs.uuid(errorMsg);
|
|
552
|
+
case "date-time":
|
|
553
|
+
return zs.datetime({ offset: true, message: errorMsg });
|
|
554
|
+
case "time":
|
|
555
|
+
return zs.time(errorMsg);
|
|
556
|
+
case "date":
|
|
557
|
+
return zs.date(errorMsg);
|
|
558
|
+
case "binary":
|
|
559
|
+
return zs.base64(errorMsg);
|
|
560
|
+
case "duration":
|
|
561
|
+
return zs.duration(errorMsg);
|
|
562
|
+
default:
|
|
563
|
+
return zs;
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
zodSchema = extendSchemaWithMessage(
|
|
567
|
+
zodSchema,
|
|
568
|
+
jsonSchema,
|
|
569
|
+
"contentEncoding",
|
|
570
|
+
(zs, _, errorMsg) => zs.base64(errorMsg)
|
|
571
|
+
);
|
|
572
|
+
zodSchema = extendSchemaWithMessage(
|
|
573
|
+
zodSchema,
|
|
574
|
+
jsonSchema,
|
|
575
|
+
"pattern",
|
|
576
|
+
(zs, pattern, errorMsg) => zs.regex(new RegExp(pattern), errorMsg)
|
|
577
|
+
);
|
|
578
|
+
zodSchema = extendSchemaWithMessage(
|
|
579
|
+
zodSchema,
|
|
580
|
+
jsonSchema,
|
|
581
|
+
"minLength",
|
|
582
|
+
(zs, minLength, errorMsg) => zs.min(minLength, errorMsg)
|
|
583
|
+
);
|
|
584
|
+
zodSchema = extendSchemaWithMessage(
|
|
585
|
+
zodSchema,
|
|
586
|
+
jsonSchema,
|
|
587
|
+
"maxLength",
|
|
588
|
+
(zs, maxLength, errorMsg) => zs.max(maxLength, errorMsg)
|
|
589
|
+
);
|
|
590
|
+
return zodSchema;
|
|
591
|
+
};
|
|
592
|
+
|
|
593
|
+
// src/parsers/parse-schema.ts
|
|
594
|
+
var addDescribes = (jsonSchema, zodSchema) => {
|
|
595
|
+
if (jsonSchema.description) {
|
|
596
|
+
zodSchema = zodSchema.describe(jsonSchema.description);
|
|
597
|
+
} else if (jsonSchema.title) {
|
|
598
|
+
zodSchema = zodSchema.describe(jsonSchema.title);
|
|
599
|
+
}
|
|
600
|
+
return zodSchema;
|
|
601
|
+
};
|
|
602
|
+
var addDefaults = (jsonSchema, zodSchema) => {
|
|
603
|
+
if (jsonSchema.default !== void 0) {
|
|
604
|
+
zodSchema = zodSchema.default(jsonSchema.default);
|
|
605
|
+
}
|
|
606
|
+
return zodSchema;
|
|
607
|
+
};
|
|
608
|
+
var addAnnotations = (jsonSchema, zodSchema) => {
|
|
609
|
+
if (jsonSchema.readOnly) {
|
|
610
|
+
zodSchema = zodSchema.readonly();
|
|
611
|
+
}
|
|
612
|
+
return zodSchema;
|
|
613
|
+
};
|
|
614
|
+
var selectParser = (schema, refs) => {
|
|
615
|
+
if (its.a.nullable(schema)) {
|
|
616
|
+
return parseNullable(schema, refs);
|
|
617
|
+
} else if (its.an.object(schema)) {
|
|
618
|
+
return parseObject(schema, refs);
|
|
619
|
+
} else if (its.an.array(schema)) {
|
|
620
|
+
return parseArray(schema, refs);
|
|
621
|
+
} else if (its.an.anyOf(schema)) {
|
|
622
|
+
return parseAnyOf(schema, refs);
|
|
623
|
+
} else if (its.an.allOf(schema)) {
|
|
624
|
+
return parseAllOf(schema, refs);
|
|
625
|
+
} else if (its.a.oneOf(schema)) {
|
|
626
|
+
return parseOneOf(schema, refs);
|
|
627
|
+
} else if (its.a.not(schema)) {
|
|
628
|
+
return parseNot(schema, refs);
|
|
629
|
+
} else if (its.an.enum(schema)) {
|
|
630
|
+
return parseEnum(schema);
|
|
631
|
+
} else if (its.a.const(schema)) {
|
|
632
|
+
return parseConst(schema);
|
|
633
|
+
} else if (its.a.multipleType(schema)) {
|
|
634
|
+
return parseMultipleType(schema, refs);
|
|
635
|
+
} else if (its.a.primitive(schema, "string")) {
|
|
636
|
+
return parseString(schema);
|
|
637
|
+
} else if (its.a.primitive(schema, "number") || its.a.primitive(schema, "integer")) {
|
|
638
|
+
return parseNumber(schema);
|
|
639
|
+
} else if (its.a.primitive(schema, "boolean")) {
|
|
640
|
+
return parseBoolean(schema);
|
|
641
|
+
} else if (its.a.primitive(schema, "null")) {
|
|
642
|
+
return parseNull(schema);
|
|
643
|
+
} else if (its.a.conditional(schema)) {
|
|
644
|
+
return parseIfThenElse(schema, refs);
|
|
645
|
+
} else {
|
|
646
|
+
return parseDefault(schema);
|
|
647
|
+
}
|
|
648
|
+
};
|
|
649
|
+
var parseSchema = (jsonSchema, refs = { seen: /* @__PURE__ */ new Map(), path: [] }, blockMeta) => {
|
|
650
|
+
if (typeof jsonSchema !== "object") return jsonSchema ? z16.any() : z16.never();
|
|
651
|
+
if (refs.parserOverride) {
|
|
652
|
+
const custom = refs.parserOverride(jsonSchema, refs);
|
|
653
|
+
if (custom instanceof z16.ZodType) {
|
|
654
|
+
return custom;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
let seen = refs.seen.get(jsonSchema);
|
|
658
|
+
if (seen) {
|
|
659
|
+
if (seen.r !== void 0) {
|
|
660
|
+
return seen.r;
|
|
661
|
+
}
|
|
662
|
+
if (refs.depth === void 0 || seen.n >= refs.depth) {
|
|
663
|
+
return z16.any();
|
|
664
|
+
}
|
|
665
|
+
seen.n += 1;
|
|
666
|
+
} else {
|
|
667
|
+
seen = { r: void 0, n: 0 };
|
|
668
|
+
refs.seen.set(jsonSchema, seen);
|
|
669
|
+
}
|
|
670
|
+
let parsedZodSchema = selectParser(jsonSchema, refs);
|
|
671
|
+
if (!blockMeta) {
|
|
672
|
+
if (!refs.withoutDescribes) {
|
|
673
|
+
parsedZodSchema = addDescribes(jsonSchema, parsedZodSchema);
|
|
674
|
+
}
|
|
675
|
+
if (!refs.withoutDefaults) {
|
|
676
|
+
parsedZodSchema = addDefaults(jsonSchema, parsedZodSchema);
|
|
677
|
+
}
|
|
678
|
+
parsedZodSchema = addAnnotations(jsonSchema, parsedZodSchema);
|
|
679
|
+
}
|
|
680
|
+
seen.r = parsedZodSchema;
|
|
681
|
+
return parsedZodSchema;
|
|
682
|
+
};
|
|
683
|
+
|
|
684
|
+
// src/json-schema-to-zod.ts
|
|
685
|
+
var jsonSchemaToZod = (schema, options = {}) => {
|
|
686
|
+
return parseSchema(schema, {
|
|
687
|
+
path: [],
|
|
688
|
+
seen: /* @__PURE__ */ new Map(),
|
|
689
|
+
...options
|
|
690
|
+
});
|
|
691
|
+
};
|
|
692
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
693
|
+
0 && (module.exports = {
|
|
694
|
+
jsonSchemaToZod
|
|
695
|
+
});
|