zod 4.2.0-canary.20251213T203150 → 4.2.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/package.json +1 -1
- package/src/v4/classic/external.ts +2 -1
- package/src/v4/classic/from-json-schema.ts +527 -0
- package/src/v4/classic/schemas.ts +43 -0
- package/src/v4/classic/tests/from-json-schema.test.ts +537 -0
- package/src/v4/classic/tests/record.test.ts +37 -0
- package/src/v4/classic/tests/union.test.ts +38 -0
- package/src/v4/core/api.ts +15 -0
- package/src/v4/core/errors.ts +12 -1
- package/src/v4/core/json-schema-processors.ts +5 -5
- package/src/v4/core/schemas.ts +99 -10
- package/src/v4/core/versions.ts +2 -2
- package/src/v4/mini/external.ts +1 -1
- package/src/v4/mini/schemas.ts +39 -0
- package/v4/classic/external.cjs +5 -2
- package/v4/classic/external.d.cts +3 -1
- package/v4/classic/external.d.ts +3 -1
- package/v4/classic/external.js +3 -1
- package/v4/classic/from-json-schema.cjs +503 -0
- package/v4/classic/from-json-schema.d.cts +10 -0
- package/v4/classic/from-json-schema.d.ts +10 -0
- package/v4/classic/from-json-schema.js +477 -0
- package/v4/classic/schemas.cjs +30 -2
- package/v4/classic/schemas.d.cts +10 -0
- package/v4/classic/schemas.d.ts +10 -0
- package/v4/classic/schemas.js +26 -0
- package/v4/core/api.cjs +9 -0
- package/v4/core/api.d.cts +2 -0
- package/v4/core/api.d.ts +2 -0
- package/v4/core/api.js +8 -0
- package/v4/core/errors.d.cts +10 -1
- package/v4/core/errors.d.ts +10 -1
- package/v4/core/json-schema-processors.cjs +5 -5
- package/v4/core/json-schema-processors.js +5 -5
- package/v4/core/schemas.cjs +76 -11
- package/v4/core/schemas.d.cts +9 -0
- package/v4/core/schemas.d.ts +9 -0
- package/v4/core/schemas.js +74 -9
- package/v4/core/versions.cjs +2 -2
- package/v4/core/versions.d.cts +1 -1
- package/v4/core/versions.d.ts +1 -1
- package/v4/core/versions.js +2 -2
- package/v4/mini/external.cjs +3 -2
- package/v4/mini/external.d.cts +2 -1
- package/v4/mini/external.d.ts +2 -1
- package/v4/mini/external.js +2 -1
- package/v4/mini/schemas.cjs +28 -2
- package/v4/mini/schemas.d.cts +8 -0
- package/v4/mini/schemas.d.ts +8 -0
- package/v4/mini/schemas.js +24 -0
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.fromJSONSchema = fromJSONSchema;
|
|
27
|
+
const z = __importStar(require("../index.cjs"));
|
|
28
|
+
function detectVersion(schema, defaultTarget) {
|
|
29
|
+
const $schema = schema.$schema;
|
|
30
|
+
if ($schema === "https://json-schema.org/draft/2020-12/schema") {
|
|
31
|
+
return "draft-2020-12";
|
|
32
|
+
}
|
|
33
|
+
if ($schema === "http://json-schema.org/draft-07/schema#") {
|
|
34
|
+
return "draft-7";
|
|
35
|
+
}
|
|
36
|
+
if ($schema === "http://json-schema.org/draft-04/schema#") {
|
|
37
|
+
return "draft-4";
|
|
38
|
+
}
|
|
39
|
+
// Use defaultTarget if provided, otherwise default to draft-2020-12
|
|
40
|
+
return defaultTarget ?? "draft-2020-12";
|
|
41
|
+
}
|
|
42
|
+
function resolveRef(ref, ctx) {
|
|
43
|
+
if (!ref.startsWith("#")) {
|
|
44
|
+
throw new Error("External $ref is not supported, only local refs (#/...) are allowed");
|
|
45
|
+
}
|
|
46
|
+
const path = ref.slice(1).split("/").filter(Boolean);
|
|
47
|
+
// Handle root reference "#"
|
|
48
|
+
if (path.length === 0) {
|
|
49
|
+
return ctx.rootSchema;
|
|
50
|
+
}
|
|
51
|
+
const defsKey = ctx.version === "draft-2020-12" ? "$defs" : "definitions";
|
|
52
|
+
if (path[0] === defsKey) {
|
|
53
|
+
const key = path[1];
|
|
54
|
+
if (!key || !ctx.defs[key]) {
|
|
55
|
+
throw new Error(`Reference not found: ${ref}`);
|
|
56
|
+
}
|
|
57
|
+
return ctx.defs[key];
|
|
58
|
+
}
|
|
59
|
+
throw new Error(`Reference not found: ${ref}`);
|
|
60
|
+
}
|
|
61
|
+
function convertBaseSchema(schema, ctx) {
|
|
62
|
+
// Handle unsupported features
|
|
63
|
+
if (schema.not !== undefined) {
|
|
64
|
+
// Special case: { not: {} } represents never
|
|
65
|
+
if (typeof schema.not === "object" && Object.keys(schema.not).length === 0) {
|
|
66
|
+
return z.never();
|
|
67
|
+
}
|
|
68
|
+
throw new Error("not is not supported in Zod (except { not: {} } for never)");
|
|
69
|
+
}
|
|
70
|
+
if (schema.unevaluatedItems !== undefined) {
|
|
71
|
+
throw new Error("unevaluatedItems is not supported");
|
|
72
|
+
}
|
|
73
|
+
if (schema.unevaluatedProperties !== undefined) {
|
|
74
|
+
throw new Error("unevaluatedProperties is not supported");
|
|
75
|
+
}
|
|
76
|
+
if (schema.if !== undefined || schema.then !== undefined || schema.else !== undefined) {
|
|
77
|
+
throw new Error("Conditional schemas (if/then/else) are not supported");
|
|
78
|
+
}
|
|
79
|
+
if (schema.dependentSchemas !== undefined || schema.dependentRequired !== undefined) {
|
|
80
|
+
throw new Error("dependentSchemas and dependentRequired are not supported");
|
|
81
|
+
}
|
|
82
|
+
// Handle $ref
|
|
83
|
+
if (schema.$ref) {
|
|
84
|
+
const refPath = schema.$ref;
|
|
85
|
+
if (ctx.refs.has(refPath)) {
|
|
86
|
+
return ctx.refs.get(refPath);
|
|
87
|
+
}
|
|
88
|
+
if (ctx.processing.has(refPath)) {
|
|
89
|
+
// Circular reference - use lazy
|
|
90
|
+
return z.lazy(() => {
|
|
91
|
+
if (!ctx.refs.has(refPath)) {
|
|
92
|
+
throw new Error(`Circular reference not resolved: ${refPath}`);
|
|
93
|
+
}
|
|
94
|
+
return ctx.refs.get(refPath);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
ctx.processing.add(refPath);
|
|
98
|
+
const resolved = resolveRef(refPath, ctx);
|
|
99
|
+
const zodSchema = convertSchema(resolved, ctx);
|
|
100
|
+
ctx.refs.set(refPath, zodSchema);
|
|
101
|
+
ctx.processing.delete(refPath);
|
|
102
|
+
return zodSchema;
|
|
103
|
+
}
|
|
104
|
+
// Handle enum
|
|
105
|
+
if (schema.enum !== undefined) {
|
|
106
|
+
const enumValues = schema.enum;
|
|
107
|
+
// Special case: OpenAPI 3.0 null representation { type: "string", nullable: true, enum: [null] }
|
|
108
|
+
if (ctx.version === "openapi-3.0" &&
|
|
109
|
+
schema.nullable === true &&
|
|
110
|
+
enumValues.length === 1 &&
|
|
111
|
+
enumValues[0] === null) {
|
|
112
|
+
return z.null();
|
|
113
|
+
}
|
|
114
|
+
if (enumValues.length === 0) {
|
|
115
|
+
return z.never();
|
|
116
|
+
}
|
|
117
|
+
if (enumValues.length === 1) {
|
|
118
|
+
return z.literal(enumValues[0]);
|
|
119
|
+
}
|
|
120
|
+
// Check if all values are strings
|
|
121
|
+
if (enumValues.every((v) => typeof v === "string")) {
|
|
122
|
+
return z.enum(enumValues);
|
|
123
|
+
}
|
|
124
|
+
// Mixed types - use union of literals
|
|
125
|
+
const literalSchemas = enumValues.map((v) => z.literal(v));
|
|
126
|
+
if (literalSchemas.length < 2) {
|
|
127
|
+
return literalSchemas[0];
|
|
128
|
+
}
|
|
129
|
+
return z.union([literalSchemas[0], literalSchemas[1], ...literalSchemas.slice(2)]);
|
|
130
|
+
}
|
|
131
|
+
// Handle const
|
|
132
|
+
if (schema.const !== undefined) {
|
|
133
|
+
return z.literal(schema.const);
|
|
134
|
+
}
|
|
135
|
+
// Handle type
|
|
136
|
+
const type = schema.type;
|
|
137
|
+
if (Array.isArray(type)) {
|
|
138
|
+
// Expand type array into anyOf union
|
|
139
|
+
const typeSchemas = type.map((t) => {
|
|
140
|
+
const typeSchema = { ...schema, type: t };
|
|
141
|
+
return convertBaseSchema(typeSchema, ctx);
|
|
142
|
+
});
|
|
143
|
+
if (typeSchemas.length === 0) {
|
|
144
|
+
return z.never();
|
|
145
|
+
}
|
|
146
|
+
if (typeSchemas.length === 1) {
|
|
147
|
+
return typeSchemas[0];
|
|
148
|
+
}
|
|
149
|
+
return z.union(typeSchemas);
|
|
150
|
+
}
|
|
151
|
+
if (!type) {
|
|
152
|
+
// No type specified - empty schema (any)
|
|
153
|
+
return z.any();
|
|
154
|
+
}
|
|
155
|
+
let zodSchema;
|
|
156
|
+
switch (type) {
|
|
157
|
+
case "string": {
|
|
158
|
+
let stringSchema = z.string();
|
|
159
|
+
// Apply format using .check() with Zod format functions
|
|
160
|
+
if (schema.format) {
|
|
161
|
+
const format = schema.format;
|
|
162
|
+
// Map common formats to Zod check functions
|
|
163
|
+
if (format === "email") {
|
|
164
|
+
stringSchema = stringSchema.check(z.email());
|
|
165
|
+
}
|
|
166
|
+
else if (format === "uri" || format === "uri-reference") {
|
|
167
|
+
stringSchema = stringSchema.check(z.url());
|
|
168
|
+
}
|
|
169
|
+
else if (format === "uuid" || format === "guid") {
|
|
170
|
+
stringSchema = stringSchema.check(z.uuid());
|
|
171
|
+
}
|
|
172
|
+
else if (format === "date-time") {
|
|
173
|
+
stringSchema = stringSchema.check(z.iso.datetime());
|
|
174
|
+
}
|
|
175
|
+
else if (format === "date") {
|
|
176
|
+
stringSchema = stringSchema.check(z.iso.date());
|
|
177
|
+
}
|
|
178
|
+
else if (format === "time") {
|
|
179
|
+
stringSchema = stringSchema.check(z.iso.time());
|
|
180
|
+
}
|
|
181
|
+
else if (format === "duration") {
|
|
182
|
+
stringSchema = stringSchema.check(z.iso.duration());
|
|
183
|
+
}
|
|
184
|
+
else if (format === "ipv4") {
|
|
185
|
+
stringSchema = stringSchema.check(z.ipv4());
|
|
186
|
+
}
|
|
187
|
+
else if (format === "ipv6") {
|
|
188
|
+
stringSchema = stringSchema.check(z.ipv6());
|
|
189
|
+
}
|
|
190
|
+
else if (format === "mac") {
|
|
191
|
+
stringSchema = stringSchema.check(z.mac());
|
|
192
|
+
}
|
|
193
|
+
else if (format === "cidr") {
|
|
194
|
+
stringSchema = stringSchema.check(z.cidrv4());
|
|
195
|
+
}
|
|
196
|
+
else if (format === "cidr-v6") {
|
|
197
|
+
stringSchema = stringSchema.check(z.cidrv6());
|
|
198
|
+
}
|
|
199
|
+
else if (format === "base64") {
|
|
200
|
+
stringSchema = stringSchema.check(z.base64());
|
|
201
|
+
}
|
|
202
|
+
else if (format === "base64url") {
|
|
203
|
+
stringSchema = stringSchema.check(z.base64url());
|
|
204
|
+
}
|
|
205
|
+
else if (format === "e164") {
|
|
206
|
+
stringSchema = stringSchema.check(z.e164());
|
|
207
|
+
}
|
|
208
|
+
else if (format === "jwt") {
|
|
209
|
+
stringSchema = stringSchema.check(z.jwt());
|
|
210
|
+
}
|
|
211
|
+
else if (format === "emoji") {
|
|
212
|
+
stringSchema = stringSchema.check(z.emoji());
|
|
213
|
+
}
|
|
214
|
+
else if (format === "nanoid") {
|
|
215
|
+
stringSchema = stringSchema.check(z.nanoid());
|
|
216
|
+
}
|
|
217
|
+
else if (format === "cuid") {
|
|
218
|
+
stringSchema = stringSchema.check(z.cuid());
|
|
219
|
+
}
|
|
220
|
+
else if (format === "cuid2") {
|
|
221
|
+
stringSchema = stringSchema.check(z.cuid2());
|
|
222
|
+
}
|
|
223
|
+
else if (format === "ulid") {
|
|
224
|
+
stringSchema = stringSchema.check(z.ulid());
|
|
225
|
+
}
|
|
226
|
+
else if (format === "xid") {
|
|
227
|
+
stringSchema = stringSchema.check(z.xid());
|
|
228
|
+
}
|
|
229
|
+
else if (format === "ksuid") {
|
|
230
|
+
stringSchema = stringSchema.check(z.ksuid());
|
|
231
|
+
}
|
|
232
|
+
// Note: json-string format is not currently supported by Zod
|
|
233
|
+
// Custom formats are ignored - keep as plain string
|
|
234
|
+
}
|
|
235
|
+
// Apply constraints
|
|
236
|
+
if (typeof schema.minLength === "number") {
|
|
237
|
+
stringSchema = stringSchema.min(schema.minLength);
|
|
238
|
+
}
|
|
239
|
+
if (typeof schema.maxLength === "number") {
|
|
240
|
+
stringSchema = stringSchema.max(schema.maxLength);
|
|
241
|
+
}
|
|
242
|
+
if (schema.pattern) {
|
|
243
|
+
// JSON Schema patterns are not implicitly anchored (match anywhere in string)
|
|
244
|
+
stringSchema = stringSchema.regex(new RegExp(schema.pattern));
|
|
245
|
+
}
|
|
246
|
+
zodSchema = stringSchema;
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
case "number":
|
|
250
|
+
case "integer": {
|
|
251
|
+
let numberSchema = type === "integer" ? z.number().int() : z.number();
|
|
252
|
+
// Apply constraints
|
|
253
|
+
if (typeof schema.minimum === "number") {
|
|
254
|
+
numberSchema = numberSchema.min(schema.minimum);
|
|
255
|
+
}
|
|
256
|
+
if (typeof schema.maximum === "number") {
|
|
257
|
+
numberSchema = numberSchema.max(schema.maximum);
|
|
258
|
+
}
|
|
259
|
+
if (typeof schema.exclusiveMinimum === "number") {
|
|
260
|
+
numberSchema = numberSchema.gt(schema.exclusiveMinimum);
|
|
261
|
+
}
|
|
262
|
+
else if (schema.exclusiveMinimum === true && typeof schema.minimum === "number") {
|
|
263
|
+
numberSchema = numberSchema.gt(schema.minimum);
|
|
264
|
+
}
|
|
265
|
+
if (typeof schema.exclusiveMaximum === "number") {
|
|
266
|
+
numberSchema = numberSchema.lt(schema.exclusiveMaximum);
|
|
267
|
+
}
|
|
268
|
+
else if (schema.exclusiveMaximum === true && typeof schema.maximum === "number") {
|
|
269
|
+
numberSchema = numberSchema.lt(schema.maximum);
|
|
270
|
+
}
|
|
271
|
+
if (typeof schema.multipleOf === "number") {
|
|
272
|
+
numberSchema = numberSchema.multipleOf(schema.multipleOf);
|
|
273
|
+
}
|
|
274
|
+
zodSchema = numberSchema;
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
case "boolean": {
|
|
278
|
+
zodSchema = z.boolean();
|
|
279
|
+
break;
|
|
280
|
+
}
|
|
281
|
+
case "null": {
|
|
282
|
+
zodSchema = z.null();
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
case "object": {
|
|
286
|
+
const shape = {};
|
|
287
|
+
const properties = schema.properties || {};
|
|
288
|
+
const requiredSet = new Set(schema.required || []);
|
|
289
|
+
// Convert properties - mark optional ones
|
|
290
|
+
for (const [key, propSchema] of Object.entries(properties)) {
|
|
291
|
+
const propZodSchema = convertSchema(propSchema, ctx);
|
|
292
|
+
// If not in required array, make it optional
|
|
293
|
+
shape[key] = requiredSet.has(key) ? propZodSchema : propZodSchema.optional();
|
|
294
|
+
}
|
|
295
|
+
// Handle propertyNames
|
|
296
|
+
if (schema.propertyNames) {
|
|
297
|
+
const keySchema = convertSchema(schema.propertyNames, ctx);
|
|
298
|
+
const valueSchema = schema.additionalProperties && typeof schema.additionalProperties === "object"
|
|
299
|
+
? convertSchema(schema.additionalProperties, ctx)
|
|
300
|
+
: z.any();
|
|
301
|
+
// Case A: No properties (pure record)
|
|
302
|
+
if (Object.keys(shape).length === 0) {
|
|
303
|
+
zodSchema = z.record(keySchema, valueSchema);
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
// Case B: With properties (intersection of object and looseRecord)
|
|
307
|
+
const objectSchema = z.object(shape).passthrough();
|
|
308
|
+
const recordSchema = z.looseRecord(keySchema, valueSchema);
|
|
309
|
+
zodSchema = z.intersection(objectSchema, recordSchema);
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
// Handle patternProperties
|
|
313
|
+
if (schema.patternProperties) {
|
|
314
|
+
// patternProperties: keys matching pattern must satisfy corresponding schema
|
|
315
|
+
// Use loose records so non-matching keys pass through
|
|
316
|
+
const patternProps = schema.patternProperties;
|
|
317
|
+
const patternKeys = Object.keys(patternProps);
|
|
318
|
+
const looseRecords = [];
|
|
319
|
+
for (const pattern of patternKeys) {
|
|
320
|
+
const patternValue = convertSchema(patternProps[pattern], ctx);
|
|
321
|
+
const keySchema = z.string().regex(new RegExp(pattern));
|
|
322
|
+
looseRecords.push(z.looseRecord(keySchema, patternValue));
|
|
323
|
+
}
|
|
324
|
+
// Build intersection: object schema + all pattern property records
|
|
325
|
+
const schemasToIntersect = [];
|
|
326
|
+
if (Object.keys(shape).length > 0) {
|
|
327
|
+
// Use passthrough so patternProperties can validate additional keys
|
|
328
|
+
schemasToIntersect.push(z.object(shape).passthrough());
|
|
329
|
+
}
|
|
330
|
+
schemasToIntersect.push(...looseRecords);
|
|
331
|
+
if (schemasToIntersect.length === 0) {
|
|
332
|
+
zodSchema = z.object({}).passthrough();
|
|
333
|
+
}
|
|
334
|
+
else if (schemasToIntersect.length === 1) {
|
|
335
|
+
zodSchema = schemasToIntersect[0];
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
// Chain intersections: (A & B) & C & D ...
|
|
339
|
+
let result = z.intersection(schemasToIntersect[0], schemasToIntersect[1]);
|
|
340
|
+
for (let i = 2; i < schemasToIntersect.length; i++) {
|
|
341
|
+
result = z.intersection(result, schemasToIntersect[i]);
|
|
342
|
+
}
|
|
343
|
+
zodSchema = result;
|
|
344
|
+
}
|
|
345
|
+
break;
|
|
346
|
+
}
|
|
347
|
+
// Handle additionalProperties
|
|
348
|
+
// In JSON Schema, additionalProperties defaults to true (allow any extra properties)
|
|
349
|
+
// In Zod, objects strip unknown keys by default, so we need to handle this explicitly
|
|
350
|
+
const objectSchema = z.object(shape);
|
|
351
|
+
if (schema.additionalProperties === false) {
|
|
352
|
+
// Strict mode - no extra properties allowed
|
|
353
|
+
zodSchema = objectSchema.strict();
|
|
354
|
+
}
|
|
355
|
+
else if (typeof schema.additionalProperties === "object") {
|
|
356
|
+
// Extra properties must match the specified schema
|
|
357
|
+
zodSchema = objectSchema.catchall(convertSchema(schema.additionalProperties, ctx));
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
// additionalProperties is true or undefined - allow any extra properties (passthrough)
|
|
361
|
+
zodSchema = objectSchema.passthrough();
|
|
362
|
+
}
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
case "array": {
|
|
366
|
+
// TODO: uniqueItems is not supported
|
|
367
|
+
// TODO: contains/minContains/maxContains are not supported
|
|
368
|
+
// Check if this is a tuple (prefixItems or items as array)
|
|
369
|
+
const prefixItems = schema.prefixItems;
|
|
370
|
+
const items = schema.items;
|
|
371
|
+
if (prefixItems && Array.isArray(prefixItems)) {
|
|
372
|
+
// Tuple with prefixItems (draft-2020-12)
|
|
373
|
+
const tupleItems = prefixItems.map((item) => convertSchema(item, ctx));
|
|
374
|
+
const rest = items && typeof items === "object" && !Array.isArray(items)
|
|
375
|
+
? convertSchema(items, ctx)
|
|
376
|
+
: undefined;
|
|
377
|
+
if (rest) {
|
|
378
|
+
zodSchema = z.tuple(tupleItems).rest(rest);
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
zodSchema = z.tuple(tupleItems);
|
|
382
|
+
}
|
|
383
|
+
// Apply minItems/maxItems constraints to tuples
|
|
384
|
+
if (typeof schema.minItems === "number") {
|
|
385
|
+
zodSchema = zodSchema.check(z.minLength(schema.minItems));
|
|
386
|
+
}
|
|
387
|
+
if (typeof schema.maxItems === "number") {
|
|
388
|
+
zodSchema = zodSchema.check(z.maxLength(schema.maxItems));
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
else if (Array.isArray(items)) {
|
|
392
|
+
// Tuple with items array (draft-7)
|
|
393
|
+
const tupleItems = items.map((item) => convertSchema(item, ctx));
|
|
394
|
+
const rest = schema.additionalItems && typeof schema.additionalItems === "object"
|
|
395
|
+
? convertSchema(schema.additionalItems, ctx)
|
|
396
|
+
: undefined; // additionalItems: false means no rest, handled by default tuple behavior
|
|
397
|
+
if (rest) {
|
|
398
|
+
zodSchema = z.tuple(tupleItems).rest(rest);
|
|
399
|
+
}
|
|
400
|
+
else {
|
|
401
|
+
zodSchema = z.tuple(tupleItems);
|
|
402
|
+
}
|
|
403
|
+
// Apply minItems/maxItems constraints to tuples
|
|
404
|
+
if (typeof schema.minItems === "number") {
|
|
405
|
+
zodSchema = zodSchema.check(z.minLength(schema.minItems));
|
|
406
|
+
}
|
|
407
|
+
if (typeof schema.maxItems === "number") {
|
|
408
|
+
zodSchema = zodSchema.check(z.maxLength(schema.maxItems));
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
else if (items !== undefined) {
|
|
412
|
+
// Regular array
|
|
413
|
+
const element = convertSchema(items, ctx);
|
|
414
|
+
let arraySchema = z.array(element);
|
|
415
|
+
// Apply constraints
|
|
416
|
+
if (typeof schema.minItems === "number") {
|
|
417
|
+
arraySchema = arraySchema.min(schema.minItems);
|
|
418
|
+
}
|
|
419
|
+
if (typeof schema.maxItems === "number") {
|
|
420
|
+
arraySchema = arraySchema.max(schema.maxItems);
|
|
421
|
+
}
|
|
422
|
+
zodSchema = arraySchema;
|
|
423
|
+
}
|
|
424
|
+
else {
|
|
425
|
+
// No items specified - array of any
|
|
426
|
+
zodSchema = z.array(z.any());
|
|
427
|
+
}
|
|
428
|
+
break;
|
|
429
|
+
}
|
|
430
|
+
default:
|
|
431
|
+
throw new Error(`Unsupported type: ${type}`);
|
|
432
|
+
}
|
|
433
|
+
// Apply metadata
|
|
434
|
+
if (schema.description) {
|
|
435
|
+
zodSchema = zodSchema.describe(schema.description);
|
|
436
|
+
}
|
|
437
|
+
if (schema.default !== undefined) {
|
|
438
|
+
zodSchema = zodSchema.default(schema.default);
|
|
439
|
+
}
|
|
440
|
+
return zodSchema;
|
|
441
|
+
}
|
|
442
|
+
function convertSchema(schema, ctx) {
|
|
443
|
+
if (typeof schema === "boolean") {
|
|
444
|
+
return schema ? z.any() : z.never();
|
|
445
|
+
}
|
|
446
|
+
// Convert base schema first (ignoring composition keywords)
|
|
447
|
+
let baseSchema = convertBaseSchema(schema, ctx);
|
|
448
|
+
const hasExplicitType = schema.type || schema.enum !== undefined || schema.const !== undefined;
|
|
449
|
+
// Process composition keywords LAST (they can appear together)
|
|
450
|
+
// Handle anyOf - wrap base schema with union
|
|
451
|
+
if (schema.anyOf && Array.isArray(schema.anyOf)) {
|
|
452
|
+
const options = schema.anyOf.map((s) => convertSchema(s, ctx));
|
|
453
|
+
const anyOfUnion = z.union(options);
|
|
454
|
+
baseSchema = hasExplicitType ? z.intersection(baseSchema, anyOfUnion) : anyOfUnion;
|
|
455
|
+
}
|
|
456
|
+
// Handle oneOf - exclusive union (exactly one must match)
|
|
457
|
+
if (schema.oneOf && Array.isArray(schema.oneOf)) {
|
|
458
|
+
const options = schema.oneOf.map((s) => convertSchema(s, ctx));
|
|
459
|
+
const oneOfUnion = z.xor(options);
|
|
460
|
+
baseSchema = hasExplicitType ? z.intersection(baseSchema, oneOfUnion) : oneOfUnion;
|
|
461
|
+
}
|
|
462
|
+
// Handle allOf - wrap base schema with intersection
|
|
463
|
+
if (schema.allOf && Array.isArray(schema.allOf)) {
|
|
464
|
+
if (schema.allOf.length === 0) {
|
|
465
|
+
baseSchema = hasExplicitType ? baseSchema : z.any();
|
|
466
|
+
}
|
|
467
|
+
else {
|
|
468
|
+
let result = hasExplicitType ? baseSchema : convertSchema(schema.allOf[0], ctx);
|
|
469
|
+
const startIdx = hasExplicitType ? 0 : 1;
|
|
470
|
+
for (let i = startIdx; i < schema.allOf.length; i++) {
|
|
471
|
+
result = z.intersection(result, convertSchema(schema.allOf[i], ctx));
|
|
472
|
+
}
|
|
473
|
+
baseSchema = result;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
// Handle nullable (OpenAPI 3.0)
|
|
477
|
+
if (schema.nullable === true && ctx.version === "openapi-3.0") {
|
|
478
|
+
baseSchema = z.nullable(baseSchema);
|
|
479
|
+
}
|
|
480
|
+
// Handle readOnly
|
|
481
|
+
if (schema.readOnly === true) {
|
|
482
|
+
baseSchema = z.readonly(baseSchema);
|
|
483
|
+
}
|
|
484
|
+
return baseSchema;
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Converts a JSON Schema to a Zod schema. This function should be considered semi-experimental. It's behavior is liable to change. */
|
|
488
|
+
function fromJSONSchema(schema, params) {
|
|
489
|
+
// Handle boolean schemas
|
|
490
|
+
if (typeof schema === "boolean") {
|
|
491
|
+
return schema ? z.any() : z.never();
|
|
492
|
+
}
|
|
493
|
+
const version = detectVersion(schema, params?.defaultTarget);
|
|
494
|
+
const defs = (schema.$defs || schema.definitions || {});
|
|
495
|
+
const ctx = {
|
|
496
|
+
version,
|
|
497
|
+
defs,
|
|
498
|
+
refs: new Map(),
|
|
499
|
+
processing: new Set(),
|
|
500
|
+
rootSchema: schema,
|
|
501
|
+
};
|
|
502
|
+
return convertSchema(schema, ctx);
|
|
503
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type * as JSONSchema from "../core/json-schema.cjs";
|
|
2
|
+
import type { ZodType } from "./schemas.cjs";
|
|
3
|
+
type JSONSchemaVersion = "draft-2020-12" | "draft-7" | "draft-4" | "openapi-3.0";
|
|
4
|
+
interface FromJSONSchemaParams {
|
|
5
|
+
defaultTarget?: JSONSchemaVersion;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Converts a JSON Schema to a Zod schema. This function should be considered semi-experimental. It's behavior is liable to change. */
|
|
9
|
+
export declare function fromJSONSchema(schema: JSONSchema.JSONSchema | boolean, params?: FromJSONSchemaParams): ZodType;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type * as JSONSchema from "../core/json-schema.js";
|
|
2
|
+
import type { ZodType } from "./schemas.js";
|
|
3
|
+
type JSONSchemaVersion = "draft-2020-12" | "draft-7" | "draft-4" | "openapi-3.0";
|
|
4
|
+
interface FromJSONSchemaParams {
|
|
5
|
+
defaultTarget?: JSONSchemaVersion;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Converts a JSON Schema to a Zod schema. This function should be considered semi-experimental. It's behavior is liable to change. */
|
|
9
|
+
export declare function fromJSONSchema(schema: JSONSchema.JSONSchema | boolean, params?: FromJSONSchemaParams): ZodType;
|
|
10
|
+
export {};
|