@composio/json-schema-to-zod 0.3.1 → 0.3.3-beta.0
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.d.mts +23 -3
- package/dist/index.mjs +448 -103
- package/package.json +10 -4
package/dist/index.d.mts
CHANGED
|
@@ -180,10 +180,12 @@ type Refs = JsonSchemaToZodOptions & {
|
|
|
180
180
|
n: number;
|
|
181
181
|
r: ZodTypeAny | undefined;
|
|
182
182
|
}>;
|
|
183
|
+
/** The document that local `$ref` pointers resolve against. */
|
|
184
|
+
root?: JsonSchema;
|
|
183
185
|
};
|
|
184
186
|
//#endregion
|
|
185
187
|
//#region src/json-schema-to-zod.d.ts
|
|
186
|
-
declare const jsonSchemaToZod: (schema: JsonSchema, options?: JsonSchemaToZodOptions) => z.ZodType;
|
|
188
|
+
export declare const jsonSchemaToZod: (schema: JsonSchema, options?: JsonSchemaToZodOptions) => z.ZodType;
|
|
187
189
|
/**
|
|
188
190
|
* A simplified type for Zod raw shapes that avoids deep type instantiation issues.
|
|
189
191
|
* This is compatible with ZodRawShape but uses a simpler type signature.
|
|
@@ -201,6 +203,24 @@ type SimpleZodRawShape = Record<string, ZodTypeAny>;
|
|
|
201
203
|
* @param options - Optional configuration for the conversion
|
|
202
204
|
* @returns A Zod raw shape object with property keys mapped to Zod types
|
|
203
205
|
*/
|
|
204
|
-
declare const jsonSchemaToZodShape: (schema: JsonSchema, options?: JsonSchemaToZodOptions) => SimpleZodRawShape;
|
|
206
|
+
export declare const jsonSchemaToZodShape: (schema: JsonSchema, options?: JsonSchemaToZodOptions) => SimpleZodRawShape;
|
|
205
207
|
//#endregion
|
|
206
|
-
|
|
208
|
+
//#region src/utils/compile-pattern.d.ts
|
|
209
|
+
type InvalidPatternReason = 'syntax' | 'too-long';
|
|
210
|
+
/**
|
|
211
|
+
* Raised at conversion time when a schema `pattern` cannot be turned into a
|
|
212
|
+
* `RegExp`. Conversion fails eagerly, in line with how this package and
|
|
213
|
+
* `@composio/json-schema-to-effect-schema` treat other schema defects: a broken
|
|
214
|
+
* schema is the tool author's bug, so it surfaces once at construction rather
|
|
215
|
+
* than on every validation as if the caller's input were wrong.
|
|
216
|
+
*/
|
|
217
|
+
export declare class InvalidPatternError extends Error {
|
|
218
|
+
readonly keyword: 'pattern' | 'patternProperties';
|
|
219
|
+
readonly pattern: string;
|
|
220
|
+
readonly path: ReadonlyArray<string | number>;
|
|
221
|
+
readonly reason: InvalidPatternReason;
|
|
222
|
+
readonly name = "InvalidPatternError";
|
|
223
|
+
constructor(keyword: 'pattern' | 'patternProperties', pattern: string, path: ReadonlyArray<string | number>, reason: InvalidPatternReason, detail: string, options?: ErrorOptions);
|
|
224
|
+
}
|
|
225
|
+
//#endregion
|
|
226
|
+
export type { InvalidPatternReason, JsonSchema, JsonSchemaObject, JsonSchemaToZodOptions, ParserOverride, ParserSelector, Refs, Serializable };
|
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,34 @@
|
|
|
1
1
|
import * as z$1 from "zod/v3";
|
|
2
2
|
import { z } from "zod/v3";
|
|
3
|
+
import { dequal } from "dequal/lite";
|
|
4
|
+
import { Validator, encodePointer } from "@cfworker/json-schema";
|
|
3
5
|
|
|
4
6
|
//#region src/utils/half.ts
|
|
5
7
|
const half = (arr) => {
|
|
6
8
|
return [arr.slice(0, arr.length / 2), arr.slice(arr.length / 2)];
|
|
7
9
|
};
|
|
8
10
|
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/utils/its.ts
|
|
13
|
+
const its = {
|
|
14
|
+
an: {
|
|
15
|
+
object: (x) => x.type === "object" || !x.type && (x.properties !== void 0 || x.additionalProperties !== void 0 || x.patternProperties !== void 0),
|
|
16
|
+
array: (x) => x.type === "array",
|
|
17
|
+
anyOf: (x) => x.anyOf !== void 0,
|
|
18
|
+
allOf: (x) => x.allOf !== void 0,
|
|
19
|
+
enum: (x) => x.enum !== void 0
|
|
20
|
+
},
|
|
21
|
+
a: {
|
|
22
|
+
nullable: (x) => x.nullable === true,
|
|
23
|
+
multipleType: (x) => Array.isArray(x.type),
|
|
24
|
+
not: (x) => x.not !== void 0,
|
|
25
|
+
const: (x) => x.const !== void 0,
|
|
26
|
+
primitive: (x, p) => x.type === p,
|
|
27
|
+
conditional: (x) => Boolean("if" in x && x.if && "then" in x && "else" in x && x.then && x.else),
|
|
28
|
+
oneOf: (x) => x.oneOf !== void 0
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
9
32
|
//#endregion
|
|
10
33
|
//#region src/parsers/parse-all-of.ts
|
|
11
34
|
const originalIndex = Symbol("Original index");
|
|
@@ -25,10 +48,10 @@ const ensureOriginalIndex = (arr) => {
|
|
|
25
48
|
}
|
|
26
49
|
return newArr;
|
|
27
50
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
const item =
|
|
51
|
+
const isClosedObjectBranch = (branch) => typeof branch === "object" && its.an.object(branch) && branch.properties !== void 0 && Object.keys(branch.properties).length > 0 && branch.additionalProperties === void 0 && branch.patternProperties === void 0 && !("$ref" in branch);
|
|
52
|
+
const intersect = (branches, refs) => {
|
|
53
|
+
if (branches.length === 1) {
|
|
54
|
+
const item = branches[0];
|
|
32
55
|
return parseSchema(item, {
|
|
33
56
|
...refs,
|
|
34
57
|
path: [
|
|
@@ -38,8 +61,28 @@ function parseAllOf(jsonSchema, refs) {
|
|
|
38
61
|
]
|
|
39
62
|
});
|
|
40
63
|
}
|
|
41
|
-
const [left, right] = half(
|
|
42
|
-
return z.intersection(
|
|
64
|
+
const [left, right] = half(branches);
|
|
65
|
+
return z.intersection(intersect(left, refs), intersect(right, refs));
|
|
66
|
+
};
|
|
67
|
+
function parseAllOf(jsonSchema, refs) {
|
|
68
|
+
if (jsonSchema.allOf.length === 0) return z.never();
|
|
69
|
+
if (jsonSchema.allOf.length === 1) return intersect(ensureOriginalIndex(jsonSchema.allOf), refs);
|
|
70
|
+
const opened = jsonSchema.allOf.map((branch) => isClosedObjectBranch(branch) ? {
|
|
71
|
+
...branch,
|
|
72
|
+
additionalProperties: true
|
|
73
|
+
} : branch);
|
|
74
|
+
const parsed = intersect(ensureOriginalIndex(opened), refs);
|
|
75
|
+
if (!jsonSchema.allOf.every(isClosedObjectBranch)) return parsed;
|
|
76
|
+
const declaredKeys = new Set(jsonSchema.allOf.flatMap((branch) => Object.keys(branch.properties ?? {})));
|
|
77
|
+
return parsed.superRefine((value, ctx) => {
|
|
78
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return;
|
|
79
|
+
const unrecognizedKeys = Object.keys(value).filter((key) => !declaredKeys.has(key));
|
|
80
|
+
if (unrecognizedKeys.length > 0) ctx.addIssue({
|
|
81
|
+
code: z.ZodIssueCode.unrecognized_keys,
|
|
82
|
+
keys: unrecognizedKeys,
|
|
83
|
+
message: `Unrecognized key(s) in object: ${unrecognizedKeys.map((key) => `'${key}'`).join(", ")}`
|
|
84
|
+
});
|
|
85
|
+
});
|
|
43
86
|
}
|
|
44
87
|
|
|
45
88
|
//#endregion
|
|
@@ -73,27 +116,6 @@ function extendSchemaWithMessage(zodSchema, jsonSchema, key, extend) {
|
|
|
73
116
|
return zodSchema;
|
|
74
117
|
}
|
|
75
118
|
|
|
76
|
-
//#endregion
|
|
77
|
-
//#region src/utils/its.ts
|
|
78
|
-
const its = {
|
|
79
|
-
an: {
|
|
80
|
-
object: (x) => x.type === "object" || !x.type && (x.properties !== void 0 || x.additionalProperties !== void 0 || x.patternProperties !== void 0),
|
|
81
|
-
array: (x) => x.type === "array",
|
|
82
|
-
anyOf: (x) => x.anyOf !== void 0,
|
|
83
|
-
allOf: (x) => x.allOf !== void 0,
|
|
84
|
-
enum: (x) => x.enum !== void 0
|
|
85
|
-
},
|
|
86
|
-
a: {
|
|
87
|
-
nullable: (x) => x.nullable === true,
|
|
88
|
-
multipleType: (x) => Array.isArray(x.type),
|
|
89
|
-
not: (x) => x.not !== void 0,
|
|
90
|
-
const: (x) => x.const !== void 0,
|
|
91
|
-
primitive: (x, p) => x.type === p,
|
|
92
|
-
conditional: (x) => Boolean("if" in x && x.if && "then" in x && "else" in x && x.then && x.else),
|
|
93
|
-
oneOf: (x) => x.oneOf !== void 0
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
|
|
97
119
|
//#endregion
|
|
98
120
|
//#region src/parsers/parse-array.ts
|
|
99
121
|
const parseArray = (jsonSchema, refs) => {
|
|
@@ -125,14 +147,30 @@ const parseArray = (jsonSchema, refs) => {
|
|
|
125
147
|
});
|
|
126
148
|
return parseSchema(newSchema, refs);
|
|
127
149
|
}
|
|
128
|
-
if (Array.isArray(jsonSchema.items))
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
150
|
+
if (Array.isArray(jsonSchema.items)) {
|
|
151
|
+
const parsedItems = jsonSchema.items.map((item, index) => parseSchema(item, {
|
|
152
|
+
...refs,
|
|
153
|
+
path: [
|
|
154
|
+
...refs.path,
|
|
155
|
+
"items",
|
|
156
|
+
index
|
|
157
|
+
]
|
|
158
|
+
}));
|
|
159
|
+
const prefixSchemas = parsedItems.map((_, length) => z.tuple(parsedItems.slice(0, length)));
|
|
160
|
+
const fullTuple = z.tuple(parsedItems);
|
|
161
|
+
let fullLengthSchema;
|
|
162
|
+
if (jsonSchema.additionalItems === false) fullLengthSchema = fullTuple;
|
|
163
|
+
else if (jsonSchema.additionalItems && jsonSchema.additionalItems !== true) fullLengthSchema = fullTuple.rest(parseSchema(jsonSchema.additionalItems, {
|
|
164
|
+
...refs,
|
|
165
|
+
path: [...refs.path, "additionalItems"]
|
|
166
|
+
}));
|
|
167
|
+
else fullLengthSchema = fullTuple.rest(z.any());
|
|
168
|
+
const variants = [...prefixSchemas, fullLengthSchema];
|
|
169
|
+
let tupleSchema = variants.length === 1 ? variants[0] : z.union(variants);
|
|
170
|
+
if (typeof jsonSchema.minItems === "number") tupleSchema = tupleSchema.refine((value) => value.length >= jsonSchema.minItems, { message: `Array must contain at least ${jsonSchema.minItems} element(s)` });
|
|
171
|
+
if (typeof jsonSchema.maxItems === "number") tupleSchema = tupleSchema.refine((value) => value.length <= jsonSchema.maxItems, { message: `Array must contain at most ${jsonSchema.maxItems} element(s)` });
|
|
172
|
+
return tupleSchema;
|
|
173
|
+
}
|
|
136
174
|
let zodSchema = !jsonSchema.items ? z.array(z.any()) : z.array(parseSchema(jsonSchema.items, {
|
|
137
175
|
...refs,
|
|
138
176
|
path: [...refs.path, "items"]
|
|
@@ -156,10 +194,26 @@ const parseBoolean = (_jsonSchema) => {
|
|
|
156
194
|
return z.boolean();
|
|
157
195
|
};
|
|
158
196
|
|
|
197
|
+
//#endregion
|
|
198
|
+
//#region src/parsers/parse-literal-values.ts
|
|
199
|
+
const parseLiteralValues = (values) => {
|
|
200
|
+
if (values.length === 0) return z.never();
|
|
201
|
+
if (values.every((value) => value === null || typeof value !== "object")) {
|
|
202
|
+
if (values.length === 1) return z.literal(values[0]);
|
|
203
|
+
if (values.every((value) => typeof value === "string")) return z.enum(values);
|
|
204
|
+
return z.union(values.map((value) => z.literal(value)));
|
|
205
|
+
}
|
|
206
|
+
return z.custom((value) => values.some((candidate) => dequal(value, candidate)));
|
|
207
|
+
};
|
|
208
|
+
|
|
159
209
|
//#endregion
|
|
160
210
|
//#region src/parsers/parse-const.ts
|
|
161
|
-
const parseConst = (jsonSchema) => {
|
|
162
|
-
|
|
211
|
+
const parseConst = (jsonSchema, refs) => {
|
|
212
|
+
const { const: value, ...baseSchema } = jsonSchema;
|
|
213
|
+
return parseSchema(baseSchema, {
|
|
214
|
+
...refs,
|
|
215
|
+
seen: new Map(refs.seen)
|
|
216
|
+
}, true).and(parseLiteralValues([value]));
|
|
163
217
|
};
|
|
164
218
|
|
|
165
219
|
//#endregion
|
|
@@ -170,11 +224,12 @@ const parseDefault = (_jsonSchema) => {
|
|
|
170
224
|
|
|
171
225
|
//#endregion
|
|
172
226
|
//#region src/parsers/parse-enum.ts
|
|
173
|
-
const parseEnum = (jsonSchema) => {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
227
|
+
const parseEnum = (jsonSchema, refs) => {
|
|
228
|
+
const { enum: values, ...baseSchema } = jsonSchema;
|
|
229
|
+
return parseSchema(baseSchema, {
|
|
230
|
+
...refs,
|
|
231
|
+
seen: new Map(refs.seen)
|
|
232
|
+
}, true).and(parseLiteralValues(values));
|
|
178
233
|
};
|
|
179
234
|
|
|
180
235
|
//#endregion
|
|
@@ -236,12 +291,37 @@ const omit = (obj, ...keys) => Object.keys(obj).reduce((acc, key) => {
|
|
|
236
291
|
*/
|
|
237
292
|
const parseNullable = (jsonSchema, refs) => {
|
|
238
293
|
const hasNullDefault = jsonSchema.default === null;
|
|
239
|
-
const
|
|
294
|
+
const baseSchema = hasNullDefault ? omit(omit(jsonSchema, "nullable"), "default") : omit(jsonSchema, "nullable");
|
|
295
|
+
const zodSchema = parseSchema(baseSchema, refs, true).nullable();
|
|
240
296
|
return hasNullDefault ? zodSchema.default(null) : zodSchema;
|
|
241
297
|
};
|
|
242
298
|
|
|
243
299
|
//#endregion
|
|
244
300
|
//#region src/parsers/parse-number.ts
|
|
301
|
+
const toDecimalInteger = (value) => {
|
|
302
|
+
if (!Number.isFinite(value)) return void 0;
|
|
303
|
+
const negative = value < 0;
|
|
304
|
+
const [coefficient, exponentText] = Math.abs(value).toString().toLowerCase().split("e");
|
|
305
|
+
const exponent = exponentText === void 0 ? 0 : Number(exponentText);
|
|
306
|
+
const [whole, fraction = ""] = coefficient.split(".");
|
|
307
|
+
let digits = `${whole}${fraction}`.replace(/^0+(?=\d)/, "");
|
|
308
|
+
let scale = fraction.length - exponent;
|
|
309
|
+
if (scale < 0) {
|
|
310
|
+
digits += "0".repeat(-scale);
|
|
311
|
+
scale = 0;
|
|
312
|
+
}
|
|
313
|
+
return {
|
|
314
|
+
integer: BigInt(`${negative ? "-" : ""}${digits}`),
|
|
315
|
+
scale
|
|
316
|
+
};
|
|
317
|
+
};
|
|
318
|
+
const isJsonMultipleOf = (value, multiple) => {
|
|
319
|
+
const dividend = toDecimalInteger(value);
|
|
320
|
+
const divisor = toDecimalInteger(multiple);
|
|
321
|
+
if (!dividend || !divisor || divisor.integer === 0n) return false;
|
|
322
|
+
const scale = Math.max(dividend.scale, divisor.scale);
|
|
323
|
+
return dividend.integer * 10n ** BigInt(scale - dividend.scale) % (divisor.integer * 10n ** BigInt(scale - divisor.scale)) === 0n;
|
|
324
|
+
};
|
|
245
325
|
const parseNumber = (jsonSchema) => {
|
|
246
326
|
let zodSchema = z.number();
|
|
247
327
|
let isInteger = false;
|
|
@@ -252,19 +332,16 @@ const parseNumber = (jsonSchema) => {
|
|
|
252
332
|
isInteger = true;
|
|
253
333
|
zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "format", (zs, _, errorMsg) => zs.int(errorMsg));
|
|
254
334
|
}
|
|
255
|
-
|
|
256
|
-
if (
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
if (typeof jsonSchema.maximum === "number") if (jsonSchema.exclusiveMaximum === true) zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "maximum", (zs, maximum, errorMsg) => zs.lt(maximum, errorMsg));
|
|
266
|
-
else zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "maximum", (zs, maximum, errorMsg) => zs.lte(maximum, errorMsg));
|
|
267
|
-
else if (typeof jsonSchema.exclusiveMaximum === "number") zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "exclusiveMaximum", (zs, exclusiveMaximum, errorMsg) => zs.lt(exclusiveMaximum, errorMsg));
|
|
335
|
+
if (typeof jsonSchema.minimum === "number") {
|
|
336
|
+
if (jsonSchema.exclusiveMinimum === true) zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "minimum", (zs, minimum, errorMsg) => zs.gt(minimum, errorMsg));
|
|
337
|
+
else zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "minimum", (zs, minimum, errorMsg) => zs.gte(minimum, errorMsg));
|
|
338
|
+
}
|
|
339
|
+
if (typeof jsonSchema.exclusiveMinimum === "number") zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "exclusiveMinimum", (zs, exclusiveMinimum, errorMsg) => zs.gt(exclusiveMinimum, errorMsg));
|
|
340
|
+
if (typeof jsonSchema.maximum === "number") {
|
|
341
|
+
if (jsonSchema.exclusiveMaximum === true) zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "maximum", (zs, maximum, errorMsg) => zs.lt(maximum, errorMsg));
|
|
342
|
+
else zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "maximum", (zs, maximum, errorMsg) => zs.lte(maximum, errorMsg));
|
|
343
|
+
}
|
|
344
|
+
if (typeof jsonSchema.exclusiveMaximum === "number") zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "exclusiveMaximum", (zs, exclusiveMaximum, errorMsg) => zs.lt(exclusiveMaximum, errorMsg));
|
|
268
345
|
if (typeof jsonSchema.min === "number" && typeof jsonSchema.minimum !== "number") zodSchema = extendSchemaWithMessage(zodSchema, {
|
|
269
346
|
...jsonSchema,
|
|
270
347
|
minimum: jsonSchema.min
|
|
@@ -273,6 +350,11 @@ const parseNumber = (jsonSchema) => {
|
|
|
273
350
|
...jsonSchema,
|
|
274
351
|
maximum: jsonSchema.max
|
|
275
352
|
}, "maximum", (zs, maximum, errorMsg) => zs.lte(maximum, errorMsg));
|
|
353
|
+
if (typeof jsonSchema.multipleOf === "number") {
|
|
354
|
+
const errorMessage = jsonSchema.errorMessage?.multipleOf;
|
|
355
|
+
if (jsonSchema.multipleOf === 1) return isInteger ? zodSchema : zodSchema.int(errorMessage);
|
|
356
|
+
return zodSchema.refine((value) => isJsonMultipleOf(value, jsonSchema.multipleOf), { message: errorMessage ?? `Number must be a multiple of ${jsonSchema.multipleOf}` });
|
|
357
|
+
}
|
|
276
358
|
return zodSchema;
|
|
277
359
|
};
|
|
278
360
|
|
|
@@ -307,6 +389,66 @@ const parseOneOf = (jsonSchema, refs) => {
|
|
|
307
389
|
});
|
|
308
390
|
};
|
|
309
391
|
|
|
392
|
+
//#endregion
|
|
393
|
+
//#region src/utils/compile-pattern.ts
|
|
394
|
+
/**
|
|
395
|
+
* Longest `pattern` / `patternProperties` key this package compiles. Tool
|
|
396
|
+
* schemas come from third-party toolkit definitions; anything past this is
|
|
397
|
+
* far outside what a format constraint needs and is rejected before it can
|
|
398
|
+
* cost anything at validation time.
|
|
399
|
+
*/
|
|
400
|
+
const MAX_PATTERN_LENGTH = 1024;
|
|
401
|
+
/**
|
|
402
|
+
* Raised at conversion time when a schema `pattern` cannot be turned into a
|
|
403
|
+
* `RegExp`. Conversion fails eagerly, in line with how this package and
|
|
404
|
+
* `@composio/json-schema-to-effect-schema` treat other schema defects: a broken
|
|
405
|
+
* schema is the tool author's bug, so it surfaces once at construction rather
|
|
406
|
+
* than on every validation as if the caller's input were wrong.
|
|
407
|
+
*/
|
|
408
|
+
var InvalidPatternError = class extends Error {
|
|
409
|
+
keyword;
|
|
410
|
+
pattern;
|
|
411
|
+
path;
|
|
412
|
+
reason;
|
|
413
|
+
name = "InvalidPatternError";
|
|
414
|
+
constructor(keyword, pattern, path, reason, detail, options) {
|
|
415
|
+
const location = path.length > 0 ? path.join(".") : "<root>";
|
|
416
|
+
super(`Invalid ${keyword} regular expression ${JSON.stringify(pattern)} at ${location}: ${detail}`, options);
|
|
417
|
+
this.keyword = keyword;
|
|
418
|
+
this.pattern = pattern;
|
|
419
|
+
this.path = path;
|
|
420
|
+
this.reason = reason;
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
/**
|
|
424
|
+
* Compile a schema pattern, rejecting anything that does not compile or that
|
|
425
|
+
* exceeds {@link MAX_PATTERN_LENGTH}.
|
|
426
|
+
*
|
|
427
|
+
* Both guards have no false positives: a pattern that fails here would never
|
|
428
|
+
* have worked. No backtracking (ReDoS) heuristic is applied on purpose — the
|
|
429
|
+
* syntactic checks that exist flag linear patterns such as `^(\d+\.)*\d+$`,
|
|
430
|
+
* and a wrong rejection fails the whole tool the schema belongs to. A hostile
|
|
431
|
+
* `pattern` that backtracks catastrophically remains a known limitation.
|
|
432
|
+
*
|
|
433
|
+
* No `u` flag is passed: existing tool schemas rely on identity escapes that
|
|
434
|
+
* Unicode mode refuses, and the goal here is to fail on defects, not to
|
|
435
|
+
* tighten which patterns are accepted.
|
|
436
|
+
*/
|
|
437
|
+
const compilePattern = (keyword, pattern, refs) => {
|
|
438
|
+
const path = keyword === "pattern" ? refs.path : [
|
|
439
|
+
...refs.path,
|
|
440
|
+
"patternProperties",
|
|
441
|
+
pattern
|
|
442
|
+
];
|
|
443
|
+
if (pattern.length > 1024) throw new InvalidPatternError(keyword, pattern, path, "too-long", `pattern exceeds ${MAX_PATTERN_LENGTH} characters`);
|
|
444
|
+
try {
|
|
445
|
+
return new RegExp(pattern);
|
|
446
|
+
} catch (cause) {
|
|
447
|
+
const detail = cause instanceof Error ? cause.message : String(cause);
|
|
448
|
+
throw new InvalidPatternError(keyword, pattern, path, "syntax", detail, { cause });
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
|
|
310
452
|
//#endregion
|
|
311
453
|
//#region src/parsers/parse-object.ts
|
|
312
454
|
/**
|
|
@@ -336,14 +478,15 @@ function parseObjectProperties(objectSchema, refs) {
|
|
|
336
478
|
]
|
|
337
479
|
});
|
|
338
480
|
const required = Array.isArray(objectSchema.required) ? objectSchema.required.includes(key) : false;
|
|
339
|
-
if (!required && propJsonSchema && typeof propJsonSchema === "object" && "default" in propJsonSchema)
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
481
|
+
if (!required && propJsonSchema && typeof propJsonSchema === "object" && "default" in propJsonSchema) {
|
|
482
|
+
if (propJsonSchema.default === null) {
|
|
483
|
+
const hasAnyOfWithNull = propJsonSchema.anyOf && Array.isArray(propJsonSchema.anyOf) && propJsonSchema.anyOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
|
|
484
|
+
const hasOneOfWithNull = propJsonSchema.oneOf && Array.isArray(propJsonSchema.oneOf) && propJsonSchema.oneOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
|
|
485
|
+
const isNullable = "nullable" in propJsonSchema && propJsonSchema.nullable === true;
|
|
486
|
+
if (hasAnyOfWithNull || hasOneOfWithNull || isNullable) properties[key] = propZodSchema.optional().default(null);
|
|
487
|
+
else properties[key] = propZodSchema.nullable().optional().default(null);
|
|
488
|
+
} else properties[key] = propZodSchema.optional().default(propJsonSchema.default);
|
|
489
|
+
} else properties[key] = required ? propZodSchema : propZodSchema.optional();
|
|
347
490
|
}
|
|
348
491
|
return z$1.object(properties);
|
|
349
492
|
}
|
|
@@ -357,7 +500,7 @@ function parseObjectProperties(objectSchema, refs) {
|
|
|
357
500
|
*/
|
|
358
501
|
function parseDynamicKeyObject(normalizedSchema, propertiesSchema, additionalProperties, refs) {
|
|
359
502
|
const patterns = Object.entries(normalizedSchema.patternProperties ?? {}).map(([pattern, schema]) => ({
|
|
360
|
-
regex:
|
|
503
|
+
regex: compilePattern("patternProperties", pattern, refs),
|
|
361
504
|
parser: parseSchema(schema, {
|
|
362
505
|
...refs,
|
|
363
506
|
path: [
|
|
@@ -421,14 +564,17 @@ function parseObject(objectSchema, refs) {
|
|
|
421
564
|
const isAdditionalPropertiesTrue = normalizedSchema.additionalProperties === true;
|
|
422
565
|
let output;
|
|
423
566
|
if (hasPatternProperties) output = parseDynamicKeyObject(normalizedSchema, propertiesSchema, additionalProperties, refs);
|
|
424
|
-
else if (propertiesSchema)
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
else
|
|
431
|
-
|
|
567
|
+
else if (propertiesSchema) {
|
|
568
|
+
if (additionalProperties) {
|
|
569
|
+
if (additionalProperties instanceof z$1.ZodNever) output = propertiesSchema.strict();
|
|
570
|
+
else if (isAdditionalPropertiesTrue) output = propertiesSchema.passthrough();
|
|
571
|
+
else output = propertiesSchema.catchall(additionalProperties);
|
|
572
|
+
} else output = propertiesSchema.strict();
|
|
573
|
+
} else if (additionalProperties) {
|
|
574
|
+
if (additionalProperties instanceof z$1.ZodNever) output = z$1.object({}).strict();
|
|
575
|
+
else if (isAdditionalPropertiesTrue) output = z$1.object({}).passthrough();
|
|
576
|
+
else output = z$1.record(additionalProperties);
|
|
577
|
+
} else output = z$1.object({}).passthrough();
|
|
432
578
|
if (its.an.anyOf(objectSchema)) output = output.and(parseAnyOf({
|
|
433
579
|
...objectSchema,
|
|
434
580
|
anyOf: objectSchema.anyOf.map((x) => typeof x === "object" && !x.type && (x.properties ?? x.additionalProperties ?? x.patternProperties) ? {
|
|
@@ -455,7 +601,7 @@ function parseObject(objectSchema, refs) {
|
|
|
455
601
|
|
|
456
602
|
//#endregion
|
|
457
603
|
//#region src/parsers/parse-string.ts
|
|
458
|
-
const parseString = (jsonSchema) => {
|
|
604
|
+
const parseString = (jsonSchema, refs) => {
|
|
459
605
|
let zodSchema = z.string();
|
|
460
606
|
zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "format", (zs, format, errorMsg) => {
|
|
461
607
|
switch (format) {
|
|
@@ -483,18 +629,207 @@ const parseString = (jsonSchema) => {
|
|
|
483
629
|
}
|
|
484
630
|
});
|
|
485
631
|
zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "contentEncoding", (zs, _, errorMsg) => zs.base64(errorMsg));
|
|
486
|
-
zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "pattern", (zs, pattern, errorMsg) => zs.regex(
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
632
|
+
zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, "pattern", (zs, pattern, errorMsg) => zs.regex(compilePattern("pattern", pattern, refs), errorMsg));
|
|
633
|
+
const errorMessages = jsonSchema.errorMessage;
|
|
634
|
+
const minLength = typeof jsonSchema.minLength === "number" ? jsonSchema.minLength : typeof jsonSchema.min === "number" ? jsonSchema.min : void 0;
|
|
635
|
+
const maxLength = typeof jsonSchema.maxLength === "number" ? jsonSchema.maxLength : typeof jsonSchema.max === "number" ? jsonSchema.max : void 0;
|
|
636
|
+
let result = zodSchema;
|
|
637
|
+
if (minLength !== void 0) result = result.refine((value) => codePointLength(value) >= minLength, { message: errorMessages?.minLength ?? `String must contain at least ${minLength} character(s)` });
|
|
638
|
+
if (maxLength !== void 0) result = result.refine((value) => codePointLength(value) <= maxLength, { message: errorMessages?.maxLength ?? `String must contain at most ${maxLength} character(s)` });
|
|
639
|
+
return result;
|
|
640
|
+
};
|
|
641
|
+
const codePointLength = (value) => [...value].length;
|
|
642
|
+
|
|
643
|
+
//#endregion
|
|
644
|
+
//#region src/parsers/parse-typeless-constraints.ts
|
|
645
|
+
const SCALAR_CONSTRAINT_KEYS = [
|
|
646
|
+
"minLength",
|
|
647
|
+
"maxLength",
|
|
648
|
+
"pattern",
|
|
649
|
+
"minimum",
|
|
650
|
+
"maximum",
|
|
651
|
+
"exclusiveMinimum",
|
|
652
|
+
"exclusiveMaximum",
|
|
653
|
+
"multipleOf"
|
|
654
|
+
];
|
|
655
|
+
const hasTypelessScalarConstraints = (schema) => schema.type === void 0 && SCALAR_CONSTRAINT_KEYS.some((key) => schema[key] !== void 0);
|
|
656
|
+
/**
|
|
657
|
+
* Draft 7 applies scalar constraints to matching instance types even when the
|
|
658
|
+
* schema declares no `type`: `{ "minLength": 2 }` restricts strings while
|
|
659
|
+
* leaving every non-string value untouched.
|
|
660
|
+
*/
|
|
661
|
+
const parseTypelessConstraints = (schema, refs) => {
|
|
662
|
+
const stringSchema = parseString({
|
|
663
|
+
...schema,
|
|
664
|
+
type: "string"
|
|
665
|
+
}, refs);
|
|
666
|
+
const numberSchema = parseNumber({
|
|
667
|
+
...schema,
|
|
668
|
+
type: "number"
|
|
669
|
+
});
|
|
670
|
+
return z.any().superRefine((value, ctx) => {
|
|
671
|
+
const applicable = typeof value === "string" ? stringSchema : typeof value === "number" ? numberSchema : void 0;
|
|
672
|
+
if (applicable === void 0) return;
|
|
673
|
+
const result = applicable.safeParse(value);
|
|
674
|
+
if (!result.success) for (const issue of result.error.issues) ctx.addIssue(issue);
|
|
675
|
+
});
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
//#endregion
|
|
679
|
+
//#region src/whole-schema-validation.ts
|
|
680
|
+
const REQUIRES_WHOLE_SCHEMA_VALIDATION = /* @__PURE__ */ new Set([
|
|
681
|
+
"$ref",
|
|
682
|
+
"additionalItems",
|
|
683
|
+
"allOf",
|
|
684
|
+
"anyOf",
|
|
685
|
+
"contains",
|
|
686
|
+
"dependencies",
|
|
687
|
+
"else",
|
|
688
|
+
"if",
|
|
689
|
+
"maxProperties",
|
|
690
|
+
"minProperties",
|
|
691
|
+
"not",
|
|
692
|
+
"oneOf",
|
|
693
|
+
"propertyNames",
|
|
694
|
+
"then",
|
|
695
|
+
"uniqueItems"
|
|
696
|
+
]);
|
|
697
|
+
const TYPELESS_TYPE_SCOPED_KEYWORDS = /* @__PURE__ */ new Set([
|
|
698
|
+
"properties",
|
|
699
|
+
"patternProperties",
|
|
700
|
+
"additionalProperties",
|
|
701
|
+
"propertyNames",
|
|
702
|
+
"required",
|
|
703
|
+
"dependencies",
|
|
704
|
+
"minProperties",
|
|
705
|
+
"maxProperties",
|
|
706
|
+
"items",
|
|
707
|
+
"additionalItems",
|
|
708
|
+
"contains",
|
|
709
|
+
"minItems",
|
|
710
|
+
"maxItems",
|
|
711
|
+
"uniqueItems"
|
|
712
|
+
]);
|
|
713
|
+
const SCHEMA_MAP_KEYWORDS = /* @__PURE__ */ new Set([
|
|
714
|
+
"$defs",
|
|
715
|
+
"definitions",
|
|
716
|
+
"dependencies",
|
|
717
|
+
"patternProperties",
|
|
718
|
+
"properties"
|
|
719
|
+
]);
|
|
720
|
+
const SCHEMA_ARRAY_KEYWORDS = /* @__PURE__ */ new Set([
|
|
721
|
+
"allOf",
|
|
722
|
+
"anyOf",
|
|
723
|
+
"oneOf"
|
|
724
|
+
]);
|
|
725
|
+
const SCHEMA_VALUE_KEYWORDS = /* @__PURE__ */ new Set([
|
|
726
|
+
"additionalItems",
|
|
727
|
+
"additionalProperties",
|
|
728
|
+
"contains",
|
|
729
|
+
"else",
|
|
730
|
+
"if",
|
|
731
|
+
"items",
|
|
732
|
+
"not",
|
|
733
|
+
"propertyNames",
|
|
734
|
+
"then"
|
|
735
|
+
]);
|
|
736
|
+
const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
737
|
+
const hasRequiredDefault = (schema) => {
|
|
738
|
+
if (!Array.isArray(schema.required) || !isObject(schema.properties)) return false;
|
|
739
|
+
return schema.required.some((name) => {
|
|
740
|
+
const property = schema.properties?.[name];
|
|
741
|
+
return isObject(property) && property.default !== void 0;
|
|
742
|
+
});
|
|
743
|
+
};
|
|
744
|
+
const hasUnparsedRequiredProperty = (schema) => {
|
|
745
|
+
if (!Array.isArray(schema.required)) return false;
|
|
746
|
+
const declared = new Set(Object.keys(schema.properties ?? {}));
|
|
747
|
+
return schema.required.some((name) => !declared.has(name));
|
|
748
|
+
};
|
|
749
|
+
/**
|
|
750
|
+
* The native parsers preserve useful Zod structure and materialize defaults,
|
|
751
|
+
* but some JSON Schema assertions cannot be represented by their first-match
|
|
752
|
+
* dispatch. Only those schemas receive an authoritative Draft 7 guard.
|
|
753
|
+
*/
|
|
754
|
+
const requiresWholeSchemaValidation = (schema, seen = /* @__PURE__ */ new WeakSet()) => {
|
|
755
|
+
if (!isObject(schema) || seen.has(schema)) return false;
|
|
756
|
+
seen.add(schema);
|
|
757
|
+
if (Object.keys(schema).some((key) => REQUIRES_WHOLE_SCHEMA_VALIDATION.has(key)) || schema.type === void 0 && Object.keys(schema).some((key) => TYPELESS_TYPE_SCOPED_KEYWORDS.has(key)) || hasUnparsedRequiredProperty(schema) || hasRequiredDefault(schema)) return true;
|
|
758
|
+
for (const [key, child] of Object.entries(schema)) if (SCHEMA_MAP_KEYWORDS.has(key) && isObject(child)) {
|
|
759
|
+
if (Object.values(child).some((value) => requiresWholeSchemaValidation(value, seen))) return true;
|
|
760
|
+
} else if (SCHEMA_ARRAY_KEYWORDS.has(key) && Array.isArray(child)) {
|
|
761
|
+
if (child.some((value) => requiresWholeSchemaValidation(value, seen))) return true;
|
|
762
|
+
} else if (SCHEMA_VALUE_KEYWORDS.has(key)) {
|
|
763
|
+
if ((Array.isArray(child) ? child : [child]).some((value) => requiresWholeSchemaValidation(value, seen))) return true;
|
|
764
|
+
}
|
|
765
|
+
return false;
|
|
766
|
+
};
|
|
767
|
+
/**
|
|
768
|
+
* Some schemas are valid for JSON instance types that the native parser does
|
|
769
|
+
* not materialize. Such a node is parsed as `z.any()` so the Draft 7 guard
|
|
770
|
+
* that every one of these keywords also activates (see
|
|
771
|
+
* `requiresWholeSchemaValidation`) stays the acceptance authority without
|
|
772
|
+
* piping into a narrower Zod schema. The check is deliberately local: a
|
|
773
|
+
* permissive descendant must not widen its enclosing parser, or sibling
|
|
774
|
+
* defaults would stop materializing.
|
|
775
|
+
*/
|
|
776
|
+
const requiresPermissiveMaterialization = (schema) => isObject(schema) && ("$ref" in schema || schema.type === void 0 && (Object.keys(schema).some((key) => TYPELESS_TYPE_SCOPED_KEYWORDS.has(key)) || [
|
|
777
|
+
"if",
|
|
778
|
+
"then",
|
|
779
|
+
"else",
|
|
780
|
+
"not"
|
|
781
|
+
].some((key) => key in schema)));
|
|
782
|
+
/**
|
|
783
|
+
* The guard schema for a node nested at `path` inside `root`: the enclosing
|
|
784
|
+
* document pointed at that node, so local `$ref`s resolve exactly as they do
|
|
785
|
+
* for the document root. Draft 7 ignores the keywords next to `$ref`, which
|
|
786
|
+
* is what lets the root's own assertions be reused as the pointer carrier.
|
|
787
|
+
*/
|
|
788
|
+
const guardSchemaAt = (node, refs) => isObject(refs.root) && refs.path.length > 0 ? {
|
|
789
|
+
...refs.root,
|
|
790
|
+
$ref: `#/${refs.path.map((part) => encodePointer(String(part))).join("/")}`
|
|
791
|
+
} : node;
|
|
792
|
+
/**
|
|
793
|
+
* OpenAPI 3.0 / Draft 4 spell exclusive bounds as a boolean flag next to
|
|
794
|
+
* `minimum`/`maximum`. The Draft 7 interpreter ignores the flag, so the guard
|
|
795
|
+
* receives the numeric spelling the native number parser already honors.
|
|
796
|
+
*/
|
|
797
|
+
const normalizeExclusiveBounds = (value, seen) => {
|
|
798
|
+
if (!isObject(value) || seen.has(value)) return;
|
|
799
|
+
seen.add(value);
|
|
800
|
+
if (value.exclusiveMinimum === true && typeof value.minimum === "number") {
|
|
801
|
+
value.exclusiveMinimum = value.minimum;
|
|
802
|
+
delete value.minimum;
|
|
803
|
+
} else if (typeof value.exclusiveMinimum === "boolean") delete value.exclusiveMinimum;
|
|
804
|
+
if (value.exclusiveMaximum === true && typeof value.maximum === "number") {
|
|
805
|
+
value.exclusiveMaximum = value.maximum;
|
|
806
|
+
delete value.maximum;
|
|
807
|
+
} else if (typeof value.exclusiveMaximum === "boolean") delete value.exclusiveMaximum;
|
|
808
|
+
for (const [key, child] of Object.entries(value)) if (SCHEMA_MAP_KEYWORDS.has(key) && isObject(child)) Object.values(child).forEach((nested) => normalizeExclusiveBounds(nested, seen));
|
|
809
|
+
else if (SCHEMA_ARRAY_KEYWORDS.has(key) && Array.isArray(child)) child.forEach((nested) => normalizeExclusiveBounds(nested, seen));
|
|
810
|
+
else if (SCHEMA_VALUE_KEYWORDS.has(key)) (Array.isArray(child) ? child : [child]).forEach((nested) => normalizeExclusiveBounds(nested, seen));
|
|
811
|
+
};
|
|
812
|
+
const withWholeSchemaValidation = (jsonSchema, parsedSchema) => {
|
|
813
|
+
const interpreterSchema = structuredClone(jsonSchema);
|
|
814
|
+
normalizeExclusiveBounds(interpreterSchema, /* @__PURE__ */ new WeakSet());
|
|
815
|
+
const validator = new Validator(interpreterSchema, "7", false);
|
|
816
|
+
const guardedSchema = z.any().superRefine((value, ctx) => {
|
|
817
|
+
let result;
|
|
818
|
+
try {
|
|
819
|
+
result = validator.validate(value);
|
|
820
|
+
} catch (cause) {
|
|
821
|
+
ctx.addIssue({
|
|
822
|
+
code: z.ZodIssueCode.custom,
|
|
823
|
+
message: `JSON Schema validation failed: ${String(cause)}`
|
|
824
|
+
});
|
|
825
|
+
return;
|
|
826
|
+
}
|
|
827
|
+
if (!result.valid) ctx.addIssue({
|
|
828
|
+
code: z.ZodIssueCode.custom,
|
|
829
|
+
message: result.errors[0]?.error ?? "Input does not satisfy the complete JSON Schema."
|
|
830
|
+
});
|
|
831
|
+
}).pipe(parsedSchema);
|
|
832
|
+
return parsedSchema.description ? guardedSchema.describe(parsedSchema.description) : guardedSchema;
|
|
498
833
|
};
|
|
499
834
|
|
|
500
835
|
//#endregion
|
|
@@ -530,17 +865,19 @@ const addAnnotations = (jsonSchema, zodSchema) => {
|
|
|
530
865
|
return zodSchema;
|
|
531
866
|
};
|
|
532
867
|
const selectParser = (schema, refs) => {
|
|
533
|
-
if (
|
|
868
|
+
if (requiresPermissiveMaterialization(schema)) return withWholeSchemaValidation(guardSchemaAt(schema, refs), z$1.any());
|
|
869
|
+
else if (its.a.nullable(schema)) return parseNullable(schema, refs);
|
|
870
|
+
else if (its.an.enum(schema)) return parseEnum(schema, refs);
|
|
871
|
+
else if (its.a.const(schema)) return parseConst(schema, refs);
|
|
534
872
|
else if (its.an.object(schema)) return parseObject(schema, refs);
|
|
535
873
|
else if (its.an.array(schema)) return parseArray(schema, refs);
|
|
536
874
|
else if (its.an.anyOf(schema)) return parseAnyOf(schema, refs);
|
|
537
875
|
else if (its.an.allOf(schema)) return parseAllOf(schema, refs);
|
|
538
876
|
else if (its.a.oneOf(schema)) return parseOneOf(schema, refs);
|
|
539
877
|
else if (its.a.not(schema)) return parseNot(schema, refs);
|
|
540
|
-
else if (
|
|
541
|
-
else if (its.a.const(schema)) return parseConst(schema);
|
|
878
|
+
else if (hasTypelessScalarConstraints(schema)) return parseTypelessConstraints(schema, refs);
|
|
542
879
|
else if (its.a.multipleType(schema)) return parseMultipleType(schema, refs);
|
|
543
|
-
else if (its.a.primitive(schema, "string")) return parseString(schema);
|
|
880
|
+
else if (its.a.primitive(schema, "string")) return parseString(schema, refs);
|
|
544
881
|
else if (its.a.primitive(schema, "number") || its.a.primitive(schema, "integer")) return parseNumber(schema);
|
|
545
882
|
else if (its.a.primitive(schema, "boolean")) return parseBoolean(schema);
|
|
546
883
|
else if (its.a.primitive(schema, "null")) return parseNull(schema);
|
|
@@ -595,23 +932,27 @@ function parseObjectShape(objectSchema, refs) {
|
|
|
595
932
|
const properties = {};
|
|
596
933
|
for (const key of propertyKeys) {
|
|
597
934
|
const propJsonSchema = objectSchema.properties[key];
|
|
598
|
-
const
|
|
935
|
+
const propRefs = {
|
|
599
936
|
...refs,
|
|
937
|
+
root: refs.root ?? objectSchema,
|
|
600
938
|
path: [
|
|
601
939
|
...refs.path,
|
|
602
940
|
"properties",
|
|
603
941
|
key
|
|
604
942
|
]
|
|
605
|
-
}
|
|
943
|
+
};
|
|
944
|
+
const parsedProperty = parseSchema(propJsonSchema, propRefs);
|
|
945
|
+
const propZodSchema = requiresWholeSchemaValidation(propJsonSchema) ? withWholeSchemaValidation(guardSchemaAt(propJsonSchema, propRefs), parsedProperty) : parsedProperty;
|
|
606
946
|
const required = Array.isArray(objectSchema.required) ? objectSchema.required.includes(key) : false;
|
|
607
|
-
if (!required && propJsonSchema && typeof propJsonSchema === "object" && "default" in propJsonSchema)
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
947
|
+
if (!required && propJsonSchema && typeof propJsonSchema === "object" && "default" in propJsonSchema) {
|
|
948
|
+
if (propJsonSchema.default === null) {
|
|
949
|
+
const hasAnyOfWithNull = propJsonSchema.anyOf && Array.isArray(propJsonSchema.anyOf) && propJsonSchema.anyOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
|
|
950
|
+
const hasOneOfWithNull = propJsonSchema.oneOf && Array.isArray(propJsonSchema.oneOf) && propJsonSchema.oneOf.some((schema) => typeof schema === "object" && schema !== null && schema.type === "null");
|
|
951
|
+
const isNullable = "nullable" in propJsonSchema && propJsonSchema.nullable === true;
|
|
952
|
+
if (hasAnyOfWithNull || hasOneOfWithNull || isNullable) properties[key] = propZodSchema.optional().default(null);
|
|
953
|
+
else properties[key] = propZodSchema.nullable().optional().default(null);
|
|
954
|
+
} else properties[key] = propZodSchema.optional().default(propJsonSchema.default);
|
|
955
|
+
} else properties[key] = required ? propZodSchema : propZodSchema.optional();
|
|
615
956
|
}
|
|
616
957
|
return properties;
|
|
617
958
|
}
|
|
@@ -619,11 +960,13 @@ function parseObjectShape(objectSchema, refs) {
|
|
|
619
960
|
//#endregion
|
|
620
961
|
//#region src/json-schema-to-zod.ts
|
|
621
962
|
const jsonSchemaToZod = (schema, options = {}) => {
|
|
622
|
-
|
|
963
|
+
const parsedSchema = parseSchema(schema, {
|
|
623
964
|
path: [],
|
|
624
965
|
seen: /* @__PURE__ */ new Map(),
|
|
966
|
+
root: schema,
|
|
625
967
|
...options
|
|
626
968
|
});
|
|
969
|
+
return requiresWholeSchemaValidation(schema) ? withWholeSchemaValidation(schema, parsedSchema) : parsedSchema;
|
|
627
970
|
};
|
|
628
971
|
/**
|
|
629
972
|
* Converts a JSON Schema object to a Zod raw shape.
|
|
@@ -639,12 +982,14 @@ const jsonSchemaToZod = (schema, options = {}) => {
|
|
|
639
982
|
*/
|
|
640
983
|
const jsonSchemaToZodShape = (schema, options = {}) => {
|
|
641
984
|
if (typeof schema !== "object" || schema === null) return {};
|
|
642
|
-
|
|
985
|
+
const objectSchema = schema;
|
|
986
|
+
return parseObjectShape(objectSchema, {
|
|
643
987
|
path: [],
|
|
644
988
|
seen: /* @__PURE__ */ new Map(),
|
|
989
|
+
root: objectSchema,
|
|
645
990
|
...options
|
|
646
991
|
});
|
|
647
992
|
};
|
|
648
993
|
|
|
649
994
|
//#endregion
|
|
650
|
-
export { jsonSchemaToZod, jsonSchemaToZodShape };
|
|
995
|
+
export { InvalidPatternError, jsonSchemaToZod, jsonSchemaToZodShape };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@composio/json-schema-to-zod",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3-beta.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.mjs",
|
|
6
6
|
"type": "module",
|
|
@@ -37,15 +37,21 @@
|
|
|
37
37
|
"license": "ISC",
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@types/json-schema": "^7.0.15",
|
|
40
|
-
"@types/node": "^26.
|
|
41
|
-
"
|
|
40
|
+
"@types/node": "^26.4.1",
|
|
41
|
+
"ajv": "^8.20.0",
|
|
42
|
+
"fast-check": "^4.9.0",
|
|
43
|
+
"tsdown": "^0.23.0",
|
|
42
44
|
"typescript": "^7.0.2",
|
|
43
45
|
"vitest": "^4.1.11",
|
|
44
|
-
"zod": "^4.4
|
|
46
|
+
"zod": "^4.5.4"
|
|
45
47
|
},
|
|
46
48
|
"peerDependencies": {
|
|
47
49
|
"zod": ">=3.25.76 <5"
|
|
48
50
|
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@cfworker/json-schema": "^4.1.1",
|
|
53
|
+
"dequal": "^2.0.3"
|
|
54
|
+
},
|
|
49
55
|
"scripts": {
|
|
50
56
|
"clean": "git clean -xdf node_modules",
|
|
51
57
|
"build": "tsdown",
|