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
|
@@ -1,828 +1,318 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.
|
|
3
|
+
exports.createStandardJSONSchemaMethod = exports.createToJSONSchemaMethod = void 0;
|
|
4
|
+
exports.initializeContext = initializeContext;
|
|
5
|
+
exports.process = process;
|
|
6
|
+
exports.extractDefs = extractDefs;
|
|
7
|
+
exports.finalize = finalize;
|
|
5
8
|
const registries_js_1 = require("./registries.cjs");
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
// function initializeContext<T extends schemas.$ZodType>(inputs: JSONSchemaGeneratorParams<T>): ToJSONSchemaContext<T> {
|
|
10
|
+
// return {
|
|
11
|
+
// processor: inputs.processor,
|
|
12
|
+
// metadataRegistry: inputs.metadata ?? globalRegistry,
|
|
13
|
+
// target: inputs.target ?? "draft-2020-12",
|
|
14
|
+
// unrepresentable: inputs.unrepresentable ?? "throw",
|
|
15
|
+
// };
|
|
16
|
+
// }
|
|
17
|
+
function initializeContext(params) {
|
|
18
|
+
// Normalize target: convert old non-hyphenated versions to hyphenated versions
|
|
19
|
+
let target = params?.target ?? "draft-2020-12";
|
|
20
|
+
if (target === "draft-4")
|
|
21
|
+
target = "draft-04";
|
|
22
|
+
if (target === "draft-7")
|
|
23
|
+
target = "draft-07";
|
|
24
|
+
return {
|
|
25
|
+
processors: params.processors ?? {},
|
|
26
|
+
metadataRegistry: params?.metadata ?? registries_js_1.globalRegistry,
|
|
27
|
+
target,
|
|
28
|
+
unrepresentable: params?.unrepresentable ?? "throw",
|
|
29
|
+
override: params?.override ?? (() => { }),
|
|
30
|
+
io: params?.io ?? "output",
|
|
31
|
+
counter: 0,
|
|
32
|
+
seen: new Map(),
|
|
33
|
+
cycles: params?.cycles ?? "ref",
|
|
34
|
+
reused: params?.reused ?? "inline",
|
|
35
|
+
external: params?.external ?? undefined,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function process(schema, ctx, _params = { path: [], schemaPath: [] }) {
|
|
39
|
+
var _a;
|
|
40
|
+
const def = schema._zod.def;
|
|
41
|
+
// check for schema in seens
|
|
42
|
+
const seen = ctx.seen.get(schema);
|
|
43
|
+
if (seen) {
|
|
44
|
+
seen.count++;
|
|
45
|
+
// check if cycle
|
|
46
|
+
const isCycle = _params.schemaPath.includes(schema);
|
|
47
|
+
if (isCycle) {
|
|
48
|
+
seen.cycle = _params.path;
|
|
49
|
+
}
|
|
50
|
+
return seen.schema;
|
|
51
|
+
}
|
|
52
|
+
// initialize
|
|
53
|
+
const result = { schema: {}, count: 1, cycle: undefined, path: _params.path };
|
|
54
|
+
ctx.seen.set(schema, result);
|
|
55
|
+
// custom method overrides default behavior
|
|
56
|
+
const overrideSchema = schema._zod.toJSONSchema?.();
|
|
57
|
+
if (overrideSchema) {
|
|
58
|
+
result.schema = overrideSchema;
|
|
16
59
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
url: "uri",
|
|
23
|
-
datetime: "date-time",
|
|
24
|
-
json_string: "json-string",
|
|
25
|
-
regex: "", // do not set
|
|
60
|
+
else {
|
|
61
|
+
const params = {
|
|
62
|
+
..._params,
|
|
63
|
+
schemaPath: [..._params.schemaPath, schema],
|
|
64
|
+
path: _params.path,
|
|
26
65
|
};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (isCycle) {
|
|
34
|
-
seen.cycle = _params.path;
|
|
35
|
-
}
|
|
36
|
-
return seen.schema;
|
|
66
|
+
const parent = schema._zod.parent;
|
|
67
|
+
if (parent) {
|
|
68
|
+
// schema was cloned from another schema
|
|
69
|
+
result.ref = parent;
|
|
70
|
+
process(parent, ctx, params);
|
|
71
|
+
ctx.seen.get(parent).isParent = true;
|
|
37
72
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
this.seen.set(schema, result);
|
|
41
|
-
// custom method overrides default behavior
|
|
42
|
-
const overrideSchema = schema._zod.toJSONSchema?.();
|
|
43
|
-
if (overrideSchema) {
|
|
44
|
-
result.schema = overrideSchema;
|
|
73
|
+
else if (schema._zod.processJSONSchema) {
|
|
74
|
+
schema._zod.processJSONSchema(ctx, result.schema, params);
|
|
45
75
|
}
|
|
46
76
|
else {
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
};
|
|
52
|
-
const parent = schema._zod.parent;
|
|
53
|
-
if (parent) {
|
|
54
|
-
// schema was cloned from another schema
|
|
55
|
-
result.ref = parent;
|
|
56
|
-
this.process(parent, params);
|
|
57
|
-
this.seen.get(parent).isParent = true;
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
const _json = result.schema;
|
|
61
|
-
switch (def.type) {
|
|
62
|
-
case "string": {
|
|
63
|
-
const json = _json;
|
|
64
|
-
json.type = "string";
|
|
65
|
-
const { minimum, maximum, format, patterns, contentEncoding } = schema._zod
|
|
66
|
-
.bag;
|
|
67
|
-
if (typeof minimum === "number")
|
|
68
|
-
json.minLength = minimum;
|
|
69
|
-
if (typeof maximum === "number")
|
|
70
|
-
json.maxLength = maximum;
|
|
71
|
-
// custom pattern overrides format
|
|
72
|
-
if (format) {
|
|
73
|
-
json.format = formatMap[format] ?? format;
|
|
74
|
-
if (json.format === "")
|
|
75
|
-
delete json.format; // empty format is not valid
|
|
76
|
-
}
|
|
77
|
-
if (contentEncoding)
|
|
78
|
-
json.contentEncoding = contentEncoding;
|
|
79
|
-
if (patterns && patterns.size > 0) {
|
|
80
|
-
const regexes = [...patterns];
|
|
81
|
-
if (regexes.length === 1)
|
|
82
|
-
json.pattern = regexes[0].source;
|
|
83
|
-
else if (regexes.length > 1) {
|
|
84
|
-
result.schema.allOf = [
|
|
85
|
-
...regexes.map((regex) => ({
|
|
86
|
-
...(this.target === "draft-7" || this.target === "draft-4" || this.target === "openapi-3.0"
|
|
87
|
-
? { type: "string" }
|
|
88
|
-
: {}),
|
|
89
|
-
pattern: regex.source,
|
|
90
|
-
})),
|
|
91
|
-
];
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
break;
|
|
95
|
-
}
|
|
96
|
-
case "number": {
|
|
97
|
-
const json = _json;
|
|
98
|
-
const { minimum, maximum, format, multipleOf, exclusiveMaximum, exclusiveMinimum } = schema._zod.bag;
|
|
99
|
-
if (typeof format === "string" && format.includes("int"))
|
|
100
|
-
json.type = "integer";
|
|
101
|
-
else
|
|
102
|
-
json.type = "number";
|
|
103
|
-
if (typeof exclusiveMinimum === "number") {
|
|
104
|
-
if (this.target === "draft-4" || this.target === "openapi-3.0") {
|
|
105
|
-
json.minimum = exclusiveMinimum;
|
|
106
|
-
json.exclusiveMinimum = true;
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
json.exclusiveMinimum = exclusiveMinimum;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
if (typeof minimum === "number") {
|
|
113
|
-
json.minimum = minimum;
|
|
114
|
-
if (typeof exclusiveMinimum === "number" && this.target !== "draft-4") {
|
|
115
|
-
if (exclusiveMinimum >= minimum)
|
|
116
|
-
delete json.minimum;
|
|
117
|
-
else
|
|
118
|
-
delete json.exclusiveMinimum;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
if (typeof exclusiveMaximum === "number") {
|
|
122
|
-
if (this.target === "draft-4" || this.target === "openapi-3.0") {
|
|
123
|
-
json.maximum = exclusiveMaximum;
|
|
124
|
-
json.exclusiveMaximum = true;
|
|
125
|
-
}
|
|
126
|
-
else {
|
|
127
|
-
json.exclusiveMaximum = exclusiveMaximum;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
if (typeof maximum === "number") {
|
|
131
|
-
json.maximum = maximum;
|
|
132
|
-
if (typeof exclusiveMaximum === "number" && this.target !== "draft-4") {
|
|
133
|
-
if (exclusiveMaximum <= maximum)
|
|
134
|
-
delete json.maximum;
|
|
135
|
-
else
|
|
136
|
-
delete json.exclusiveMaximum;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
if (typeof multipleOf === "number")
|
|
140
|
-
json.multipleOf = multipleOf;
|
|
141
|
-
break;
|
|
142
|
-
}
|
|
143
|
-
case "boolean": {
|
|
144
|
-
const json = _json;
|
|
145
|
-
json.type = "boolean";
|
|
146
|
-
break;
|
|
147
|
-
}
|
|
148
|
-
case "bigint": {
|
|
149
|
-
if (this.unrepresentable === "throw") {
|
|
150
|
-
throw new Error("BigInt cannot be represented in JSON Schema");
|
|
151
|
-
}
|
|
152
|
-
break;
|
|
153
|
-
}
|
|
154
|
-
case "symbol": {
|
|
155
|
-
if (this.unrepresentable === "throw") {
|
|
156
|
-
throw new Error("Symbols cannot be represented in JSON Schema");
|
|
157
|
-
}
|
|
158
|
-
break;
|
|
159
|
-
}
|
|
160
|
-
case "null": {
|
|
161
|
-
if (this.target === "openapi-3.0") {
|
|
162
|
-
_json.type = "string";
|
|
163
|
-
_json.nullable = true;
|
|
164
|
-
_json.enum = [null];
|
|
165
|
-
}
|
|
166
|
-
else
|
|
167
|
-
_json.type = "null";
|
|
168
|
-
break;
|
|
169
|
-
}
|
|
170
|
-
case "any": {
|
|
171
|
-
break;
|
|
172
|
-
}
|
|
173
|
-
case "unknown": {
|
|
174
|
-
break;
|
|
175
|
-
}
|
|
176
|
-
case "undefined": {
|
|
177
|
-
if (this.unrepresentable === "throw") {
|
|
178
|
-
throw new Error("Undefined cannot be represented in JSON Schema");
|
|
179
|
-
}
|
|
180
|
-
break;
|
|
181
|
-
}
|
|
182
|
-
case "void": {
|
|
183
|
-
if (this.unrepresentable === "throw") {
|
|
184
|
-
throw new Error("Void cannot be represented in JSON Schema");
|
|
185
|
-
}
|
|
186
|
-
break;
|
|
187
|
-
}
|
|
188
|
-
case "never": {
|
|
189
|
-
_json.not = {};
|
|
190
|
-
break;
|
|
191
|
-
}
|
|
192
|
-
case "date": {
|
|
193
|
-
if (this.unrepresentable === "throw") {
|
|
194
|
-
throw new Error("Date cannot be represented in JSON Schema");
|
|
195
|
-
}
|
|
196
|
-
break;
|
|
197
|
-
}
|
|
198
|
-
case "array": {
|
|
199
|
-
const json = _json;
|
|
200
|
-
const { minimum, maximum } = schema._zod.bag;
|
|
201
|
-
if (typeof minimum === "number")
|
|
202
|
-
json.minItems = minimum;
|
|
203
|
-
if (typeof maximum === "number")
|
|
204
|
-
json.maxItems = maximum;
|
|
205
|
-
json.type = "array";
|
|
206
|
-
json.items = this.process(def.element, { ...params, path: [...params.path, "items"] });
|
|
207
|
-
break;
|
|
208
|
-
}
|
|
209
|
-
case "object": {
|
|
210
|
-
const json = _json;
|
|
211
|
-
json.type = "object";
|
|
212
|
-
json.properties = {};
|
|
213
|
-
const shape = def.shape; // params.shapeCache.get(schema)!;
|
|
214
|
-
for (const key in shape) {
|
|
215
|
-
json.properties[key] = this.process(shape[key], {
|
|
216
|
-
...params,
|
|
217
|
-
path: [...params.path, "properties", key],
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
// required keys
|
|
221
|
-
const allKeys = new Set(Object.keys(shape));
|
|
222
|
-
// const optionalKeys = new Set(def.optional);
|
|
223
|
-
const requiredKeys = new Set([...allKeys].filter((key) => {
|
|
224
|
-
const v = def.shape[key]._zod;
|
|
225
|
-
if (this.io === "input") {
|
|
226
|
-
return v.optin === undefined;
|
|
227
|
-
}
|
|
228
|
-
else {
|
|
229
|
-
return v.optout === undefined;
|
|
230
|
-
}
|
|
231
|
-
}));
|
|
232
|
-
if (requiredKeys.size > 0) {
|
|
233
|
-
json.required = Array.from(requiredKeys);
|
|
234
|
-
}
|
|
235
|
-
// catchall
|
|
236
|
-
if (def.catchall?._zod.def.type === "never") {
|
|
237
|
-
// strict
|
|
238
|
-
json.additionalProperties = false;
|
|
239
|
-
}
|
|
240
|
-
else if (!def.catchall) {
|
|
241
|
-
// regular
|
|
242
|
-
if (this.io === "output")
|
|
243
|
-
json.additionalProperties = false;
|
|
244
|
-
}
|
|
245
|
-
else if (def.catchall) {
|
|
246
|
-
json.additionalProperties = this.process(def.catchall, {
|
|
247
|
-
...params,
|
|
248
|
-
path: [...params.path, "additionalProperties"],
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
break;
|
|
252
|
-
}
|
|
253
|
-
case "union": {
|
|
254
|
-
const json = _json;
|
|
255
|
-
// Discriminated unions use oneOf (exactly one match) instead of anyOf (one or more matches)
|
|
256
|
-
// because the discriminator field ensures mutual exclusivity between options in JSON Schema
|
|
257
|
-
const isDiscriminated = def.discriminator !== undefined;
|
|
258
|
-
const options = def.options.map((x, i) => this.process(x, {
|
|
259
|
-
...params,
|
|
260
|
-
path: [...params.path, isDiscriminated ? "oneOf" : "anyOf", i],
|
|
261
|
-
}));
|
|
262
|
-
if (isDiscriminated) {
|
|
263
|
-
json.oneOf = options;
|
|
264
|
-
}
|
|
265
|
-
else {
|
|
266
|
-
json.anyOf = options;
|
|
267
|
-
}
|
|
268
|
-
break;
|
|
269
|
-
}
|
|
270
|
-
case "intersection": {
|
|
271
|
-
const json = _json;
|
|
272
|
-
const a = this.process(def.left, {
|
|
273
|
-
...params,
|
|
274
|
-
path: [...params.path, "allOf", 0],
|
|
275
|
-
});
|
|
276
|
-
const b = this.process(def.right, {
|
|
277
|
-
...params,
|
|
278
|
-
path: [...params.path, "allOf", 1],
|
|
279
|
-
});
|
|
280
|
-
const isSimpleIntersection = (val) => "allOf" in val && Object.keys(val).length === 1;
|
|
281
|
-
const allOf = [
|
|
282
|
-
...(isSimpleIntersection(a) ? a.allOf : [a]),
|
|
283
|
-
...(isSimpleIntersection(b) ? b.allOf : [b]),
|
|
284
|
-
];
|
|
285
|
-
json.allOf = allOf;
|
|
286
|
-
break;
|
|
287
|
-
}
|
|
288
|
-
case "tuple": {
|
|
289
|
-
const json = _json;
|
|
290
|
-
json.type = "array";
|
|
291
|
-
const prefixPath = this.target === "draft-2020-12" ? "prefixItems" : "items";
|
|
292
|
-
const restPath = this.target === "draft-2020-12" ? "items" : this.target === "openapi-3.0" ? "items" : "additionalItems";
|
|
293
|
-
const prefixItems = def.items.map((x, i) => this.process(x, {
|
|
294
|
-
...params,
|
|
295
|
-
path: [...params.path, prefixPath, i],
|
|
296
|
-
}));
|
|
297
|
-
const rest = def.rest
|
|
298
|
-
? this.process(def.rest, {
|
|
299
|
-
...params,
|
|
300
|
-
path: [...params.path, restPath, ...(this.target === "openapi-3.0" ? [def.items.length] : [])],
|
|
301
|
-
})
|
|
302
|
-
: null;
|
|
303
|
-
if (this.target === "draft-2020-12") {
|
|
304
|
-
json.prefixItems = prefixItems;
|
|
305
|
-
if (rest) {
|
|
306
|
-
json.items = rest;
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
else if (this.target === "openapi-3.0") {
|
|
310
|
-
json.items = {
|
|
311
|
-
anyOf: prefixItems,
|
|
312
|
-
};
|
|
313
|
-
if (rest) {
|
|
314
|
-
json.items.anyOf.push(rest);
|
|
315
|
-
}
|
|
316
|
-
json.minItems = prefixItems.length;
|
|
317
|
-
if (!rest) {
|
|
318
|
-
json.maxItems = prefixItems.length;
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
else {
|
|
322
|
-
json.items = prefixItems;
|
|
323
|
-
if (rest) {
|
|
324
|
-
json.additionalItems = rest;
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
// length
|
|
328
|
-
const { minimum, maximum } = schema._zod.bag;
|
|
329
|
-
if (typeof minimum === "number")
|
|
330
|
-
json.minItems = minimum;
|
|
331
|
-
if (typeof maximum === "number")
|
|
332
|
-
json.maxItems = maximum;
|
|
333
|
-
break;
|
|
334
|
-
}
|
|
335
|
-
case "record": {
|
|
336
|
-
const json = _json;
|
|
337
|
-
json.type = "object";
|
|
338
|
-
if (this.target === "draft-7" || this.target === "draft-2020-12") {
|
|
339
|
-
json.propertyNames = this.process(def.keyType, {
|
|
340
|
-
...params,
|
|
341
|
-
path: [...params.path, "propertyNames"],
|
|
342
|
-
});
|
|
343
|
-
}
|
|
344
|
-
json.additionalProperties = this.process(def.valueType, {
|
|
345
|
-
...params,
|
|
346
|
-
path: [...params.path, "additionalProperties"],
|
|
347
|
-
});
|
|
348
|
-
break;
|
|
349
|
-
}
|
|
350
|
-
case "map": {
|
|
351
|
-
if (this.unrepresentable === "throw") {
|
|
352
|
-
throw new Error("Map cannot be represented in JSON Schema");
|
|
353
|
-
}
|
|
354
|
-
break;
|
|
355
|
-
}
|
|
356
|
-
case "set": {
|
|
357
|
-
if (this.unrepresentable === "throw") {
|
|
358
|
-
throw new Error("Set cannot be represented in JSON Schema");
|
|
359
|
-
}
|
|
360
|
-
break;
|
|
361
|
-
}
|
|
362
|
-
case "enum": {
|
|
363
|
-
const json = _json;
|
|
364
|
-
const values = (0, util_js_1.getEnumValues)(def.entries);
|
|
365
|
-
// Number enums can have both string and number values
|
|
366
|
-
if (values.every((v) => typeof v === "number"))
|
|
367
|
-
json.type = "number";
|
|
368
|
-
if (values.every((v) => typeof v === "string"))
|
|
369
|
-
json.type = "string";
|
|
370
|
-
json.enum = values;
|
|
371
|
-
break;
|
|
372
|
-
}
|
|
373
|
-
case "literal": {
|
|
374
|
-
const json = _json;
|
|
375
|
-
const vals = [];
|
|
376
|
-
for (const val of def.values) {
|
|
377
|
-
if (val === undefined) {
|
|
378
|
-
if (this.unrepresentable === "throw") {
|
|
379
|
-
throw new Error("Literal `undefined` cannot be represented in JSON Schema");
|
|
380
|
-
}
|
|
381
|
-
else {
|
|
382
|
-
// do not add to vals
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
else if (typeof val === "bigint") {
|
|
386
|
-
if (this.unrepresentable === "throw") {
|
|
387
|
-
throw new Error("BigInt literals cannot be represented in JSON Schema");
|
|
388
|
-
}
|
|
389
|
-
else {
|
|
390
|
-
vals.push(Number(val));
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
else {
|
|
394
|
-
vals.push(val);
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
if (vals.length === 0) {
|
|
398
|
-
// do nothing (an undefined literal was stripped)
|
|
399
|
-
}
|
|
400
|
-
else if (vals.length === 1) {
|
|
401
|
-
const val = vals[0];
|
|
402
|
-
json.type = val === null ? "null" : typeof val;
|
|
403
|
-
if (this.target === "draft-4" || this.target === "openapi-3.0") {
|
|
404
|
-
json.enum = [val];
|
|
405
|
-
}
|
|
406
|
-
else {
|
|
407
|
-
json.const = val;
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
else {
|
|
411
|
-
if (vals.every((v) => typeof v === "number"))
|
|
412
|
-
json.type = "number";
|
|
413
|
-
if (vals.every((v) => typeof v === "string"))
|
|
414
|
-
json.type = "string";
|
|
415
|
-
if (vals.every((v) => typeof v === "boolean"))
|
|
416
|
-
json.type = "string";
|
|
417
|
-
if (vals.every((v) => v === null))
|
|
418
|
-
json.type = "null";
|
|
419
|
-
json.enum = vals;
|
|
420
|
-
}
|
|
421
|
-
break;
|
|
422
|
-
}
|
|
423
|
-
case "file": {
|
|
424
|
-
const json = _json;
|
|
425
|
-
const file = {
|
|
426
|
-
type: "string",
|
|
427
|
-
format: "binary",
|
|
428
|
-
contentEncoding: "binary",
|
|
429
|
-
};
|
|
430
|
-
const { minimum, maximum, mime } = schema._zod.bag;
|
|
431
|
-
if (minimum !== undefined)
|
|
432
|
-
file.minLength = minimum;
|
|
433
|
-
if (maximum !== undefined)
|
|
434
|
-
file.maxLength = maximum;
|
|
435
|
-
if (mime) {
|
|
436
|
-
if (mime.length === 1) {
|
|
437
|
-
file.contentMediaType = mime[0];
|
|
438
|
-
Object.assign(json, file);
|
|
439
|
-
}
|
|
440
|
-
else {
|
|
441
|
-
json.anyOf = mime.map((m) => {
|
|
442
|
-
const mFile = { ...file, contentMediaType: m };
|
|
443
|
-
return mFile;
|
|
444
|
-
});
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
else {
|
|
448
|
-
Object.assign(json, file);
|
|
449
|
-
}
|
|
450
|
-
// if (this.unrepresentable === "throw") {
|
|
451
|
-
// throw new Error("File cannot be represented in JSON Schema");
|
|
452
|
-
// }
|
|
453
|
-
break;
|
|
454
|
-
}
|
|
455
|
-
case "transform": {
|
|
456
|
-
if (this.unrepresentable === "throw") {
|
|
457
|
-
throw new Error("Transforms cannot be represented in JSON Schema");
|
|
458
|
-
}
|
|
459
|
-
break;
|
|
460
|
-
}
|
|
461
|
-
case "nullable": {
|
|
462
|
-
const inner = this.process(def.innerType, params);
|
|
463
|
-
if (this.target === "openapi-3.0") {
|
|
464
|
-
result.ref = def.innerType;
|
|
465
|
-
_json.nullable = true;
|
|
466
|
-
}
|
|
467
|
-
else {
|
|
468
|
-
_json.anyOf = [inner, { type: "null" }];
|
|
469
|
-
}
|
|
470
|
-
break;
|
|
471
|
-
}
|
|
472
|
-
case "nonoptional": {
|
|
473
|
-
this.process(def.innerType, params);
|
|
474
|
-
result.ref = def.innerType;
|
|
475
|
-
break;
|
|
476
|
-
}
|
|
477
|
-
case "success": {
|
|
478
|
-
const json = _json;
|
|
479
|
-
json.type = "boolean";
|
|
480
|
-
break;
|
|
481
|
-
}
|
|
482
|
-
case "default": {
|
|
483
|
-
this.process(def.innerType, params);
|
|
484
|
-
result.ref = def.innerType;
|
|
485
|
-
_json.default = JSON.parse(JSON.stringify(def.defaultValue));
|
|
486
|
-
break;
|
|
487
|
-
}
|
|
488
|
-
case "prefault": {
|
|
489
|
-
this.process(def.innerType, params);
|
|
490
|
-
result.ref = def.innerType;
|
|
491
|
-
if (this.io === "input")
|
|
492
|
-
_json._prefault = JSON.parse(JSON.stringify(def.defaultValue));
|
|
493
|
-
break;
|
|
494
|
-
}
|
|
495
|
-
case "catch": {
|
|
496
|
-
// use conditionals
|
|
497
|
-
this.process(def.innerType, params);
|
|
498
|
-
result.ref = def.innerType;
|
|
499
|
-
let catchValue;
|
|
500
|
-
try {
|
|
501
|
-
catchValue = def.catchValue(undefined);
|
|
502
|
-
}
|
|
503
|
-
catch {
|
|
504
|
-
throw new Error("Dynamic catch values are not supported in JSON Schema");
|
|
505
|
-
}
|
|
506
|
-
_json.default = catchValue;
|
|
507
|
-
break;
|
|
508
|
-
}
|
|
509
|
-
case "nan": {
|
|
510
|
-
if (this.unrepresentable === "throw") {
|
|
511
|
-
throw new Error("NaN cannot be represented in JSON Schema");
|
|
512
|
-
}
|
|
513
|
-
break;
|
|
514
|
-
}
|
|
515
|
-
case "template_literal": {
|
|
516
|
-
const json = _json;
|
|
517
|
-
const pattern = schema._zod.pattern;
|
|
518
|
-
if (!pattern)
|
|
519
|
-
throw new Error("Pattern not found in template literal");
|
|
520
|
-
json.type = "string";
|
|
521
|
-
json.pattern = pattern.source;
|
|
522
|
-
break;
|
|
523
|
-
}
|
|
524
|
-
case "pipe": {
|
|
525
|
-
const innerType = this.io === "input" ? (def.in._zod.def.type === "transform" ? def.out : def.in) : def.out;
|
|
526
|
-
this.process(innerType, params);
|
|
527
|
-
result.ref = innerType;
|
|
528
|
-
break;
|
|
529
|
-
}
|
|
530
|
-
case "readonly": {
|
|
531
|
-
this.process(def.innerType, params);
|
|
532
|
-
result.ref = def.innerType;
|
|
533
|
-
_json.readOnly = true;
|
|
534
|
-
break;
|
|
535
|
-
}
|
|
536
|
-
// passthrough types
|
|
537
|
-
case "promise": {
|
|
538
|
-
this.process(def.innerType, params);
|
|
539
|
-
result.ref = def.innerType;
|
|
540
|
-
break;
|
|
541
|
-
}
|
|
542
|
-
case "optional": {
|
|
543
|
-
this.process(def.innerType, params);
|
|
544
|
-
result.ref = def.innerType;
|
|
545
|
-
break;
|
|
546
|
-
}
|
|
547
|
-
case "lazy": {
|
|
548
|
-
const innerType = schema._zod.innerType;
|
|
549
|
-
this.process(innerType, params);
|
|
550
|
-
result.ref = innerType;
|
|
551
|
-
break;
|
|
552
|
-
}
|
|
553
|
-
case "custom": {
|
|
554
|
-
if (this.unrepresentable === "throw") {
|
|
555
|
-
throw new Error("Custom types cannot be represented in JSON Schema");
|
|
556
|
-
}
|
|
557
|
-
break;
|
|
558
|
-
}
|
|
559
|
-
case "function": {
|
|
560
|
-
if (this.unrepresentable === "throw") {
|
|
561
|
-
throw new Error("Function types cannot be represented in JSON Schema");
|
|
562
|
-
}
|
|
563
|
-
break;
|
|
564
|
-
}
|
|
565
|
-
default: {
|
|
566
|
-
def;
|
|
567
|
-
}
|
|
568
|
-
}
|
|
77
|
+
const _json = result.schema;
|
|
78
|
+
const processor = ctx.processors[def.type];
|
|
79
|
+
if (!processor) {
|
|
80
|
+
throw new Error(`[toJSONSchema]: Non-representable type encountered: ${def.type}`);
|
|
569
81
|
}
|
|
82
|
+
processor(schema, ctx, _json, params);
|
|
570
83
|
}
|
|
571
|
-
// metadata
|
|
572
|
-
const meta = this.metadataRegistry.get(schema);
|
|
573
|
-
if (meta)
|
|
574
|
-
Object.assign(result.schema, meta);
|
|
575
|
-
if (this.io === "input" && isTransforming(schema)) {
|
|
576
|
-
// examples/defaults only apply to output type of pipe
|
|
577
|
-
delete result.schema.examples;
|
|
578
|
-
delete result.schema.default;
|
|
579
|
-
}
|
|
580
|
-
// set prefault as default
|
|
581
|
-
if (this.io === "input" && result.schema._prefault)
|
|
582
|
-
(_a = result.schema).default ?? (_a.default = result.schema._prefault);
|
|
583
|
-
delete result.schema._prefault;
|
|
584
|
-
// pulling fresh from this.seen in case it was overwritten
|
|
585
|
-
const _result = this.seen.get(schema);
|
|
586
|
-
return _result.schema;
|
|
587
84
|
}
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
}
|
|
622
|
-
if
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
const uriPrefix = `#`;
|
|
627
|
-
const defUriPrefix = `${uriPrefix}/${defsSegment}/`;
|
|
628
|
-
const defId = entry[1].schema.id ?? `__schema${this.counter++}`;
|
|
629
|
-
return { defId, ref: defUriPrefix + defId };
|
|
630
|
-
};
|
|
631
|
-
// stored cached version in `def` property
|
|
632
|
-
// remove all properties, set $ref
|
|
633
|
-
const extractToDef = (entry) => {
|
|
634
|
-
// if the schema is already a reference, do not extract it
|
|
635
|
-
if (entry[1].schema.$ref) {
|
|
636
|
-
return;
|
|
637
|
-
}
|
|
638
|
-
const seen = entry[1];
|
|
639
|
-
const { ref, defId } = makeURI(entry);
|
|
640
|
-
seen.def = { ...seen.schema };
|
|
641
|
-
// defId won't be set if the schema is a reference to an external schema
|
|
642
|
-
if (defId)
|
|
643
|
-
seen.defId = defId;
|
|
644
|
-
// wipe away all properties except $ref
|
|
645
|
-
const schema = seen.schema;
|
|
646
|
-
for (const key in schema) {
|
|
647
|
-
delete schema[key];
|
|
648
|
-
}
|
|
649
|
-
schema.$ref = ref;
|
|
650
|
-
};
|
|
651
|
-
// throw on cycles
|
|
652
|
-
// break cycles
|
|
653
|
-
if (params.cycles === "throw") {
|
|
654
|
-
for (const entry of this.seen.entries()) {
|
|
655
|
-
const seen = entry[1];
|
|
656
|
-
if (seen.cycle) {
|
|
657
|
-
throw new Error("Cycle detected: " +
|
|
658
|
-
`#/${seen.cycle?.join("/")}/<root>` +
|
|
659
|
-
'\n\nSet the `cycles` parameter to `"ref"` to resolve cyclical schemas with defs.');
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
// extract schemas into $defs
|
|
664
|
-
for (const entry of this.seen.entries()) {
|
|
665
|
-
const seen = entry[1];
|
|
666
|
-
// convert root schema to # $ref
|
|
667
|
-
if (schema === entry[0]) {
|
|
668
|
-
extractToDef(entry); // this has special handling for the root schema
|
|
669
|
-
continue;
|
|
670
|
-
}
|
|
671
|
-
// extract schemas that are in the external registry
|
|
672
|
-
if (params.external) {
|
|
673
|
-
const ext = params.external.registry.get(entry[0])?.id;
|
|
674
|
-
if (schema !== entry[0] && ext) {
|
|
675
|
-
extractToDef(entry);
|
|
676
|
-
continue;
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
// extract schemas with `id` meta
|
|
680
|
-
const id = this.metadataRegistry.get(entry[0])?.id;
|
|
681
|
-
if (id) {
|
|
682
|
-
extractToDef(entry);
|
|
683
|
-
continue;
|
|
684
|
-
}
|
|
685
|
-
// break cycles
|
|
686
|
-
if (seen.cycle) {
|
|
687
|
-
// any
|
|
688
|
-
extractToDef(entry);
|
|
689
|
-
continue;
|
|
690
|
-
}
|
|
691
|
-
// extract reused schemas
|
|
692
|
-
if (seen.count > 1) {
|
|
693
|
-
if (params.reused === "ref") {
|
|
694
|
-
extractToDef(entry);
|
|
695
|
-
// biome-ignore lint:
|
|
696
|
-
continue;
|
|
697
|
-
}
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
// flatten _refs
|
|
701
|
-
const flattenRef = (zodSchema, params) => {
|
|
702
|
-
const seen = this.seen.get(zodSchema);
|
|
703
|
-
const schema = seen.def ?? seen.schema;
|
|
704
|
-
const _cached = { ...schema };
|
|
705
|
-
// already seen
|
|
706
|
-
if (seen.ref === null) {
|
|
707
|
-
return;
|
|
708
|
-
}
|
|
709
|
-
// flatten ref if defined
|
|
710
|
-
const ref = seen.ref;
|
|
711
|
-
seen.ref = null; // prevent recursion
|
|
712
|
-
if (ref) {
|
|
713
|
-
flattenRef(ref, params);
|
|
714
|
-
// merge referenced schema into current
|
|
715
|
-
const refSchema = this.seen.get(ref).schema;
|
|
716
|
-
if (refSchema.$ref &&
|
|
717
|
-
(params.target === "draft-7" || params.target === "draft-4" || params.target === "openapi-3.0")) {
|
|
718
|
-
schema.allOf = schema.allOf ?? [];
|
|
719
|
-
schema.allOf.push(refSchema);
|
|
720
|
-
}
|
|
721
|
-
else {
|
|
722
|
-
Object.assign(schema, refSchema);
|
|
723
|
-
Object.assign(schema, _cached); // prevent overwriting any fields in the original schema
|
|
724
|
-
}
|
|
85
|
+
// metadata
|
|
86
|
+
const meta = ctx.metadataRegistry.get(schema);
|
|
87
|
+
if (meta)
|
|
88
|
+
Object.assign(result.schema, meta);
|
|
89
|
+
if (ctx.io === "input" && isTransforming(schema)) {
|
|
90
|
+
// examples/defaults only apply to output type of pipe
|
|
91
|
+
delete result.schema.examples;
|
|
92
|
+
delete result.schema.default;
|
|
93
|
+
}
|
|
94
|
+
// set prefault as default
|
|
95
|
+
if (ctx.io === "input" && result.schema._prefault)
|
|
96
|
+
(_a = result.schema).default ?? (_a.default = result.schema._prefault);
|
|
97
|
+
delete result.schema._prefault;
|
|
98
|
+
// pulling fresh from ctx.seen in case it was overwritten
|
|
99
|
+
const _result = ctx.seen.get(schema);
|
|
100
|
+
return _result.schema;
|
|
101
|
+
}
|
|
102
|
+
function extractDefs(ctx, schema
|
|
103
|
+
// params: EmitParams
|
|
104
|
+
) {
|
|
105
|
+
// iterate over seen map;
|
|
106
|
+
const root = ctx.seen.get(schema);
|
|
107
|
+
if (!root)
|
|
108
|
+
throw new Error("Unprocessed schema. This is a bug in Zod.");
|
|
109
|
+
// returns a ref to the schema
|
|
110
|
+
// defId will be empty if the ref points to an external schema (or #)
|
|
111
|
+
const makeURI = (entry) => {
|
|
112
|
+
// comparing the seen objects because sometimes
|
|
113
|
+
// multiple schemas map to the same seen object.
|
|
114
|
+
// e.g. lazy
|
|
115
|
+
// external is configured
|
|
116
|
+
const defsSegment = ctx.target === "draft-2020-12" ? "$defs" : "definitions";
|
|
117
|
+
if (ctx.external) {
|
|
118
|
+
const externalId = ctx.external.registry.get(entry[0])?.id; // ?? "__shared";// `__schema${ctx.counter++}`;
|
|
119
|
+
// check if schema is in the external registry
|
|
120
|
+
const uriGenerator = ctx.external.uri ?? ((id) => id);
|
|
121
|
+
if (externalId) {
|
|
122
|
+
return { ref: uriGenerator(externalId) };
|
|
725
123
|
}
|
|
726
|
-
//
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
jsonSchema: schema,
|
|
731
|
-
path: seen.path ?? [],
|
|
732
|
-
});
|
|
733
|
-
};
|
|
734
|
-
for (const entry of [...this.seen.entries()].reverse()) {
|
|
735
|
-
flattenRef(entry[0], { target: this.target });
|
|
736
|
-
}
|
|
737
|
-
const result = {};
|
|
738
|
-
if (this.target === "draft-2020-12") {
|
|
739
|
-
result.$schema = "https://json-schema.org/draft/2020-12/schema";
|
|
124
|
+
// otherwise, add to __shared
|
|
125
|
+
const id = entry[1].defId ?? entry[1].schema.id ?? `schema${ctx.counter++}`;
|
|
126
|
+
entry[1].defId = id; // set defId so it will be reused if needed
|
|
127
|
+
return { defId: id, ref: `${uriGenerator("__shared")}#/${defsSegment}/${id}` };
|
|
740
128
|
}
|
|
741
|
-
|
|
742
|
-
|
|
129
|
+
if (entry[1] === root) {
|
|
130
|
+
return { ref: "#" };
|
|
743
131
|
}
|
|
744
|
-
|
|
745
|
-
|
|
132
|
+
// self-contained schema
|
|
133
|
+
const uriPrefix = `#`;
|
|
134
|
+
const defUriPrefix = `${uriPrefix}/${defsSegment}/`;
|
|
135
|
+
const defId = entry[1].schema.id ?? `__schema${ctx.counter++}`;
|
|
136
|
+
return { defId, ref: defUriPrefix + defId };
|
|
137
|
+
};
|
|
138
|
+
// stored cached version in `def` property
|
|
139
|
+
// remove all properties, set $ref
|
|
140
|
+
const extractToDef = (entry) => {
|
|
141
|
+
// if the schema is already a reference, do not extract it
|
|
142
|
+
if (entry[1].schema.$ref) {
|
|
143
|
+
return;
|
|
746
144
|
}
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
145
|
+
const seen = entry[1];
|
|
146
|
+
const { ref, defId } = makeURI(entry);
|
|
147
|
+
seen.def = { ...seen.schema };
|
|
148
|
+
// defId won't be set if the schema is a reference to an external schema
|
|
149
|
+
// or if the schema is the root schema
|
|
150
|
+
if (defId)
|
|
151
|
+
seen.defId = defId;
|
|
152
|
+
// wipe away all properties except $ref
|
|
153
|
+
const schema = seen.schema;
|
|
154
|
+
for (const key in schema) {
|
|
155
|
+
delete schema[key];
|
|
753
156
|
}
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
Object.assign(result, root.def);
|
|
761
|
-
// build defs object
|
|
762
|
-
const defs = params.external?.defs ?? {};
|
|
763
|
-
for (const entry of this.seen.entries()) {
|
|
157
|
+
schema.$ref = ref;
|
|
158
|
+
};
|
|
159
|
+
// throw on cycles
|
|
160
|
+
// break cycles
|
|
161
|
+
if (ctx.cycles === "throw") {
|
|
162
|
+
for (const entry of ctx.seen.entries()) {
|
|
764
163
|
const seen = entry[1];
|
|
765
|
-
if (seen.
|
|
766
|
-
|
|
164
|
+
if (seen.cycle) {
|
|
165
|
+
throw new Error("Cycle detected: " +
|
|
166
|
+
`#/${seen.cycle?.join("/")}/<root>` +
|
|
167
|
+
'\n\nSet the `cycles` parameter to `"ref"` to resolve cyclical schemas with defs.');
|
|
767
168
|
}
|
|
768
169
|
}
|
|
769
|
-
|
|
770
|
-
|
|
170
|
+
}
|
|
171
|
+
// extract schemas into $defs
|
|
172
|
+
for (const entry of ctx.seen.entries()) {
|
|
173
|
+
const seen = entry[1];
|
|
174
|
+
// convert root schema to # $ref
|
|
175
|
+
if (schema === entry[0]) {
|
|
176
|
+
extractToDef(entry); // this has special handling for the root schema
|
|
177
|
+
continue;
|
|
771
178
|
}
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
result.definitions = defs;
|
|
779
|
-
}
|
|
179
|
+
// extract schemas that are in the external registry
|
|
180
|
+
if (ctx.external) {
|
|
181
|
+
const ext = ctx.external.registry.get(entry[0])?.id;
|
|
182
|
+
if (schema !== entry[0] && ext) {
|
|
183
|
+
extractToDef(entry);
|
|
184
|
+
continue;
|
|
780
185
|
}
|
|
781
186
|
}
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
187
|
+
// extract schemas with `id` meta
|
|
188
|
+
const id = ctx.metadataRegistry.get(entry[0])?.id;
|
|
189
|
+
if (id) {
|
|
190
|
+
extractToDef(entry);
|
|
191
|
+
continue;
|
|
787
192
|
}
|
|
788
|
-
|
|
789
|
-
|
|
193
|
+
// break cycles
|
|
194
|
+
if (seen.cycle) {
|
|
195
|
+
// any
|
|
196
|
+
extractToDef(entry);
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
// extract reused schemas
|
|
200
|
+
if (seen.count > 1) {
|
|
201
|
+
if (ctx.reused === "ref") {
|
|
202
|
+
extractToDef(entry);
|
|
203
|
+
// biome-ignore lint:
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
790
206
|
}
|
|
791
207
|
}
|
|
792
208
|
}
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
209
|
+
function finalize(ctx, schema) {
|
|
210
|
+
//
|
|
211
|
+
// iterate over seen map;
|
|
212
|
+
const root = ctx.seen.get(schema);
|
|
213
|
+
if (!root)
|
|
214
|
+
throw new Error("Unprocessed schema. This is a bug in Zod.");
|
|
215
|
+
// flatten _refs
|
|
216
|
+
const flattenRef = (zodSchema) => {
|
|
217
|
+
const seen = ctx.seen.get(zodSchema);
|
|
218
|
+
const schema = seen.def ?? seen.schema;
|
|
219
|
+
const _cached = { ...schema };
|
|
220
|
+
// already seen
|
|
221
|
+
if (seen.ref === null) {
|
|
222
|
+
return;
|
|
801
223
|
}
|
|
802
|
-
|
|
803
|
-
const
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
224
|
+
// flatten ref if defined
|
|
225
|
+
const ref = seen.ref;
|
|
226
|
+
seen.ref = null; // prevent recursion
|
|
227
|
+
if (ref) {
|
|
228
|
+
flattenRef(ref);
|
|
229
|
+
// merge referenced schema into current
|
|
230
|
+
const refSchema = ctx.seen.get(ref).schema;
|
|
231
|
+
if (refSchema.$ref && (ctx.target === "draft-07" || ctx.target === "draft-04" || ctx.target === "openapi-3.0")) {
|
|
232
|
+
schema.allOf = schema.allOf ?? [];
|
|
233
|
+
schema.allOf.push(refSchema);
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
Object.assign(schema, refSchema);
|
|
237
|
+
Object.assign(schema, _cached); // prevent overwriting any fields in the original schema
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
// execute overrides
|
|
241
|
+
if (!seen.isParent)
|
|
242
|
+
ctx.override({
|
|
243
|
+
zodSchema: zodSchema,
|
|
244
|
+
jsonSchema: schema,
|
|
245
|
+
path: seen.path ?? [],
|
|
813
246
|
});
|
|
247
|
+
};
|
|
248
|
+
for (const entry of [...ctx.seen.entries()].reverse()) {
|
|
249
|
+
flattenRef(entry[0]);
|
|
250
|
+
}
|
|
251
|
+
const result = {};
|
|
252
|
+
if (ctx.target === "draft-2020-12") {
|
|
253
|
+
result.$schema = "https://json-schema.org/draft/2020-12/schema";
|
|
254
|
+
}
|
|
255
|
+
else if (ctx.target === "draft-07") {
|
|
256
|
+
result.$schema = "http://json-schema.org/draft-07/schema#";
|
|
257
|
+
}
|
|
258
|
+
else if (ctx.target === "draft-04") {
|
|
259
|
+
result.$schema = "http://json-schema.org/draft-04/schema#";
|
|
260
|
+
}
|
|
261
|
+
else if (ctx.target === "openapi-3.0") {
|
|
262
|
+
// OpenAPI 3.0 schema objects should not include a $schema property
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
// Arbitrary string values are allowed but won't have a $schema property set
|
|
266
|
+
}
|
|
267
|
+
if (ctx.external?.uri) {
|
|
268
|
+
const id = ctx.external.registry.get(schema)?.id;
|
|
269
|
+
if (!id)
|
|
270
|
+
throw new Error("Schema is missing an `id` property");
|
|
271
|
+
result.$id = ctx.external.uri(id);
|
|
272
|
+
}
|
|
273
|
+
Object.assign(result, root.def ?? root.schema);
|
|
274
|
+
// build defs object
|
|
275
|
+
const defs = ctx.external?.defs ?? {};
|
|
276
|
+
for (const entry of ctx.seen.entries()) {
|
|
277
|
+
const seen = entry[1];
|
|
278
|
+
if (seen.def && seen.defId) {
|
|
279
|
+
defs[seen.defId] = seen.def;
|
|
814
280
|
}
|
|
281
|
+
}
|
|
282
|
+
// set definitions in result
|
|
283
|
+
if (ctx.external) {
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
815
286
|
if (Object.keys(defs).length > 0) {
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
287
|
+
if (ctx.target === "draft-2020-12") {
|
|
288
|
+
result.$defs = defs;
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
result.definitions = defs;
|
|
292
|
+
}
|
|
820
293
|
}
|
|
821
|
-
return { schemas };
|
|
822
294
|
}
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
295
|
+
try {
|
|
296
|
+
// this "finalizes" this schema and ensures all cycles are removed
|
|
297
|
+
// each call to finalize() is functionally independent
|
|
298
|
+
// though the seen map is shared
|
|
299
|
+
const finalized = JSON.parse(JSON.stringify(result));
|
|
300
|
+
Object.defineProperty(finalized, "~standard", {
|
|
301
|
+
value: {
|
|
302
|
+
...schema["~standard"],
|
|
303
|
+
jsonSchema: {
|
|
304
|
+
input: (0, exports.createStandardJSONSchemaMethod)(schema, "input"),
|
|
305
|
+
output: (0, exports.createStandardJSONSchemaMethod)(schema, "output"),
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
enumerable: false,
|
|
309
|
+
writable: false,
|
|
310
|
+
});
|
|
311
|
+
return finalized;
|
|
312
|
+
}
|
|
313
|
+
catch (_err) {
|
|
314
|
+
throw new Error("Error converting schema to JSON.");
|
|
315
|
+
}
|
|
826
316
|
}
|
|
827
317
|
function isTransforming(_schema, _ctx) {
|
|
828
318
|
const ctx = _ctx ?? { seen: new Set() };
|
|
@@ -881,3 +371,22 @@ function isTransforming(_schema, _ctx) {
|
|
|
881
371
|
}
|
|
882
372
|
return false;
|
|
883
373
|
}
|
|
374
|
+
/**
|
|
375
|
+
* Creates a toJSONSchema method for a schema instance.
|
|
376
|
+
* This encapsulates the logic of initializing context, processing, extracting defs, and finalizing.
|
|
377
|
+
*/
|
|
378
|
+
const createToJSONSchemaMethod = (schema, processors = {}) => (params) => {
|
|
379
|
+
const ctx = initializeContext({ ...params, processors });
|
|
380
|
+
process(schema, ctx);
|
|
381
|
+
extractDefs(ctx, schema);
|
|
382
|
+
return finalize(ctx, schema);
|
|
383
|
+
};
|
|
384
|
+
exports.createToJSONSchemaMethod = createToJSONSchemaMethod;
|
|
385
|
+
const createStandardJSONSchemaMethod = (schema, io) => (params) => {
|
|
386
|
+
const { libraryOptions, target } = params ?? {};
|
|
387
|
+
const ctx = initializeContext({ ...(libraryOptions ?? {}), target, io, processors: {} });
|
|
388
|
+
process(schema, ctx);
|
|
389
|
+
extractDefs(ctx, schema);
|
|
390
|
+
return finalize(ctx, schema);
|
|
391
|
+
};
|
|
392
|
+
exports.createStandardJSONSchemaMethod = createStandardJSONSchemaMethod;
|