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