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