zod 4.2.0-canary.20251202T062120 → 4.2.0-canary.20251213T203150
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/schemas.ts +97 -5
- package/src/v4/classic/tests/fix-json-issue.test.ts +26 -0
- package/src/v4/classic/tests/json.test.ts +4 -3
- package/src/v4/classic/tests/standard-schema.test.ts +77 -0
- package/src/v4/classic/tests/to-json-schema-methods.test.ts +438 -0
- package/src/v4/classic/tests/to-json-schema.test.ts +66 -30
- package/src/v4/core/index.ts +2 -0
- package/src/v4/core/json-schema-generator.ts +124 -0
- package/src/v4/core/json-schema-processors.ts +630 -0
- package/src/v4/core/schemas.ts +8 -13
- package/src/v4/core/standard-schema.ts +114 -19
- package/src/v4/core/to-json-schema.ts +373 -827
- package/src/v4/mini/schemas.ts +2 -2
- package/src/v4/mini/tests/standard-schema.test.ts +17 -0
- package/v4/classic/schemas.cjs +48 -0
- package/v4/classic/schemas.d.cts +35 -0
- package/v4/classic/schemas.d.ts +35 -0
- package/v4/classic/schemas.js +48 -0
- package/v4/core/index.cjs +5 -1
- package/v4/core/index.d.cts +2 -0
- package/v4/core/index.d.ts +2 -0
- package/v4/core/index.js +2 -0
- package/v4/core/json-schema-generator.cjs +99 -0
- package/v4/core/json-schema-generator.d.cts +64 -0
- package/v4/core/json-schema-generator.d.ts +64 -0
- package/v4/core/json-schema-generator.js +95 -0
- package/v4/core/json-schema-processors.cjs +617 -0
- package/v4/core/json-schema-processors.d.cts +49 -0
- package/v4/core/json-schema-processors.d.ts +49 -0
- package/v4/core/json-schema-processors.js +574 -0
- package/v4/core/schemas.cjs +0 -10
- package/v4/core/schemas.d.cts +4 -1
- package/v4/core/schemas.d.ts +4 -1
- package/v4/core/schemas.js +0 -10
- package/v4/core/standard-schema.d.cts +90 -19
- package/v4/core/standard-schema.d.ts +90 -19
- package/v4/core/to-json-schema.cjs +302 -793
- package/v4/core/to-json-schema.d.cts +56 -33
- package/v4/core/to-json-schema.d.ts +56 -33
- package/v4/core/to-json-schema.js +296 -791
- package/v4/mini/schemas.d.cts +2 -2
- package/v4/mini/schemas.d.ts +2 -2
|
@@ -0,0 +1,617 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.allProcessors = exports.lazyProcessor = exports.optionalProcessor = exports.promiseProcessor = exports.readonlyProcessor = exports.pipeProcessor = exports.catchProcessor = exports.prefaultProcessor = exports.defaultProcessor = exports.nonoptionalProcessor = exports.nullableProcessor = exports.recordProcessor = exports.tupleProcessor = exports.intersectionProcessor = exports.unionProcessor = exports.objectProcessor = exports.arrayProcessor = exports.setProcessor = exports.mapProcessor = exports.transformProcessor = exports.functionProcessor = exports.customProcessor = exports.successProcessor = exports.fileProcessor = exports.templateLiteralProcessor = exports.nanProcessor = exports.literalProcessor = exports.enumProcessor = exports.dateProcessor = exports.unknownProcessor = exports.anyProcessor = exports.neverProcessor = exports.voidProcessor = exports.undefinedProcessor = exports.nullProcessor = exports.symbolProcessor = exports.bigintProcessor = exports.booleanProcessor = exports.numberProcessor = exports.stringProcessor = void 0;
|
|
4
|
+
exports.toJSONSchema = toJSONSchema;
|
|
5
|
+
const to_json_schema_js_1 = require("./to-json-schema.cjs");
|
|
6
|
+
const util_js_1 = require("./util.cjs");
|
|
7
|
+
const formatMap = {
|
|
8
|
+
guid: "uuid",
|
|
9
|
+
url: "uri",
|
|
10
|
+
datetime: "date-time",
|
|
11
|
+
json_string: "json-string",
|
|
12
|
+
regex: "", // do not set
|
|
13
|
+
};
|
|
14
|
+
// ==================== SIMPLE TYPE PROCESSORS ====================
|
|
15
|
+
const stringProcessor = (schema, ctx, _json, _params) => {
|
|
16
|
+
const json = _json;
|
|
17
|
+
json.type = "string";
|
|
18
|
+
const { minimum, maximum, format, patterns, contentEncoding } = schema._zod
|
|
19
|
+
.bag;
|
|
20
|
+
if (typeof minimum === "number")
|
|
21
|
+
json.minLength = minimum;
|
|
22
|
+
if (typeof maximum === "number")
|
|
23
|
+
json.maxLength = maximum;
|
|
24
|
+
// custom pattern overrides format
|
|
25
|
+
if (format) {
|
|
26
|
+
json.format = formatMap[format] ?? format;
|
|
27
|
+
if (json.format === "")
|
|
28
|
+
delete json.format; // empty format is not valid
|
|
29
|
+
}
|
|
30
|
+
if (contentEncoding)
|
|
31
|
+
json.contentEncoding = contentEncoding;
|
|
32
|
+
if (patterns && patterns.size > 0) {
|
|
33
|
+
const regexes = [...patterns];
|
|
34
|
+
if (regexes.length === 1)
|
|
35
|
+
json.pattern = regexes[0].source;
|
|
36
|
+
else if (regexes.length > 1) {
|
|
37
|
+
json.allOf = [
|
|
38
|
+
...regexes.map((regex) => ({
|
|
39
|
+
...(ctx.target === "draft-07" || ctx.target === "draft-04" || ctx.target === "openapi-3.0"
|
|
40
|
+
? { type: "string" }
|
|
41
|
+
: {}),
|
|
42
|
+
pattern: regex.source,
|
|
43
|
+
})),
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
exports.stringProcessor = stringProcessor;
|
|
49
|
+
const numberProcessor = (schema, ctx, _json, _params) => {
|
|
50
|
+
const json = _json;
|
|
51
|
+
const { minimum, maximum, format, multipleOf, exclusiveMaximum, exclusiveMinimum } = schema._zod.bag;
|
|
52
|
+
if (typeof format === "string" && format.includes("int"))
|
|
53
|
+
json.type = "integer";
|
|
54
|
+
else
|
|
55
|
+
json.type = "number";
|
|
56
|
+
if (typeof exclusiveMinimum === "number") {
|
|
57
|
+
if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
|
|
58
|
+
json.minimum = exclusiveMinimum;
|
|
59
|
+
json.exclusiveMinimum = true;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
json.exclusiveMinimum = exclusiveMinimum;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (typeof minimum === "number") {
|
|
66
|
+
json.minimum = minimum;
|
|
67
|
+
if (typeof exclusiveMinimum === "number" && ctx.target !== "draft-04") {
|
|
68
|
+
if (exclusiveMinimum >= minimum)
|
|
69
|
+
delete json.minimum;
|
|
70
|
+
else
|
|
71
|
+
delete json.exclusiveMinimum;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (typeof exclusiveMaximum === "number") {
|
|
75
|
+
if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
|
|
76
|
+
json.maximum = exclusiveMaximum;
|
|
77
|
+
json.exclusiveMaximum = true;
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
json.exclusiveMaximum = exclusiveMaximum;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (typeof maximum === "number") {
|
|
84
|
+
json.maximum = maximum;
|
|
85
|
+
if (typeof exclusiveMaximum === "number" && ctx.target !== "draft-04") {
|
|
86
|
+
if (exclusiveMaximum <= maximum)
|
|
87
|
+
delete json.maximum;
|
|
88
|
+
else
|
|
89
|
+
delete json.exclusiveMaximum;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (typeof multipleOf === "number")
|
|
93
|
+
json.multipleOf = multipleOf;
|
|
94
|
+
};
|
|
95
|
+
exports.numberProcessor = numberProcessor;
|
|
96
|
+
const booleanProcessor = (_schema, _ctx, json, _params) => {
|
|
97
|
+
json.type = "boolean";
|
|
98
|
+
};
|
|
99
|
+
exports.booleanProcessor = booleanProcessor;
|
|
100
|
+
const bigintProcessor = (_schema, ctx, _json, _params) => {
|
|
101
|
+
if (ctx.unrepresentable === "throw") {
|
|
102
|
+
throw new Error("BigInt cannot be represented in JSON Schema");
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
exports.bigintProcessor = bigintProcessor;
|
|
106
|
+
const symbolProcessor = (_schema, ctx, _json, _params) => {
|
|
107
|
+
if (ctx.unrepresentable === "throw") {
|
|
108
|
+
throw new Error("Symbols cannot be represented in JSON Schema");
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
exports.symbolProcessor = symbolProcessor;
|
|
112
|
+
const nullProcessor = (_schema, ctx, json, _params) => {
|
|
113
|
+
if (ctx.target === "openapi-3.0") {
|
|
114
|
+
json.type = "string";
|
|
115
|
+
json.nullable = true;
|
|
116
|
+
json.enum = [null];
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
json.type = "null";
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
exports.nullProcessor = nullProcessor;
|
|
123
|
+
const undefinedProcessor = (_schema, ctx, _json, _params) => {
|
|
124
|
+
if (ctx.unrepresentable === "throw") {
|
|
125
|
+
throw new Error("Undefined cannot be represented in JSON Schema");
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
exports.undefinedProcessor = undefinedProcessor;
|
|
129
|
+
const voidProcessor = (_schema, ctx, _json, _params) => {
|
|
130
|
+
if (ctx.unrepresentable === "throw") {
|
|
131
|
+
throw new Error("Void cannot be represented in JSON Schema");
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
exports.voidProcessor = voidProcessor;
|
|
135
|
+
const neverProcessor = (_schema, _ctx, json, _params) => {
|
|
136
|
+
json.not = {};
|
|
137
|
+
};
|
|
138
|
+
exports.neverProcessor = neverProcessor;
|
|
139
|
+
const anyProcessor = (_schema, _ctx, _json, _params) => {
|
|
140
|
+
// empty schema accepts anything
|
|
141
|
+
};
|
|
142
|
+
exports.anyProcessor = anyProcessor;
|
|
143
|
+
const unknownProcessor = (_schema, _ctx, _json, _params) => {
|
|
144
|
+
// empty schema accepts anything
|
|
145
|
+
};
|
|
146
|
+
exports.unknownProcessor = unknownProcessor;
|
|
147
|
+
const dateProcessor = (_schema, ctx, _json, _params) => {
|
|
148
|
+
if (ctx.unrepresentable === "throw") {
|
|
149
|
+
throw new Error("Date cannot be represented in JSON Schema");
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
exports.dateProcessor = dateProcessor;
|
|
153
|
+
const enumProcessor = (schema, _ctx, json, _params) => {
|
|
154
|
+
const def = schema._zod.def;
|
|
155
|
+
const values = (0, util_js_1.getEnumValues)(def.entries);
|
|
156
|
+
// Number enums can have both string and number values
|
|
157
|
+
if (values.every((v) => typeof v === "number"))
|
|
158
|
+
json.type = "number";
|
|
159
|
+
if (values.every((v) => typeof v === "string"))
|
|
160
|
+
json.type = "string";
|
|
161
|
+
json.enum = values;
|
|
162
|
+
};
|
|
163
|
+
exports.enumProcessor = enumProcessor;
|
|
164
|
+
const literalProcessor = (schema, ctx, json, _params) => {
|
|
165
|
+
const def = schema._zod.def;
|
|
166
|
+
const vals = [];
|
|
167
|
+
for (const val of def.values) {
|
|
168
|
+
if (val === undefined) {
|
|
169
|
+
if (ctx.unrepresentable === "throw") {
|
|
170
|
+
throw new Error("Literal `undefined` cannot be represented in JSON Schema");
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
// do not add to vals
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
else if (typeof val === "bigint") {
|
|
177
|
+
if (ctx.unrepresentable === "throw") {
|
|
178
|
+
throw new Error("BigInt literals cannot be represented in JSON Schema");
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
vals.push(Number(val));
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
vals.push(val);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (vals.length === 0) {
|
|
189
|
+
// do nothing (an undefined literal was stripped)
|
|
190
|
+
}
|
|
191
|
+
else if (vals.length === 1) {
|
|
192
|
+
const val = vals[0];
|
|
193
|
+
json.type = val === null ? "null" : typeof val;
|
|
194
|
+
if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
|
|
195
|
+
json.enum = [val];
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
json.const = val;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
202
|
+
if (vals.every((v) => typeof v === "number"))
|
|
203
|
+
json.type = "number";
|
|
204
|
+
if (vals.every((v) => typeof v === "string"))
|
|
205
|
+
json.type = "string";
|
|
206
|
+
if (vals.every((v) => typeof v === "boolean"))
|
|
207
|
+
json.type = "boolean";
|
|
208
|
+
if (vals.every((v) => v === null))
|
|
209
|
+
json.type = "null";
|
|
210
|
+
json.enum = vals;
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
exports.literalProcessor = literalProcessor;
|
|
214
|
+
const nanProcessor = (_schema, ctx, _json, _params) => {
|
|
215
|
+
if (ctx.unrepresentable === "throw") {
|
|
216
|
+
throw new Error("NaN cannot be represented in JSON Schema");
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
exports.nanProcessor = nanProcessor;
|
|
220
|
+
const templateLiteralProcessor = (schema, _ctx, json, _params) => {
|
|
221
|
+
const _json = json;
|
|
222
|
+
const pattern = schema._zod.pattern;
|
|
223
|
+
if (!pattern)
|
|
224
|
+
throw new Error("Pattern not found in template literal");
|
|
225
|
+
_json.type = "string";
|
|
226
|
+
_json.pattern = pattern.source;
|
|
227
|
+
};
|
|
228
|
+
exports.templateLiteralProcessor = templateLiteralProcessor;
|
|
229
|
+
const fileProcessor = (schema, _ctx, json, _params) => {
|
|
230
|
+
const _json = json;
|
|
231
|
+
const file = {
|
|
232
|
+
type: "string",
|
|
233
|
+
format: "binary",
|
|
234
|
+
contentEncoding: "binary",
|
|
235
|
+
};
|
|
236
|
+
const { minimum, maximum, mime } = schema._zod.bag;
|
|
237
|
+
if (minimum !== undefined)
|
|
238
|
+
file.minLength = minimum;
|
|
239
|
+
if (maximum !== undefined)
|
|
240
|
+
file.maxLength = maximum;
|
|
241
|
+
if (mime) {
|
|
242
|
+
if (mime.length === 1) {
|
|
243
|
+
file.contentMediaType = mime[0];
|
|
244
|
+
Object.assign(_json, file);
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
_json.anyOf = mime.map((m) => {
|
|
248
|
+
const mFile = { ...file, contentMediaType: m };
|
|
249
|
+
return mFile;
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
Object.assign(_json, file);
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
exports.fileProcessor = fileProcessor;
|
|
258
|
+
const successProcessor = (_schema, _ctx, json, _params) => {
|
|
259
|
+
json.type = "boolean";
|
|
260
|
+
};
|
|
261
|
+
exports.successProcessor = successProcessor;
|
|
262
|
+
const customProcessor = (_schema, ctx, _json, _params) => {
|
|
263
|
+
if (ctx.unrepresentable === "throw") {
|
|
264
|
+
throw new Error("Custom types cannot be represented in JSON Schema");
|
|
265
|
+
}
|
|
266
|
+
};
|
|
267
|
+
exports.customProcessor = customProcessor;
|
|
268
|
+
const functionProcessor = (_schema, ctx, _json, _params) => {
|
|
269
|
+
if (ctx.unrepresentable === "throw") {
|
|
270
|
+
throw new Error("Function types cannot be represented in JSON Schema");
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
exports.functionProcessor = functionProcessor;
|
|
274
|
+
const transformProcessor = (_schema, ctx, _json, _params) => {
|
|
275
|
+
if (ctx.unrepresentable === "throw") {
|
|
276
|
+
throw new Error("Transforms cannot be represented in JSON Schema");
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
exports.transformProcessor = transformProcessor;
|
|
280
|
+
const mapProcessor = (_schema, ctx, _json, _params) => {
|
|
281
|
+
if (ctx.unrepresentable === "throw") {
|
|
282
|
+
throw new Error("Map cannot be represented in JSON Schema");
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
exports.mapProcessor = mapProcessor;
|
|
286
|
+
const setProcessor = (_schema, ctx, _json, _params) => {
|
|
287
|
+
if (ctx.unrepresentable === "throw") {
|
|
288
|
+
throw new Error("Set cannot be represented in JSON Schema");
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
exports.setProcessor = setProcessor;
|
|
292
|
+
// ==================== COMPOSITE TYPE PROCESSORS ====================
|
|
293
|
+
const arrayProcessor = (schema, ctx, _json, params) => {
|
|
294
|
+
const json = _json;
|
|
295
|
+
const def = schema._zod.def;
|
|
296
|
+
const { minimum, maximum } = schema._zod.bag;
|
|
297
|
+
if (typeof minimum === "number")
|
|
298
|
+
json.minItems = minimum;
|
|
299
|
+
if (typeof maximum === "number")
|
|
300
|
+
json.maxItems = maximum;
|
|
301
|
+
json.type = "array";
|
|
302
|
+
json.items = (0, to_json_schema_js_1.process)(def.element, ctx, { ...params, path: [...params.path, "items"] });
|
|
303
|
+
};
|
|
304
|
+
exports.arrayProcessor = arrayProcessor;
|
|
305
|
+
const objectProcessor = (schema, ctx, _json, params) => {
|
|
306
|
+
const json = _json;
|
|
307
|
+
const def = schema._zod.def;
|
|
308
|
+
json.type = "object";
|
|
309
|
+
json.properties = {};
|
|
310
|
+
const shape = def.shape;
|
|
311
|
+
for (const key in shape) {
|
|
312
|
+
json.properties[key] = (0, to_json_schema_js_1.process)(shape[key], ctx, {
|
|
313
|
+
...params,
|
|
314
|
+
path: [...params.path, "properties", key],
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
// required keys
|
|
318
|
+
const allKeys = new Set(Object.keys(shape));
|
|
319
|
+
const requiredKeys = new Set([...allKeys].filter((key) => {
|
|
320
|
+
const v = def.shape[key]._zod;
|
|
321
|
+
if (ctx.io === "input") {
|
|
322
|
+
return v.optin === undefined;
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
return v.optout === undefined;
|
|
326
|
+
}
|
|
327
|
+
}));
|
|
328
|
+
if (requiredKeys.size > 0) {
|
|
329
|
+
json.required = Array.from(requiredKeys);
|
|
330
|
+
}
|
|
331
|
+
// catchall
|
|
332
|
+
if (def.catchall?._zod.def.type === "never") {
|
|
333
|
+
// strict
|
|
334
|
+
json.additionalProperties = false;
|
|
335
|
+
}
|
|
336
|
+
else if (!def.catchall) {
|
|
337
|
+
// regular
|
|
338
|
+
if (ctx.io === "output")
|
|
339
|
+
json.additionalProperties = false;
|
|
340
|
+
}
|
|
341
|
+
else if (def.catchall) {
|
|
342
|
+
json.additionalProperties = (0, to_json_schema_js_1.process)(def.catchall, ctx, {
|
|
343
|
+
...params,
|
|
344
|
+
path: [...params.path, "additionalProperties"],
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
exports.objectProcessor = objectProcessor;
|
|
349
|
+
const unionProcessor = (schema, ctx, json, params) => {
|
|
350
|
+
const def = schema._zod.def;
|
|
351
|
+
// Discriminated unions use oneOf (exactly one match) instead of anyOf (one or more matches)
|
|
352
|
+
// because the discriminator field ensures mutual exclusivity between options in JSON Schema
|
|
353
|
+
const isDiscriminated = def.discriminator !== undefined;
|
|
354
|
+
const options = def.options.map((x, i) => (0, to_json_schema_js_1.process)(x, ctx, {
|
|
355
|
+
...params,
|
|
356
|
+
path: [...params.path, isDiscriminated ? "oneOf" : "anyOf", i],
|
|
357
|
+
}));
|
|
358
|
+
if (isDiscriminated) {
|
|
359
|
+
json.oneOf = options;
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
json.anyOf = options;
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
exports.unionProcessor = unionProcessor;
|
|
366
|
+
const intersectionProcessor = (schema, ctx, json, params) => {
|
|
367
|
+
const def = schema._zod.def;
|
|
368
|
+
const a = (0, to_json_schema_js_1.process)(def.left, ctx, {
|
|
369
|
+
...params,
|
|
370
|
+
path: [...params.path, "allOf", 0],
|
|
371
|
+
});
|
|
372
|
+
const b = (0, to_json_schema_js_1.process)(def.right, ctx, {
|
|
373
|
+
...params,
|
|
374
|
+
path: [...params.path, "allOf", 1],
|
|
375
|
+
});
|
|
376
|
+
const isSimpleIntersection = (val) => "allOf" in val && Object.keys(val).length === 1;
|
|
377
|
+
const allOf = [
|
|
378
|
+
...(isSimpleIntersection(a) ? a.allOf : [a]),
|
|
379
|
+
...(isSimpleIntersection(b) ? b.allOf : [b]),
|
|
380
|
+
];
|
|
381
|
+
json.allOf = allOf;
|
|
382
|
+
};
|
|
383
|
+
exports.intersectionProcessor = intersectionProcessor;
|
|
384
|
+
const tupleProcessor = (schema, ctx, _json, params) => {
|
|
385
|
+
const json = _json;
|
|
386
|
+
const def = schema._zod.def;
|
|
387
|
+
json.type = "array";
|
|
388
|
+
const prefixPath = ctx.target === "draft-2020-12" ? "prefixItems" : "items";
|
|
389
|
+
const restPath = ctx.target === "draft-2020-12" ? "items" : ctx.target === "openapi-3.0" ? "items" : "additionalItems";
|
|
390
|
+
const prefixItems = def.items.map((x, i) => (0, to_json_schema_js_1.process)(x, ctx, {
|
|
391
|
+
...params,
|
|
392
|
+
path: [...params.path, prefixPath, i],
|
|
393
|
+
}));
|
|
394
|
+
const rest = def.rest
|
|
395
|
+
? (0, to_json_schema_js_1.process)(def.rest, ctx, {
|
|
396
|
+
...params,
|
|
397
|
+
path: [...params.path, restPath, ...(ctx.target === "openapi-3.0" ? [def.items.length] : [])],
|
|
398
|
+
})
|
|
399
|
+
: null;
|
|
400
|
+
if (ctx.target === "draft-2020-12") {
|
|
401
|
+
json.prefixItems = prefixItems;
|
|
402
|
+
if (rest) {
|
|
403
|
+
json.items = rest;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
else if (ctx.target === "openapi-3.0") {
|
|
407
|
+
json.items = {
|
|
408
|
+
anyOf: prefixItems,
|
|
409
|
+
};
|
|
410
|
+
if (rest) {
|
|
411
|
+
json.items.anyOf.push(rest);
|
|
412
|
+
}
|
|
413
|
+
json.minItems = prefixItems.length;
|
|
414
|
+
if (!rest) {
|
|
415
|
+
json.maxItems = prefixItems.length;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
else {
|
|
419
|
+
json.items = prefixItems;
|
|
420
|
+
if (rest) {
|
|
421
|
+
json.additionalItems = rest;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
// length
|
|
425
|
+
const { minimum, maximum } = schema._zod.bag;
|
|
426
|
+
if (typeof minimum === "number")
|
|
427
|
+
json.minItems = minimum;
|
|
428
|
+
if (typeof maximum === "number")
|
|
429
|
+
json.maxItems = maximum;
|
|
430
|
+
};
|
|
431
|
+
exports.tupleProcessor = tupleProcessor;
|
|
432
|
+
const recordProcessor = (schema, ctx, _json, params) => {
|
|
433
|
+
const json = _json;
|
|
434
|
+
const def = schema._zod.def;
|
|
435
|
+
json.type = "object";
|
|
436
|
+
if (ctx.target === "draft-07" || ctx.target === "draft-2020-12") {
|
|
437
|
+
json.propertyNames = (0, to_json_schema_js_1.process)(def.keyType, ctx, {
|
|
438
|
+
...params,
|
|
439
|
+
path: [...params.path, "propertyNames"],
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
json.additionalProperties = (0, to_json_schema_js_1.process)(def.valueType, ctx, {
|
|
443
|
+
...params,
|
|
444
|
+
path: [...params.path, "additionalProperties"],
|
|
445
|
+
});
|
|
446
|
+
};
|
|
447
|
+
exports.recordProcessor = recordProcessor;
|
|
448
|
+
const nullableProcessor = (schema, ctx, json, params) => {
|
|
449
|
+
const def = schema._zod.def;
|
|
450
|
+
const inner = (0, to_json_schema_js_1.process)(def.innerType, ctx, params);
|
|
451
|
+
const seen = ctx.seen.get(schema);
|
|
452
|
+
if (ctx.target === "openapi-3.0") {
|
|
453
|
+
seen.ref = def.innerType;
|
|
454
|
+
json.nullable = true;
|
|
455
|
+
}
|
|
456
|
+
else {
|
|
457
|
+
json.anyOf = [inner, { type: "null" }];
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
exports.nullableProcessor = nullableProcessor;
|
|
461
|
+
const nonoptionalProcessor = (schema, ctx, _json, params) => {
|
|
462
|
+
const def = schema._zod.def;
|
|
463
|
+
(0, to_json_schema_js_1.process)(def.innerType, ctx, params);
|
|
464
|
+
const seen = ctx.seen.get(schema);
|
|
465
|
+
seen.ref = def.innerType;
|
|
466
|
+
};
|
|
467
|
+
exports.nonoptionalProcessor = nonoptionalProcessor;
|
|
468
|
+
const defaultProcessor = (schema, ctx, json, params) => {
|
|
469
|
+
const def = schema._zod.def;
|
|
470
|
+
(0, to_json_schema_js_1.process)(def.innerType, ctx, params);
|
|
471
|
+
const seen = ctx.seen.get(schema);
|
|
472
|
+
seen.ref = def.innerType;
|
|
473
|
+
json.default = JSON.parse(JSON.stringify(def.defaultValue));
|
|
474
|
+
};
|
|
475
|
+
exports.defaultProcessor = defaultProcessor;
|
|
476
|
+
const prefaultProcessor = (schema, ctx, json, params) => {
|
|
477
|
+
const def = schema._zod.def;
|
|
478
|
+
(0, to_json_schema_js_1.process)(def.innerType, ctx, params);
|
|
479
|
+
const seen = ctx.seen.get(schema);
|
|
480
|
+
seen.ref = def.innerType;
|
|
481
|
+
if (ctx.io === "input")
|
|
482
|
+
json._prefault = JSON.parse(JSON.stringify(def.defaultValue));
|
|
483
|
+
};
|
|
484
|
+
exports.prefaultProcessor = prefaultProcessor;
|
|
485
|
+
const catchProcessor = (schema, ctx, json, params) => {
|
|
486
|
+
const def = schema._zod.def;
|
|
487
|
+
(0, to_json_schema_js_1.process)(def.innerType, ctx, params);
|
|
488
|
+
const seen = ctx.seen.get(schema);
|
|
489
|
+
seen.ref = def.innerType;
|
|
490
|
+
let catchValue;
|
|
491
|
+
try {
|
|
492
|
+
catchValue = def.catchValue(undefined);
|
|
493
|
+
}
|
|
494
|
+
catch {
|
|
495
|
+
throw new Error("Dynamic catch values are not supported in JSON Schema");
|
|
496
|
+
}
|
|
497
|
+
json.default = catchValue;
|
|
498
|
+
};
|
|
499
|
+
exports.catchProcessor = catchProcessor;
|
|
500
|
+
const pipeProcessor = (schema, ctx, _json, params) => {
|
|
501
|
+
const def = schema._zod.def;
|
|
502
|
+
const innerType = ctx.io === "input" ? (def.in._zod.def.type === "transform" ? def.out : def.in) : def.out;
|
|
503
|
+
(0, to_json_schema_js_1.process)(innerType, ctx, params);
|
|
504
|
+
const seen = ctx.seen.get(schema);
|
|
505
|
+
seen.ref = innerType;
|
|
506
|
+
};
|
|
507
|
+
exports.pipeProcessor = pipeProcessor;
|
|
508
|
+
const readonlyProcessor = (schema, ctx, json, params) => {
|
|
509
|
+
const def = schema._zod.def;
|
|
510
|
+
(0, to_json_schema_js_1.process)(def.innerType, ctx, params);
|
|
511
|
+
const seen = ctx.seen.get(schema);
|
|
512
|
+
seen.ref = def.innerType;
|
|
513
|
+
json.readOnly = true;
|
|
514
|
+
};
|
|
515
|
+
exports.readonlyProcessor = readonlyProcessor;
|
|
516
|
+
const promiseProcessor = (schema, ctx, _json, params) => {
|
|
517
|
+
const def = schema._zod.def;
|
|
518
|
+
(0, to_json_schema_js_1.process)(def.innerType, ctx, params);
|
|
519
|
+
const seen = ctx.seen.get(schema);
|
|
520
|
+
seen.ref = def.innerType;
|
|
521
|
+
};
|
|
522
|
+
exports.promiseProcessor = promiseProcessor;
|
|
523
|
+
const optionalProcessor = (schema, ctx, _json, params) => {
|
|
524
|
+
const def = schema._zod.def;
|
|
525
|
+
(0, to_json_schema_js_1.process)(def.innerType, ctx, params);
|
|
526
|
+
const seen = ctx.seen.get(schema);
|
|
527
|
+
seen.ref = def.innerType;
|
|
528
|
+
};
|
|
529
|
+
exports.optionalProcessor = optionalProcessor;
|
|
530
|
+
const lazyProcessor = (schema, ctx, _json, params) => {
|
|
531
|
+
const innerType = schema._zod.innerType;
|
|
532
|
+
(0, to_json_schema_js_1.process)(innerType, ctx, params);
|
|
533
|
+
const seen = ctx.seen.get(schema);
|
|
534
|
+
seen.ref = innerType;
|
|
535
|
+
};
|
|
536
|
+
exports.lazyProcessor = lazyProcessor;
|
|
537
|
+
// ==================== ALL PROCESSORS ====================
|
|
538
|
+
exports.allProcessors = {
|
|
539
|
+
string: exports.stringProcessor,
|
|
540
|
+
number: exports.numberProcessor,
|
|
541
|
+
boolean: exports.booleanProcessor,
|
|
542
|
+
bigint: exports.bigintProcessor,
|
|
543
|
+
symbol: exports.symbolProcessor,
|
|
544
|
+
null: exports.nullProcessor,
|
|
545
|
+
undefined: exports.undefinedProcessor,
|
|
546
|
+
void: exports.voidProcessor,
|
|
547
|
+
never: exports.neverProcessor,
|
|
548
|
+
any: exports.anyProcessor,
|
|
549
|
+
unknown: exports.unknownProcessor,
|
|
550
|
+
date: exports.dateProcessor,
|
|
551
|
+
enum: exports.enumProcessor,
|
|
552
|
+
literal: exports.literalProcessor,
|
|
553
|
+
nan: exports.nanProcessor,
|
|
554
|
+
template_literal: exports.templateLiteralProcessor,
|
|
555
|
+
file: exports.fileProcessor,
|
|
556
|
+
success: exports.successProcessor,
|
|
557
|
+
custom: exports.customProcessor,
|
|
558
|
+
function: exports.functionProcessor,
|
|
559
|
+
transform: exports.transformProcessor,
|
|
560
|
+
map: exports.mapProcessor,
|
|
561
|
+
set: exports.setProcessor,
|
|
562
|
+
array: exports.arrayProcessor,
|
|
563
|
+
object: exports.objectProcessor,
|
|
564
|
+
union: exports.unionProcessor,
|
|
565
|
+
intersection: exports.intersectionProcessor,
|
|
566
|
+
tuple: exports.tupleProcessor,
|
|
567
|
+
record: exports.recordProcessor,
|
|
568
|
+
nullable: exports.nullableProcessor,
|
|
569
|
+
nonoptional: exports.nonoptionalProcessor,
|
|
570
|
+
default: exports.defaultProcessor,
|
|
571
|
+
prefault: exports.prefaultProcessor,
|
|
572
|
+
catch: exports.catchProcessor,
|
|
573
|
+
pipe: exports.pipeProcessor,
|
|
574
|
+
readonly: exports.readonlyProcessor,
|
|
575
|
+
promise: exports.promiseProcessor,
|
|
576
|
+
optional: exports.optionalProcessor,
|
|
577
|
+
lazy: exports.lazyProcessor,
|
|
578
|
+
};
|
|
579
|
+
function toJSONSchema(input, params) {
|
|
580
|
+
if ("_idmap" in input) {
|
|
581
|
+
// Registry case
|
|
582
|
+
const registry = input;
|
|
583
|
+
const ctx = (0, to_json_schema_js_1.initializeContext)({ ...params, processors: exports.allProcessors });
|
|
584
|
+
const defs = {};
|
|
585
|
+
// First pass: process all schemas to build the seen map
|
|
586
|
+
for (const entry of registry._idmap.entries()) {
|
|
587
|
+
const [_, schema] = entry;
|
|
588
|
+
(0, to_json_schema_js_1.process)(schema, ctx);
|
|
589
|
+
}
|
|
590
|
+
const schemas = {};
|
|
591
|
+
const external = {
|
|
592
|
+
registry,
|
|
593
|
+
uri: params?.uri,
|
|
594
|
+
defs,
|
|
595
|
+
};
|
|
596
|
+
// Update the context with external configuration
|
|
597
|
+
ctx.external = external;
|
|
598
|
+
// Second pass: emit each schema
|
|
599
|
+
for (const entry of registry._idmap.entries()) {
|
|
600
|
+
const [key, schema] = entry;
|
|
601
|
+
(0, to_json_schema_js_1.extractDefs)(ctx, schema);
|
|
602
|
+
schemas[key] = (0, to_json_schema_js_1.finalize)(ctx, schema);
|
|
603
|
+
}
|
|
604
|
+
if (Object.keys(defs).length > 0) {
|
|
605
|
+
const defsSegment = ctx.target === "draft-2020-12" ? "$defs" : "definitions";
|
|
606
|
+
schemas.__shared = {
|
|
607
|
+
[defsSegment]: defs,
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
return { schemas };
|
|
611
|
+
}
|
|
612
|
+
// Single schema case
|
|
613
|
+
const ctx = (0, to_json_schema_js_1.initializeContext)({ ...params, processors: exports.allProcessors });
|
|
614
|
+
(0, to_json_schema_js_1.process)(input, ctx);
|
|
615
|
+
(0, to_json_schema_js_1.extractDefs)(ctx, input);
|
|
616
|
+
return (0, to_json_schema_js_1.finalize)(ctx, input);
|
|
617
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { $ZodRegistry } from "./registries.cjs";
|
|
2
|
+
import type * as schemas from "./schemas.cjs";
|
|
3
|
+
import { type Processor, type RegistryToJSONSchemaParams, type ToJSONSchemaParams, type ZodStandardJSONSchemaPayload } from "./to-json-schema.cjs";
|
|
4
|
+
export declare const stringProcessor: Processor<schemas.$ZodString>;
|
|
5
|
+
export declare const numberProcessor: Processor<schemas.$ZodNumber>;
|
|
6
|
+
export declare const booleanProcessor: Processor<schemas.$ZodBoolean>;
|
|
7
|
+
export declare const bigintProcessor: Processor<schemas.$ZodBigInt>;
|
|
8
|
+
export declare const symbolProcessor: Processor<schemas.$ZodSymbol>;
|
|
9
|
+
export declare const nullProcessor: Processor<schemas.$ZodNull>;
|
|
10
|
+
export declare const undefinedProcessor: Processor<schemas.$ZodUndefined>;
|
|
11
|
+
export declare const voidProcessor: Processor<schemas.$ZodVoid>;
|
|
12
|
+
export declare const neverProcessor: Processor<schemas.$ZodNever>;
|
|
13
|
+
export declare const anyProcessor: Processor<schemas.$ZodAny>;
|
|
14
|
+
export declare const unknownProcessor: Processor<schemas.$ZodUnknown>;
|
|
15
|
+
export declare const dateProcessor: Processor<schemas.$ZodDate>;
|
|
16
|
+
export declare const enumProcessor: Processor<schemas.$ZodEnum>;
|
|
17
|
+
export declare const literalProcessor: Processor<schemas.$ZodLiteral>;
|
|
18
|
+
export declare const nanProcessor: Processor<schemas.$ZodNaN>;
|
|
19
|
+
export declare const templateLiteralProcessor: Processor<schemas.$ZodTemplateLiteral>;
|
|
20
|
+
export declare const fileProcessor: Processor<schemas.$ZodFile>;
|
|
21
|
+
export declare const successProcessor: Processor<schemas.$ZodSuccess>;
|
|
22
|
+
export declare const customProcessor: Processor<schemas.$ZodCustom>;
|
|
23
|
+
export declare const functionProcessor: Processor<schemas.$ZodFunction>;
|
|
24
|
+
export declare const transformProcessor: Processor<schemas.$ZodTransform>;
|
|
25
|
+
export declare const mapProcessor: Processor<schemas.$ZodMap>;
|
|
26
|
+
export declare const setProcessor: Processor<schemas.$ZodSet>;
|
|
27
|
+
export declare const arrayProcessor: Processor<schemas.$ZodArray>;
|
|
28
|
+
export declare const objectProcessor: Processor<schemas.$ZodObject>;
|
|
29
|
+
export declare const unionProcessor: Processor<schemas.$ZodUnion>;
|
|
30
|
+
export declare const intersectionProcessor: Processor<schemas.$ZodIntersection>;
|
|
31
|
+
export declare const tupleProcessor: Processor<schemas.$ZodTuple>;
|
|
32
|
+
export declare const recordProcessor: Processor<schemas.$ZodRecord>;
|
|
33
|
+
export declare const nullableProcessor: Processor<schemas.$ZodNullable>;
|
|
34
|
+
export declare const nonoptionalProcessor: Processor<schemas.$ZodNonOptional>;
|
|
35
|
+
export declare const defaultProcessor: Processor<schemas.$ZodDefault>;
|
|
36
|
+
export declare const prefaultProcessor: Processor<schemas.$ZodPrefault>;
|
|
37
|
+
export declare const catchProcessor: Processor<schemas.$ZodCatch>;
|
|
38
|
+
export declare const pipeProcessor: Processor<schemas.$ZodPipe>;
|
|
39
|
+
export declare const readonlyProcessor: Processor<schemas.$ZodReadonly>;
|
|
40
|
+
export declare const promiseProcessor: Processor<schemas.$ZodPromise>;
|
|
41
|
+
export declare const optionalProcessor: Processor<schemas.$ZodOptional>;
|
|
42
|
+
export declare const lazyProcessor: Processor<schemas.$ZodLazy>;
|
|
43
|
+
export declare const allProcessors: Record<string, Processor<any>>;
|
|
44
|
+
export declare function toJSONSchema<T extends schemas.$ZodType>(schema: T, params?: ToJSONSchemaParams): ZodStandardJSONSchemaPayload<T>;
|
|
45
|
+
export declare function toJSONSchema(registry: $ZodRegistry<{
|
|
46
|
+
id?: string | undefined;
|
|
47
|
+
}>, params?: RegistryToJSONSchemaParams): {
|
|
48
|
+
schemas: Record<string, ZodStandardJSONSchemaPayload<schemas.$ZodType>>;
|
|
49
|
+
};
|