surreal-zod 0.0.0-alpha.10 → 0.0.0-alpha.12
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/lib/index.d.ts +4 -3
- package/lib/index.js +4 -3
- package/lib/registries.d.ts +7 -0
- package/lib/registries.js +5 -0
- package/lib/surql.d.ts +34 -17
- package/lib/surql.js +661 -265
- package/lib/zod/core.d.ts +262 -0
- package/lib/zod/core.js +10 -0
- package/lib/zod/index.d.ts +2 -0
- package/lib/zod/index.js +2 -0
- package/lib/zod/json-schema.d.ts +89 -0
- package/lib/zod/json-schema.js +629 -0
- package/lib/zod/original.d.ts +2 -0
- package/lib/zod/original.js +177 -0
- package/lib/zod/parse.d.ts +71 -0
- package/lib/zod/parse.js +17 -0
- package/lib/zod/schema.d.ts +1798 -233
- package/lib/zod/schema.js +2255 -278
- package/lib/zod/utils.d.ts +3 -2
- package/lib/zod/utils.js +0 -2
- package/package.json +6 -8
- package/src/index.ts +4 -3
- package/src/registries.ts +9 -0
- package/src/surql.ts +768 -324
- package/src/zod/core.ts +605 -0
- package/src/zod/index.ts +2 -0
- package/src/zod/json-schema.ts +892 -0
- package/src/zod/original.ts +369 -0
- package/src/zod/parse.ts +138 -0
- package/src/zod/schema.ts +7099 -984
- package/src/zod/utils.ts +15 -5
|
@@ -0,0 +1,892 @@
|
|
|
1
|
+
import * as core from "zod/v4/core";
|
|
2
|
+
import type { $ZodSurrealType } from "./core";
|
|
3
|
+
|
|
4
|
+
const formatMap: Partial<Record<core.$ZodStringFormats, string | undefined>> = {
|
|
5
|
+
guid: "uuid",
|
|
6
|
+
url: "uri",
|
|
7
|
+
datetime: "date-time",
|
|
8
|
+
json_string: "json-string",
|
|
9
|
+
regex: "", // do not set
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// ==================== SIMPLE TYPE PROCESSORS ====================
|
|
13
|
+
|
|
14
|
+
export const stringProcessor: core.Processor<core.$ZodString> = (
|
|
15
|
+
schema,
|
|
16
|
+
ctx,
|
|
17
|
+
_json,
|
|
18
|
+
_params,
|
|
19
|
+
) => {
|
|
20
|
+
const json = _json as core.JSONSchema.StringSchema;
|
|
21
|
+
json.type = "string";
|
|
22
|
+
const { minimum, maximum, format, patterns, contentEncoding } = schema._zod
|
|
23
|
+
.bag as core.$ZodStringInternals<unknown>["bag"];
|
|
24
|
+
if (typeof minimum === "number") json.minLength = minimum;
|
|
25
|
+
if (typeof maximum === "number") json.maxLength = maximum;
|
|
26
|
+
// custom pattern overrides format
|
|
27
|
+
if (format) {
|
|
28
|
+
json.format = formatMap[format as core.$ZodStringFormats] ?? format;
|
|
29
|
+
if (json.format === "") delete json.format; // empty format is not valid
|
|
30
|
+
|
|
31
|
+
// JSON Schema format: "time" requires a full time with offset or Z
|
|
32
|
+
// z.iso.time() does not include timezone information, so format: "time" should never be used
|
|
33
|
+
if (format === "time") {
|
|
34
|
+
delete json.format;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (contentEncoding) json.contentEncoding = contentEncoding;
|
|
38
|
+
if (patterns && patterns.size > 0) {
|
|
39
|
+
const regexes = [...patterns];
|
|
40
|
+
if (regexes.length === 1) json.pattern = regexes[0]!.source;
|
|
41
|
+
else if (regexes.length > 1) {
|
|
42
|
+
json.allOf = [
|
|
43
|
+
...regexes.map((regex) => ({
|
|
44
|
+
...(ctx.target === "draft-07" ||
|
|
45
|
+
ctx.target === "draft-04" ||
|
|
46
|
+
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: core.Processor<core.$ZodNumber> = (
|
|
57
|
+
schema,
|
|
58
|
+
ctx,
|
|
59
|
+
_json,
|
|
60
|
+
_params,
|
|
61
|
+
) => {
|
|
62
|
+
const json = _json as
|
|
63
|
+
| core.JSONSchema.NumberSchema
|
|
64
|
+
| core.JSONSchema.IntegerSchema;
|
|
65
|
+
const {
|
|
66
|
+
minimum,
|
|
67
|
+
maximum,
|
|
68
|
+
format,
|
|
69
|
+
multipleOf,
|
|
70
|
+
exclusiveMaximum,
|
|
71
|
+
exclusiveMinimum,
|
|
72
|
+
} = schema._zod.bag;
|
|
73
|
+
if (typeof format === "string" && format.includes("int"))
|
|
74
|
+
json.type = "integer";
|
|
75
|
+
else json.type = "number";
|
|
76
|
+
|
|
77
|
+
if (typeof exclusiveMinimum === "number") {
|
|
78
|
+
if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
|
|
79
|
+
json.minimum = exclusiveMinimum;
|
|
80
|
+
json.exclusiveMinimum = true;
|
|
81
|
+
} else {
|
|
82
|
+
json.exclusiveMinimum = exclusiveMinimum;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (typeof minimum === "number") {
|
|
86
|
+
json.minimum = minimum;
|
|
87
|
+
if (typeof exclusiveMinimum === "number" && ctx.target !== "draft-04") {
|
|
88
|
+
if (exclusiveMinimum >= minimum) delete json.minimum;
|
|
89
|
+
else delete json.exclusiveMinimum;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (typeof exclusiveMaximum === "number") {
|
|
94
|
+
if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
|
|
95
|
+
json.maximum = exclusiveMaximum;
|
|
96
|
+
json.exclusiveMaximum = true;
|
|
97
|
+
} else {
|
|
98
|
+
json.exclusiveMaximum = exclusiveMaximum;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (typeof maximum === "number") {
|
|
102
|
+
json.maximum = maximum;
|
|
103
|
+
if (typeof exclusiveMaximum === "number" && ctx.target !== "draft-04") {
|
|
104
|
+
if (exclusiveMaximum <= maximum) delete json.maximum;
|
|
105
|
+
else delete json.exclusiveMaximum;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (typeof multipleOf === "number") json.multipleOf = multipleOf;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export const booleanProcessor: core.Processor<core.$ZodBoolean> = (
|
|
113
|
+
_schema,
|
|
114
|
+
_ctx,
|
|
115
|
+
json,
|
|
116
|
+
_params,
|
|
117
|
+
) => {
|
|
118
|
+
(json as core.JSONSchema.BooleanSchema).type = "boolean";
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export const bigintProcessor: core.Processor<core.$ZodBigInt> = (
|
|
122
|
+
_schema,
|
|
123
|
+
ctx,
|
|
124
|
+
_json,
|
|
125
|
+
_params,
|
|
126
|
+
) => {
|
|
127
|
+
if (ctx.unrepresentable === "throw") {
|
|
128
|
+
throw new Error("BigInt cannot be represented in JSON Schema");
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export const symbolProcessor: core.Processor<core.$ZodSymbol> = (
|
|
133
|
+
_schema,
|
|
134
|
+
ctx,
|
|
135
|
+
_json,
|
|
136
|
+
_params,
|
|
137
|
+
) => {
|
|
138
|
+
if (ctx.unrepresentable === "throw") {
|
|
139
|
+
throw new Error("Symbols cannot be represented in JSON Schema");
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export const nullProcessor: core.Processor<core.$ZodNull> = (
|
|
144
|
+
_schema,
|
|
145
|
+
ctx,
|
|
146
|
+
json,
|
|
147
|
+
_params,
|
|
148
|
+
) => {
|
|
149
|
+
if (ctx.target === "openapi-3.0") {
|
|
150
|
+
json.type = "string";
|
|
151
|
+
json.nullable = true;
|
|
152
|
+
json.enum = [null];
|
|
153
|
+
} else {
|
|
154
|
+
json.type = "null";
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
export const undefinedProcessor: core.Processor<core.$ZodUndefined> = (
|
|
159
|
+
_schema,
|
|
160
|
+
ctx,
|
|
161
|
+
_json,
|
|
162
|
+
_params,
|
|
163
|
+
) => {
|
|
164
|
+
if (ctx.unrepresentable === "throw") {
|
|
165
|
+
throw new Error("Undefined cannot be represented in JSON Schema");
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export const voidProcessor: core.Processor<core.$ZodVoid> = (
|
|
170
|
+
_schema,
|
|
171
|
+
ctx,
|
|
172
|
+
_json,
|
|
173
|
+
_params,
|
|
174
|
+
) => {
|
|
175
|
+
if (ctx.unrepresentable === "throw") {
|
|
176
|
+
throw new Error("Void cannot be represented in JSON Schema");
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
export const neverProcessor: core.Processor<core.$ZodNever> = (
|
|
181
|
+
_schema,
|
|
182
|
+
_ctx,
|
|
183
|
+
json,
|
|
184
|
+
_params,
|
|
185
|
+
) => {
|
|
186
|
+
json.not = {};
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
export const anyProcessor: core.Processor<core.$ZodAny> = (
|
|
190
|
+
_schema,
|
|
191
|
+
_ctx,
|
|
192
|
+
_json,
|
|
193
|
+
_params,
|
|
194
|
+
) => {
|
|
195
|
+
// empty schema accepts anything
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
export const unknownProcessor: core.Processor<core.$ZodUnknown> = (
|
|
199
|
+
_schema,
|
|
200
|
+
_ctx,
|
|
201
|
+
_json,
|
|
202
|
+
_params,
|
|
203
|
+
) => {
|
|
204
|
+
// empty schema accepts anything
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
export const dateProcessor: core.Processor<core.$ZodDate> = (
|
|
208
|
+
_schema,
|
|
209
|
+
ctx,
|
|
210
|
+
_json,
|
|
211
|
+
_params,
|
|
212
|
+
) => {
|
|
213
|
+
if (ctx.unrepresentable === "throw") {
|
|
214
|
+
throw new Error("Date cannot be represented in JSON Schema");
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
export const enumProcessor: core.Processor<core.$ZodEnum> = (
|
|
219
|
+
schema,
|
|
220
|
+
_ctx,
|
|
221
|
+
json,
|
|
222
|
+
_params,
|
|
223
|
+
) => {
|
|
224
|
+
const def = schema._zod.def as core.$ZodEnumDef;
|
|
225
|
+
const values = core.util.getEnumValues(def.entries);
|
|
226
|
+
// Number enums can have both string and number values
|
|
227
|
+
if (values.every((v) => typeof v === "number")) json.type = "number";
|
|
228
|
+
if (values.every((v) => typeof v === "string")) json.type = "string";
|
|
229
|
+
json.enum = values;
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
export const literalProcessor: core.Processor<core.$ZodLiteral> = (
|
|
233
|
+
schema,
|
|
234
|
+
ctx,
|
|
235
|
+
json,
|
|
236
|
+
_params,
|
|
237
|
+
) => {
|
|
238
|
+
const def = schema._zod.def as core.$ZodLiteralDef<any>;
|
|
239
|
+
const vals: (string | number | boolean | null)[] = [];
|
|
240
|
+
for (const val of def.values) {
|
|
241
|
+
if (val === undefined) {
|
|
242
|
+
if (ctx.unrepresentable === "throw") {
|
|
243
|
+
throw new Error(
|
|
244
|
+
"Literal `undefined` cannot be represented in JSON Schema",
|
|
245
|
+
);
|
|
246
|
+
} else {
|
|
247
|
+
// do not add to vals
|
|
248
|
+
}
|
|
249
|
+
} else if (typeof val === "bigint") {
|
|
250
|
+
if (ctx.unrepresentable === "throw") {
|
|
251
|
+
throw new Error("BigInt literals cannot be represented in JSON Schema");
|
|
252
|
+
} else {
|
|
253
|
+
vals.push(Number(val));
|
|
254
|
+
}
|
|
255
|
+
} else {
|
|
256
|
+
vals.push(val);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
if (vals.length === 0) {
|
|
260
|
+
// do nothing (an undefined literal was stripped)
|
|
261
|
+
} else if (vals.length === 1) {
|
|
262
|
+
const val = vals[0]!;
|
|
263
|
+
json.type = val === null ? ("null" as const) : (typeof val as any);
|
|
264
|
+
if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
|
|
265
|
+
json.enum = [val];
|
|
266
|
+
} else {
|
|
267
|
+
json.const = val;
|
|
268
|
+
}
|
|
269
|
+
} else {
|
|
270
|
+
if (vals.every((v) => typeof v === "number")) json.type = "number";
|
|
271
|
+
if (vals.every((v) => typeof v === "string")) json.type = "string";
|
|
272
|
+
if (vals.every((v) => typeof v === "boolean")) json.type = "boolean";
|
|
273
|
+
if (vals.every((v) => v === null)) json.type = "null";
|
|
274
|
+
json.enum = vals;
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
export const nanProcessor: core.Processor<core.$ZodNaN> = (
|
|
279
|
+
_schema,
|
|
280
|
+
ctx,
|
|
281
|
+
_json,
|
|
282
|
+
_params,
|
|
283
|
+
) => {
|
|
284
|
+
if (ctx.unrepresentable === "throw") {
|
|
285
|
+
throw new Error("NaN cannot be represented in JSON Schema");
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
export const templateLiteralProcessor: core.Processor<
|
|
290
|
+
core.$ZodTemplateLiteral
|
|
291
|
+
> = (schema, _ctx, json, _params) => {
|
|
292
|
+
const _json = json as core.JSONSchema.StringSchema;
|
|
293
|
+
const pattern = schema._zod.pattern;
|
|
294
|
+
if (!pattern) throw new Error("Pattern not found in template literal");
|
|
295
|
+
_json.type = "string";
|
|
296
|
+
_json.pattern = pattern.source;
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
export const fileProcessor: core.Processor<core.$ZodFile> = (
|
|
300
|
+
schema,
|
|
301
|
+
_ctx,
|
|
302
|
+
json,
|
|
303
|
+
_params,
|
|
304
|
+
) => {
|
|
305
|
+
const _json = json as core.JSONSchema.StringSchema;
|
|
306
|
+
const file: core.JSONSchema.StringSchema = {
|
|
307
|
+
type: "string",
|
|
308
|
+
format: "binary",
|
|
309
|
+
contentEncoding: "binary",
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const { minimum, maximum, mime } = schema._zod
|
|
313
|
+
.bag as core.$ZodFileInternals["bag"];
|
|
314
|
+
if (minimum !== undefined) file.minLength = minimum;
|
|
315
|
+
if (maximum !== undefined) file.maxLength = maximum;
|
|
316
|
+
if (mime) {
|
|
317
|
+
if (mime.length === 1) {
|
|
318
|
+
file.contentMediaType = mime[0]!;
|
|
319
|
+
Object.assign(_json, file);
|
|
320
|
+
} else {
|
|
321
|
+
Object.assign(_json, file); // shared props at root
|
|
322
|
+
_json.anyOf = mime.map((m) => ({ contentMediaType: m })); // only contentMediaType differs
|
|
323
|
+
}
|
|
324
|
+
} else {
|
|
325
|
+
Object.assign(_json, file);
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
export const successProcessor: core.Processor<core.$ZodSuccess> = (
|
|
330
|
+
_schema,
|
|
331
|
+
_ctx,
|
|
332
|
+
json,
|
|
333
|
+
_params,
|
|
334
|
+
) => {
|
|
335
|
+
(json as core.JSONSchema.BooleanSchema).type = "boolean";
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
export const customProcessor: core.Processor<core.$ZodCustom> = (
|
|
339
|
+
_schema,
|
|
340
|
+
ctx,
|
|
341
|
+
_json,
|
|
342
|
+
_params,
|
|
343
|
+
) => {
|
|
344
|
+
if (ctx.unrepresentable === "throw") {
|
|
345
|
+
throw new Error("Custom types cannot be represented in JSON Schema");
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
export const functionProcessor: core.Processor<core.$ZodFunction> = (
|
|
350
|
+
_schema,
|
|
351
|
+
ctx,
|
|
352
|
+
_json,
|
|
353
|
+
_params,
|
|
354
|
+
) => {
|
|
355
|
+
if (ctx.unrepresentable === "throw") {
|
|
356
|
+
throw new Error("Function types cannot be represented in JSON Schema");
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
export const transformProcessor: core.Processor<core.$ZodTransform> = (
|
|
361
|
+
_schema,
|
|
362
|
+
ctx,
|
|
363
|
+
_json,
|
|
364
|
+
_params,
|
|
365
|
+
) => {
|
|
366
|
+
if (ctx.unrepresentable === "throw") {
|
|
367
|
+
throw new Error("Transforms cannot be represented in JSON Schema");
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
export const mapProcessor: core.Processor<core.$ZodMap> = (
|
|
372
|
+
_schema,
|
|
373
|
+
ctx,
|
|
374
|
+
_json,
|
|
375
|
+
_params,
|
|
376
|
+
) => {
|
|
377
|
+
if (ctx.unrepresentable === "throw") {
|
|
378
|
+
throw new Error("Map cannot be represented in JSON Schema");
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
export const setProcessor: core.Processor<core.$ZodSet> = (
|
|
383
|
+
_schema,
|
|
384
|
+
ctx,
|
|
385
|
+
_json,
|
|
386
|
+
_params,
|
|
387
|
+
) => {
|
|
388
|
+
if (ctx.unrepresentable === "throw") {
|
|
389
|
+
throw new Error("Set cannot be represented in JSON Schema");
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
// ==================== COMPOSITE TYPE PROCESSORS ====================
|
|
394
|
+
|
|
395
|
+
export const arrayProcessor: core.Processor<core.$ZodArray> = (
|
|
396
|
+
schema,
|
|
397
|
+
ctx,
|
|
398
|
+
_json,
|
|
399
|
+
params,
|
|
400
|
+
) => {
|
|
401
|
+
const json = _json as core.JSONSchema.ArraySchema;
|
|
402
|
+
const def = schema._zod.def as core.$ZodArrayDef;
|
|
403
|
+
const { minimum, maximum } = schema._zod.bag;
|
|
404
|
+
if (typeof minimum === "number") json.minItems = minimum;
|
|
405
|
+
if (typeof maximum === "number") json.maxItems = maximum;
|
|
406
|
+
|
|
407
|
+
json.type = "array";
|
|
408
|
+
json.items = core.process(def.element, ctx as any, {
|
|
409
|
+
...params,
|
|
410
|
+
path: [...params.path, "items"],
|
|
411
|
+
});
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
export const objectProcessor: core.Processor<core.$ZodObject> = (
|
|
415
|
+
schema,
|
|
416
|
+
ctx,
|
|
417
|
+
_json,
|
|
418
|
+
params,
|
|
419
|
+
) => {
|
|
420
|
+
const json = _json as core.JSONSchema.ObjectSchema;
|
|
421
|
+
const def = schema._zod.def as core.$ZodObjectDef;
|
|
422
|
+
json.type = "object";
|
|
423
|
+
json.properties = {};
|
|
424
|
+
const shape = def.shape;
|
|
425
|
+
|
|
426
|
+
for (const key in shape) {
|
|
427
|
+
json.properties[key] = core.process(shape[key]!, ctx as any, {
|
|
428
|
+
...params,
|
|
429
|
+
path: [...params.path, "properties", key],
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// required keys
|
|
434
|
+
const allKeys = new Set(Object.keys(shape));
|
|
435
|
+
const requiredKeys = new Set(
|
|
436
|
+
[...allKeys].filter((key) => {
|
|
437
|
+
const v = def.shape[key]!._zod;
|
|
438
|
+
if (ctx.io === "input") {
|
|
439
|
+
return v.optin === undefined;
|
|
440
|
+
} else {
|
|
441
|
+
return v.optout === undefined;
|
|
442
|
+
}
|
|
443
|
+
}),
|
|
444
|
+
);
|
|
445
|
+
|
|
446
|
+
if (requiredKeys.size > 0) {
|
|
447
|
+
json.required = Array.from(requiredKeys);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// catchall
|
|
451
|
+
if (def.catchall?._zod.def.type === "never") {
|
|
452
|
+
// strict
|
|
453
|
+
json.additionalProperties = false;
|
|
454
|
+
} else if (!def.catchall) {
|
|
455
|
+
// regular
|
|
456
|
+
if (ctx.io === "output") json.additionalProperties = false;
|
|
457
|
+
} else if (def.catchall) {
|
|
458
|
+
json.additionalProperties = core.process(def.catchall, ctx as any, {
|
|
459
|
+
...params,
|
|
460
|
+
path: [...params.path, "additionalProperties"],
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
export const unionProcessor: core.Processor<core.$ZodUnion> = (
|
|
466
|
+
schema,
|
|
467
|
+
ctx,
|
|
468
|
+
json,
|
|
469
|
+
params,
|
|
470
|
+
) => {
|
|
471
|
+
const def = schema._zod.def as core.$ZodUnionDef;
|
|
472
|
+
// Exclusive unions (inclusive === false) use oneOf (exactly one match) instead of anyOf (one or more matches)
|
|
473
|
+
// This includes both z.xor() and discriminated unions
|
|
474
|
+
const isExclusive = def.inclusive === false;
|
|
475
|
+
const options = def.options.map((x, i) =>
|
|
476
|
+
core.process(x, ctx as any, {
|
|
477
|
+
...params,
|
|
478
|
+
path: [...params.path, isExclusive ? "oneOf" : "anyOf", i],
|
|
479
|
+
}),
|
|
480
|
+
);
|
|
481
|
+
if (isExclusive) {
|
|
482
|
+
json.oneOf = options;
|
|
483
|
+
} else {
|
|
484
|
+
json.anyOf = options;
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
export const intersectionProcessor: core.Processor<core.$ZodIntersection> = (
|
|
489
|
+
schema,
|
|
490
|
+
ctx,
|
|
491
|
+
json,
|
|
492
|
+
params,
|
|
493
|
+
) => {
|
|
494
|
+
const def = schema._zod.def as core.$ZodIntersectionDef;
|
|
495
|
+
const a = core.process(def.left, ctx as any, {
|
|
496
|
+
...params,
|
|
497
|
+
path: [...params.path, "allOf", 0],
|
|
498
|
+
});
|
|
499
|
+
const b = core.process(def.right, ctx as any, {
|
|
500
|
+
...params,
|
|
501
|
+
path: [...params.path, "allOf", 1],
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
const isSimpleIntersection = (val: any) =>
|
|
505
|
+
"allOf" in val && Object.keys(val).length === 1;
|
|
506
|
+
const allOf = [
|
|
507
|
+
...(isSimpleIntersection(a) ? (a.allOf as any[]) : [a]),
|
|
508
|
+
...(isSimpleIntersection(b) ? (b.allOf as any[]) : [b]),
|
|
509
|
+
];
|
|
510
|
+
json.allOf = allOf;
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
export const tupleProcessor: core.Processor<core.$ZodTuple> = (
|
|
514
|
+
schema,
|
|
515
|
+
ctx,
|
|
516
|
+
_json,
|
|
517
|
+
params,
|
|
518
|
+
) => {
|
|
519
|
+
const json = _json as core.JSONSchema.ArraySchema;
|
|
520
|
+
const def = schema._zod.def as core.$ZodTupleDef;
|
|
521
|
+
json.type = "array";
|
|
522
|
+
|
|
523
|
+
const prefixPath = ctx.target === "draft-2020-12" ? "prefixItems" : "items";
|
|
524
|
+
const restPath =
|
|
525
|
+
ctx.target === "draft-2020-12"
|
|
526
|
+
? "items"
|
|
527
|
+
: ctx.target === "openapi-3.0"
|
|
528
|
+
? "items"
|
|
529
|
+
: "additionalItems";
|
|
530
|
+
|
|
531
|
+
const prefixItems = def.items.map((x, i) =>
|
|
532
|
+
core.process(x, ctx as any, {
|
|
533
|
+
...params,
|
|
534
|
+
path: [...params.path, prefixPath, i],
|
|
535
|
+
}),
|
|
536
|
+
);
|
|
537
|
+
const rest = def.rest
|
|
538
|
+
? core.process(def.rest, ctx as any, {
|
|
539
|
+
...params,
|
|
540
|
+
path: [
|
|
541
|
+
...params.path,
|
|
542
|
+
restPath,
|
|
543
|
+
...(ctx.target === "openapi-3.0" ? [def.items.length] : []),
|
|
544
|
+
],
|
|
545
|
+
})
|
|
546
|
+
: null;
|
|
547
|
+
|
|
548
|
+
if (ctx.target === "draft-2020-12") {
|
|
549
|
+
json.prefixItems = prefixItems;
|
|
550
|
+
if (rest) {
|
|
551
|
+
json.items = rest;
|
|
552
|
+
}
|
|
553
|
+
} else if (ctx.target === "openapi-3.0") {
|
|
554
|
+
json.items = {
|
|
555
|
+
anyOf: prefixItems,
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
if (rest) {
|
|
559
|
+
json.items.anyOf!.push(rest);
|
|
560
|
+
}
|
|
561
|
+
json.minItems = prefixItems.length;
|
|
562
|
+
if (!rest) {
|
|
563
|
+
json.maxItems = prefixItems.length;
|
|
564
|
+
}
|
|
565
|
+
} else {
|
|
566
|
+
json.items = prefixItems;
|
|
567
|
+
if (rest) {
|
|
568
|
+
json.additionalItems = rest;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// length
|
|
573
|
+
const { minimum, maximum } = schema._zod.bag as {
|
|
574
|
+
minimum?: number;
|
|
575
|
+
maximum?: number;
|
|
576
|
+
};
|
|
577
|
+
if (typeof minimum === "number") json.minItems = minimum;
|
|
578
|
+
if (typeof maximum === "number") json.maxItems = maximum;
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
export const recordProcessor: core.Processor<core.$ZodRecord> = (
|
|
582
|
+
schema,
|
|
583
|
+
ctx,
|
|
584
|
+
_json,
|
|
585
|
+
params,
|
|
586
|
+
) => {
|
|
587
|
+
const json = _json as core.JSONSchema.ObjectSchema;
|
|
588
|
+
const def = schema._zod.def as core.$ZodRecordDef;
|
|
589
|
+
json.type = "object";
|
|
590
|
+
|
|
591
|
+
// For looseRecord with regex patterns, use patternProperties
|
|
592
|
+
// This correctly represents "only validate keys matching the pattern" semantics
|
|
593
|
+
// and composes well with allOf (intersections)
|
|
594
|
+
const keyType = def.keyType as core.$ZodTypes;
|
|
595
|
+
const keyBag = keyType._zod.bag as
|
|
596
|
+
| core.$ZodStringInternals<unknown>["bag"]
|
|
597
|
+
| undefined;
|
|
598
|
+
const patterns = keyBag?.patterns;
|
|
599
|
+
|
|
600
|
+
if (def.mode === "loose" && patterns && patterns.size > 0) {
|
|
601
|
+
// Use patternProperties for looseRecord with regex patterns
|
|
602
|
+
const valueSchema = core.process(def.valueType, ctx as any, {
|
|
603
|
+
...params,
|
|
604
|
+
path: [...params.path, "patternProperties", "*"],
|
|
605
|
+
});
|
|
606
|
+
json.patternProperties = {};
|
|
607
|
+
for (const pattern of patterns) {
|
|
608
|
+
json.patternProperties[pattern.source] = valueSchema;
|
|
609
|
+
}
|
|
610
|
+
} else {
|
|
611
|
+
// Default behavior: use propertyNames + additionalProperties
|
|
612
|
+
if (ctx.target === "draft-07" || ctx.target === "draft-2020-12") {
|
|
613
|
+
json.propertyNames = core.process(def.keyType, ctx as any, {
|
|
614
|
+
...params,
|
|
615
|
+
path: [...params.path, "propertyNames"],
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
json.additionalProperties = core.process(def.valueType, ctx as any, {
|
|
619
|
+
...params,
|
|
620
|
+
path: [...params.path, "additionalProperties"],
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// Add required for keys with discrete values (enum, literal, etc.)
|
|
625
|
+
const keyValues = keyType._zod.values;
|
|
626
|
+
if (keyValues) {
|
|
627
|
+
const validKeyValues = [...keyValues].filter(
|
|
628
|
+
(v): v is string | number =>
|
|
629
|
+
typeof v === "string" || typeof v === "number",
|
|
630
|
+
);
|
|
631
|
+
|
|
632
|
+
if (validKeyValues.length > 0) {
|
|
633
|
+
json.required = validKeyValues as string[];
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
export const nullableProcessor: core.Processor<core.$ZodNullable> = (
|
|
639
|
+
schema,
|
|
640
|
+
ctx,
|
|
641
|
+
json,
|
|
642
|
+
params,
|
|
643
|
+
) => {
|
|
644
|
+
const def = schema._zod.def as core.$ZodNullableDef;
|
|
645
|
+
const inner = core.process(def.innerType, ctx as any, params);
|
|
646
|
+
const seen = ctx.seen.get(schema)!;
|
|
647
|
+
if (ctx.target === "openapi-3.0") {
|
|
648
|
+
seen.ref = def.innerType;
|
|
649
|
+
json.nullable = true;
|
|
650
|
+
} else {
|
|
651
|
+
json.anyOf = [inner, { type: "null" }];
|
|
652
|
+
}
|
|
653
|
+
};
|
|
654
|
+
|
|
655
|
+
export const nonoptionalProcessor: core.Processor<core.$ZodNonOptional> = (
|
|
656
|
+
schema,
|
|
657
|
+
ctx,
|
|
658
|
+
_json,
|
|
659
|
+
params,
|
|
660
|
+
) => {
|
|
661
|
+
const def = schema._zod.def as core.$ZodNonOptionalDef;
|
|
662
|
+
core.process(def.innerType, ctx as any, params);
|
|
663
|
+
const seen = ctx.seen.get(schema)!;
|
|
664
|
+
seen.ref = def.innerType;
|
|
665
|
+
};
|
|
666
|
+
|
|
667
|
+
export const defaultProcessor: core.Processor<core.$ZodDefault> = (
|
|
668
|
+
schema,
|
|
669
|
+
ctx,
|
|
670
|
+
json,
|
|
671
|
+
params,
|
|
672
|
+
) => {
|
|
673
|
+
const def = schema._zod.def as core.$ZodDefaultDef;
|
|
674
|
+
core.process(def.innerType, ctx as any, params);
|
|
675
|
+
const seen = ctx.seen.get(schema)!;
|
|
676
|
+
seen.ref = def.innerType;
|
|
677
|
+
json.default = JSON.parse(JSON.stringify(def.defaultValue));
|
|
678
|
+
};
|
|
679
|
+
|
|
680
|
+
export const prefaultProcessor: core.Processor<core.$ZodPrefault> = (
|
|
681
|
+
schema,
|
|
682
|
+
ctx,
|
|
683
|
+
json,
|
|
684
|
+
params,
|
|
685
|
+
) => {
|
|
686
|
+
const def = schema._zod.def as core.$ZodPrefaultDef;
|
|
687
|
+
core.process(def.innerType, ctx as any, params);
|
|
688
|
+
const seen = ctx.seen.get(schema)!;
|
|
689
|
+
seen.ref = def.innerType;
|
|
690
|
+
if (ctx.io === "input")
|
|
691
|
+
json._prefault = JSON.parse(JSON.stringify(def.defaultValue));
|
|
692
|
+
};
|
|
693
|
+
|
|
694
|
+
export const catchProcessor: core.Processor<core.$ZodCatch> = (
|
|
695
|
+
schema,
|
|
696
|
+
ctx,
|
|
697
|
+
json,
|
|
698
|
+
params,
|
|
699
|
+
) => {
|
|
700
|
+
const def = schema._zod.def as core.$ZodCatchDef;
|
|
701
|
+
core.process(def.innerType, ctx as any, params);
|
|
702
|
+
const seen = ctx.seen.get(schema)!;
|
|
703
|
+
seen.ref = def.innerType;
|
|
704
|
+
let catchValue: any;
|
|
705
|
+
try {
|
|
706
|
+
catchValue = def.catchValue(undefined as any);
|
|
707
|
+
} catch {
|
|
708
|
+
throw new Error("Dynamic catch values are not supported in JSON Schema");
|
|
709
|
+
}
|
|
710
|
+
json.default = catchValue;
|
|
711
|
+
};
|
|
712
|
+
|
|
713
|
+
export const pipeProcessor: core.Processor<core.$ZodPipe> = (
|
|
714
|
+
schema,
|
|
715
|
+
ctx,
|
|
716
|
+
_json,
|
|
717
|
+
params,
|
|
718
|
+
) => {
|
|
719
|
+
const def = schema._zod.def as core.$ZodPipeDef;
|
|
720
|
+
const innerType =
|
|
721
|
+
ctx.io === "input"
|
|
722
|
+
? def.in._zod.def.type === "transform"
|
|
723
|
+
? def.out
|
|
724
|
+
: def.in
|
|
725
|
+
: def.out;
|
|
726
|
+
core.process(innerType, ctx as any, params);
|
|
727
|
+
const seen = ctx.seen.get(schema)!;
|
|
728
|
+
seen.ref = innerType;
|
|
729
|
+
};
|
|
730
|
+
|
|
731
|
+
export const readonlyProcessor: core.Processor<core.$ZodReadonly> = (
|
|
732
|
+
schema,
|
|
733
|
+
ctx,
|
|
734
|
+
json,
|
|
735
|
+
params,
|
|
736
|
+
) => {
|
|
737
|
+
const def = schema._zod.def as core.$ZodReadonlyDef;
|
|
738
|
+
core.process(def.innerType, ctx as any, params);
|
|
739
|
+
const seen = ctx.seen.get(schema)!;
|
|
740
|
+
seen.ref = def.innerType;
|
|
741
|
+
json.readOnly = true;
|
|
742
|
+
};
|
|
743
|
+
|
|
744
|
+
export const promiseProcessor: core.Processor<core.$ZodPromise> = (
|
|
745
|
+
schema,
|
|
746
|
+
ctx,
|
|
747
|
+
_json,
|
|
748
|
+
params,
|
|
749
|
+
) => {
|
|
750
|
+
const def = schema._zod.def as core.$ZodPromiseDef;
|
|
751
|
+
core.process(def.innerType, ctx as any, params);
|
|
752
|
+
const seen = ctx.seen.get(schema)!;
|
|
753
|
+
seen.ref = def.innerType;
|
|
754
|
+
};
|
|
755
|
+
|
|
756
|
+
export const optionalProcessor: core.Processor<core.$ZodOptional> = (
|
|
757
|
+
schema,
|
|
758
|
+
ctx,
|
|
759
|
+
_json,
|
|
760
|
+
params,
|
|
761
|
+
) => {
|
|
762
|
+
const def = schema._zod.def as core.$ZodOptionalDef;
|
|
763
|
+
core.process(def.innerType, ctx as any, params);
|
|
764
|
+
const seen = ctx.seen.get(schema)!;
|
|
765
|
+
seen.ref = def.innerType;
|
|
766
|
+
};
|
|
767
|
+
|
|
768
|
+
export const lazyProcessor: core.Processor<core.$ZodLazy> = (
|
|
769
|
+
schema,
|
|
770
|
+
ctx,
|
|
771
|
+
_json,
|
|
772
|
+
params,
|
|
773
|
+
) => {
|
|
774
|
+
const innerType = (schema as core.$ZodLazy)._zod.innerType;
|
|
775
|
+
core.process(innerType, ctx as any, params);
|
|
776
|
+
const seen = ctx.seen.get(schema)!;
|
|
777
|
+
seen.ref = innerType;
|
|
778
|
+
};
|
|
779
|
+
|
|
780
|
+
// ==================== ALL PROCESSORS ====================
|
|
781
|
+
|
|
782
|
+
export const allProcessors = {
|
|
783
|
+
string: stringProcessor,
|
|
784
|
+
number: numberProcessor,
|
|
785
|
+
boolean: booleanProcessor,
|
|
786
|
+
bigint: bigintProcessor,
|
|
787
|
+
symbol: symbolProcessor,
|
|
788
|
+
null: nullProcessor,
|
|
789
|
+
undefined: undefinedProcessor,
|
|
790
|
+
void: voidProcessor,
|
|
791
|
+
never: neverProcessor,
|
|
792
|
+
any: anyProcessor,
|
|
793
|
+
unknown: unknownProcessor,
|
|
794
|
+
date: dateProcessor,
|
|
795
|
+
enum: enumProcessor,
|
|
796
|
+
literal: literalProcessor,
|
|
797
|
+
nan: nanProcessor,
|
|
798
|
+
template_literal: templateLiteralProcessor,
|
|
799
|
+
file: fileProcessor,
|
|
800
|
+
success: successProcessor,
|
|
801
|
+
custom: customProcessor,
|
|
802
|
+
function: functionProcessor,
|
|
803
|
+
transform: transformProcessor,
|
|
804
|
+
map: mapProcessor,
|
|
805
|
+
set: setProcessor,
|
|
806
|
+
array: arrayProcessor,
|
|
807
|
+
object: objectProcessor,
|
|
808
|
+
union: unionProcessor,
|
|
809
|
+
intersection: intersectionProcessor,
|
|
810
|
+
tuple: tupleProcessor,
|
|
811
|
+
record: recordProcessor,
|
|
812
|
+
nullable: nullableProcessor,
|
|
813
|
+
nonoptional: nonoptionalProcessor,
|
|
814
|
+
default: defaultProcessor,
|
|
815
|
+
prefault: prefaultProcessor,
|
|
816
|
+
catch: catchProcessor,
|
|
817
|
+
pipe: pipeProcessor,
|
|
818
|
+
readonly: readonlyProcessor,
|
|
819
|
+
promise: promiseProcessor,
|
|
820
|
+
optional: optionalProcessor,
|
|
821
|
+
lazy: lazyProcessor,
|
|
822
|
+
};
|
|
823
|
+
|
|
824
|
+
// ==================== TOP-LEVEL toJSONSchema ====================
|
|
825
|
+
|
|
826
|
+
export function toJSONSchema<T extends core.$ZodType>(
|
|
827
|
+
schema: T,
|
|
828
|
+
params?: core.ToJSONSchemaParams,
|
|
829
|
+
): core.ZodStandardJSONSchemaPayload<T>;
|
|
830
|
+
export function toJSONSchema(
|
|
831
|
+
registry: core.$ZodRegistry<{ id?: string | undefined }>,
|
|
832
|
+
params?: core.RegistryToJSONSchemaParams,
|
|
833
|
+
): {
|
|
834
|
+
schemas: Record<string, core.ZodStandardJSONSchemaPayload<core.$ZodType>>;
|
|
835
|
+
};
|
|
836
|
+
export function toJSONSchema(
|
|
837
|
+
input: core.$ZodType | core.$ZodRegistry<{ id?: string | undefined }>,
|
|
838
|
+
params?: core.ToJSONSchemaParams | core.RegistryToJSONSchemaParams,
|
|
839
|
+
): any {
|
|
840
|
+
if ("_idmap" in input) {
|
|
841
|
+
// Registry case
|
|
842
|
+
const registry = input as core.$ZodRegistry<{ id?: string | undefined }>;
|
|
843
|
+
const ctx = core.initializeContext({
|
|
844
|
+
...params,
|
|
845
|
+
processors: allProcessors as any,
|
|
846
|
+
});
|
|
847
|
+
const defs: any = {};
|
|
848
|
+
|
|
849
|
+
// First pass: process all schemas to build the seen map
|
|
850
|
+
for (const entry of registry._idmap.entries()) {
|
|
851
|
+
const [_, schema] = entry;
|
|
852
|
+
core.process(schema, ctx as any);
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
const schemas: Record<string, core.JSONSchema.BaseSchema> = {};
|
|
856
|
+
const external = {
|
|
857
|
+
registry,
|
|
858
|
+
uri: (params as core.RegistryToJSONSchemaParams)?.uri,
|
|
859
|
+
defs,
|
|
860
|
+
};
|
|
861
|
+
|
|
862
|
+
// Update the context with external configuration
|
|
863
|
+
ctx.external = external;
|
|
864
|
+
|
|
865
|
+
// Second pass: emit each schema
|
|
866
|
+
for (const entry of registry._idmap.entries()) {
|
|
867
|
+
const [key, schema] = entry;
|
|
868
|
+
core.extractDefs(ctx as any, schema);
|
|
869
|
+
schemas[key] = core.finalize(ctx as any, schema);
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
if (Object.keys(defs).length > 0) {
|
|
873
|
+
const defsSegment =
|
|
874
|
+
ctx.target === "draft-2020-12" ? "$defs" : "definitions";
|
|
875
|
+
// @ts-expect-error
|
|
876
|
+
core.__shared = {
|
|
877
|
+
[defsSegment]: defs,
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
return { schemas };
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
// Single schema case
|
|
885
|
+
const ctx = core.initializeContext({
|
|
886
|
+
...params,
|
|
887
|
+
processors: allProcessors as any,
|
|
888
|
+
});
|
|
889
|
+
core.process(input, ctx as any);
|
|
890
|
+
core.extractDefs(ctx as any, input);
|
|
891
|
+
return core.finalize(ctx as any, input);
|
|
892
|
+
}
|