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