surreal-zod 0.0.0-alpha.11 → 0.0.0-alpha.13
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 +257 -0
- package/lib/zod/core.js +10 -0
- package/lib/zod/external.d.ts +2 -0
- package/lib/zod/external.js +2 -0
- package/lib/zod/index.d.ts +3 -0
- package/lib/zod/index.js +3 -0
- package/lib/zod/json-schema.d.ts +89 -0
- package/lib/zod/json-schema.js +628 -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 +2254 -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 +596 -0
- package/src/zod/external.ts +8 -0
- package/src/zod/index.ts +3 -0
- package/src/zod/json-schema.ts +891 -0
- package/src/zod/original.ts +369 -0
- package/src/zod/parse.ts +138 -0
- package/src/zod/schema.ts +7098 -984
- package/src/zod/utils.ts +15 -5
|
@@ -0,0 +1,628 @@
|
|
|
1
|
+
import * as core from "zod/v4/core";
|
|
2
|
+
const formatMap = {
|
|
3
|
+
guid: "uuid",
|
|
4
|
+
url: "uri",
|
|
5
|
+
datetime: "date-time",
|
|
6
|
+
json_string: "json-string",
|
|
7
|
+
regex: "", // do not set
|
|
8
|
+
};
|
|
9
|
+
// ==================== SIMPLE TYPE PROCESSORS ====================
|
|
10
|
+
export const stringProcessor = (schema, ctx, _json, _params) => {
|
|
11
|
+
const json = _json;
|
|
12
|
+
json.type = "string";
|
|
13
|
+
const { minimum, maximum, format, patterns, contentEncoding } = schema._zod
|
|
14
|
+
.bag;
|
|
15
|
+
if (typeof minimum === "number")
|
|
16
|
+
json.minLength = minimum;
|
|
17
|
+
if (typeof maximum === "number")
|
|
18
|
+
json.maxLength = maximum;
|
|
19
|
+
// custom pattern overrides format
|
|
20
|
+
if (format) {
|
|
21
|
+
json.format = formatMap[format] ?? format;
|
|
22
|
+
if (json.format === "")
|
|
23
|
+
delete json.format; // empty format is not valid
|
|
24
|
+
// JSON Schema format: "time" requires a full time with offset or Z
|
|
25
|
+
// z.iso.time() does not include timezone information, so format: "time" should never be used
|
|
26
|
+
if (format === "time") {
|
|
27
|
+
delete json.format;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (contentEncoding)
|
|
31
|
+
json.contentEncoding = contentEncoding;
|
|
32
|
+
if (patterns && patterns.size > 0) {
|
|
33
|
+
const regexes = [...patterns];
|
|
34
|
+
if (regexes.length === 1)
|
|
35
|
+
json.pattern = regexes[0].source;
|
|
36
|
+
else if (regexes.length > 1) {
|
|
37
|
+
json.allOf = [
|
|
38
|
+
...regexes.map((regex) => ({
|
|
39
|
+
...(ctx.target === "draft-07" ||
|
|
40
|
+
ctx.target === "draft-04" ||
|
|
41
|
+
ctx.target === "openapi-3.0"
|
|
42
|
+
? { type: "string" }
|
|
43
|
+
: {}),
|
|
44
|
+
pattern: regex.source,
|
|
45
|
+
})),
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
export const numberProcessor = (schema, ctx, _json, _params) => {
|
|
51
|
+
const json = _json;
|
|
52
|
+
const { minimum, maximum, format, multipleOf, exclusiveMaximum, exclusiveMinimum, } = schema._zod.bag;
|
|
53
|
+
if (typeof format === "string" && format.includes("int"))
|
|
54
|
+
json.type = "integer";
|
|
55
|
+
else
|
|
56
|
+
json.type = "number";
|
|
57
|
+
if (typeof exclusiveMinimum === "number") {
|
|
58
|
+
if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
|
|
59
|
+
json.minimum = exclusiveMinimum;
|
|
60
|
+
json.exclusiveMinimum = true;
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
json.exclusiveMinimum = exclusiveMinimum;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (typeof minimum === "number") {
|
|
67
|
+
json.minimum = minimum;
|
|
68
|
+
if (typeof exclusiveMinimum === "number" && ctx.target !== "draft-04") {
|
|
69
|
+
if (exclusiveMinimum >= minimum)
|
|
70
|
+
delete json.minimum;
|
|
71
|
+
else
|
|
72
|
+
delete json.exclusiveMinimum;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (typeof exclusiveMaximum === "number") {
|
|
76
|
+
if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
|
|
77
|
+
json.maximum = exclusiveMaximum;
|
|
78
|
+
json.exclusiveMaximum = true;
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
json.exclusiveMaximum = exclusiveMaximum;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (typeof maximum === "number") {
|
|
85
|
+
json.maximum = maximum;
|
|
86
|
+
if (typeof exclusiveMaximum === "number" && ctx.target !== "draft-04") {
|
|
87
|
+
if (exclusiveMaximum <= maximum)
|
|
88
|
+
delete json.maximum;
|
|
89
|
+
else
|
|
90
|
+
delete json.exclusiveMaximum;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (typeof multipleOf === "number")
|
|
94
|
+
json.multipleOf = multipleOf;
|
|
95
|
+
};
|
|
96
|
+
export const booleanProcessor = (_schema, _ctx, json, _params) => {
|
|
97
|
+
json.type = "boolean";
|
|
98
|
+
};
|
|
99
|
+
export const bigintProcessor = (_schema, ctx, _json, _params) => {
|
|
100
|
+
if (ctx.unrepresentable === "throw") {
|
|
101
|
+
throw new Error("BigInt cannot be represented in JSON Schema");
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
export const symbolProcessor = (_schema, ctx, _json, _params) => {
|
|
105
|
+
if (ctx.unrepresentable === "throw") {
|
|
106
|
+
throw new Error("Symbols cannot be represented in JSON Schema");
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
export const nullProcessor = (_schema, ctx, json, _params) => {
|
|
110
|
+
if (ctx.target === "openapi-3.0") {
|
|
111
|
+
json.type = "string";
|
|
112
|
+
json.nullable = true;
|
|
113
|
+
json.enum = [null];
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
json.type = "null";
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
export const undefinedProcessor = (_schema, ctx, _json, _params) => {
|
|
120
|
+
if (ctx.unrepresentable === "throw") {
|
|
121
|
+
throw new Error("Undefined cannot be represented in JSON Schema");
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
export const voidProcessor = (_schema, ctx, _json, _params) => {
|
|
125
|
+
if (ctx.unrepresentable === "throw") {
|
|
126
|
+
throw new Error("Void cannot be represented in JSON Schema");
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
export const neverProcessor = (_schema, _ctx, json, _params) => {
|
|
130
|
+
json.not = {};
|
|
131
|
+
};
|
|
132
|
+
export const anyProcessor = (_schema, _ctx, _json, _params) => {
|
|
133
|
+
// empty schema accepts anything
|
|
134
|
+
};
|
|
135
|
+
export const unknownProcessor = (_schema, _ctx, _json, _params) => {
|
|
136
|
+
// empty schema accepts anything
|
|
137
|
+
};
|
|
138
|
+
export const dateProcessor = (_schema, ctx, _json, _params) => {
|
|
139
|
+
if (ctx.unrepresentable === "throw") {
|
|
140
|
+
throw new Error("Date cannot be represented in JSON Schema");
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
export const enumProcessor = (schema, _ctx, json, _params) => {
|
|
144
|
+
const def = schema._zod.def;
|
|
145
|
+
const values = core.util.getEnumValues(def.entries);
|
|
146
|
+
// Number enums can have both string and number values
|
|
147
|
+
if (values.every((v) => typeof v === "number"))
|
|
148
|
+
json.type = "number";
|
|
149
|
+
if (values.every((v) => typeof v === "string"))
|
|
150
|
+
json.type = "string";
|
|
151
|
+
json.enum = values;
|
|
152
|
+
};
|
|
153
|
+
export const literalProcessor = (schema, ctx, json, _params) => {
|
|
154
|
+
const def = schema._zod.def;
|
|
155
|
+
const vals = [];
|
|
156
|
+
for (const val of def.values) {
|
|
157
|
+
if (val === undefined) {
|
|
158
|
+
if (ctx.unrepresentable === "throw") {
|
|
159
|
+
throw new Error("Literal `undefined` cannot be represented in JSON Schema");
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
// do not add to vals
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
else if (typeof val === "bigint") {
|
|
166
|
+
if (ctx.unrepresentable === "throw") {
|
|
167
|
+
throw new Error("BigInt literals cannot be represented in JSON Schema");
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
vals.push(Number(val));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
vals.push(val);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (vals.length === 0) {
|
|
178
|
+
// do nothing (an undefined literal was stripped)
|
|
179
|
+
}
|
|
180
|
+
else if (vals.length === 1) {
|
|
181
|
+
const val = vals[0];
|
|
182
|
+
json.type = val === null ? "null" : typeof val;
|
|
183
|
+
if (ctx.target === "draft-04" || ctx.target === "openapi-3.0") {
|
|
184
|
+
json.enum = [val];
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
json.const = val;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
if (vals.every((v) => typeof v === "number"))
|
|
192
|
+
json.type = "number";
|
|
193
|
+
if (vals.every((v) => typeof v === "string"))
|
|
194
|
+
json.type = "string";
|
|
195
|
+
if (vals.every((v) => typeof v === "boolean"))
|
|
196
|
+
json.type = "boolean";
|
|
197
|
+
if (vals.every((v) => v === null))
|
|
198
|
+
json.type = "null";
|
|
199
|
+
json.enum = vals;
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
export const nanProcessor = (_schema, ctx, _json, _params) => {
|
|
203
|
+
if (ctx.unrepresentable === "throw") {
|
|
204
|
+
throw new Error("NaN cannot be represented in JSON Schema");
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
export const templateLiteralProcessor = (schema, _ctx, json, _params) => {
|
|
208
|
+
const _json = json;
|
|
209
|
+
const pattern = schema._zod.pattern;
|
|
210
|
+
if (!pattern)
|
|
211
|
+
throw new Error("Pattern not found in template literal");
|
|
212
|
+
_json.type = "string";
|
|
213
|
+
_json.pattern = pattern.source;
|
|
214
|
+
};
|
|
215
|
+
export const fileProcessor = (schema, _ctx, json, _params) => {
|
|
216
|
+
const _json = json;
|
|
217
|
+
const file = {
|
|
218
|
+
type: "string",
|
|
219
|
+
format: "binary",
|
|
220
|
+
contentEncoding: "binary",
|
|
221
|
+
};
|
|
222
|
+
const { minimum, maximum, mime } = schema._zod
|
|
223
|
+
.bag;
|
|
224
|
+
if (minimum !== undefined)
|
|
225
|
+
file.minLength = minimum;
|
|
226
|
+
if (maximum !== undefined)
|
|
227
|
+
file.maxLength = maximum;
|
|
228
|
+
if (mime) {
|
|
229
|
+
if (mime.length === 1) {
|
|
230
|
+
file.contentMediaType = mime[0];
|
|
231
|
+
Object.assign(_json, file);
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
Object.assign(_json, file); // shared props at root
|
|
235
|
+
_json.anyOf = mime.map((m) => ({ contentMediaType: m })); // only contentMediaType differs
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
Object.assign(_json, file);
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
export const successProcessor = (_schema, _ctx, json, _params) => {
|
|
243
|
+
json.type = "boolean";
|
|
244
|
+
};
|
|
245
|
+
export const customProcessor = (_schema, ctx, _json, _params) => {
|
|
246
|
+
if (ctx.unrepresentable === "throw") {
|
|
247
|
+
throw new Error("Custom types cannot be represented in JSON Schema");
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
export const functionProcessor = (_schema, ctx, _json, _params) => {
|
|
251
|
+
if (ctx.unrepresentable === "throw") {
|
|
252
|
+
throw new Error("Function types cannot be represented in JSON Schema");
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
export const transformProcessor = (_schema, ctx, _json, _params) => {
|
|
256
|
+
if (ctx.unrepresentable === "throw") {
|
|
257
|
+
throw new Error("Transforms cannot be represented in JSON Schema");
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
export const mapProcessor = (_schema, ctx, _json, _params) => {
|
|
261
|
+
if (ctx.unrepresentable === "throw") {
|
|
262
|
+
throw new Error("Map cannot be represented in JSON Schema");
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
export const setProcessor = (_schema, ctx, _json, _params) => {
|
|
266
|
+
if (ctx.unrepresentable === "throw") {
|
|
267
|
+
throw new Error("Set cannot be represented in JSON Schema");
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
// ==================== COMPOSITE TYPE PROCESSORS ====================
|
|
271
|
+
export const arrayProcessor = (schema, ctx, _json, params) => {
|
|
272
|
+
const json = _json;
|
|
273
|
+
const def = schema._zod.def;
|
|
274
|
+
const { minimum, maximum } = schema._zod.bag;
|
|
275
|
+
if (typeof minimum === "number")
|
|
276
|
+
json.minItems = minimum;
|
|
277
|
+
if (typeof maximum === "number")
|
|
278
|
+
json.maxItems = maximum;
|
|
279
|
+
json.type = "array";
|
|
280
|
+
json.items = core.process(def.element, ctx, {
|
|
281
|
+
...params,
|
|
282
|
+
path: [...params.path, "items"],
|
|
283
|
+
});
|
|
284
|
+
};
|
|
285
|
+
export const objectProcessor = (schema, ctx, _json, params) => {
|
|
286
|
+
const json = _json;
|
|
287
|
+
const def = schema._zod.def;
|
|
288
|
+
json.type = "object";
|
|
289
|
+
json.properties = {};
|
|
290
|
+
const shape = def.shape;
|
|
291
|
+
for (const key in shape) {
|
|
292
|
+
json.properties[key] = core.process(shape[key], ctx, {
|
|
293
|
+
...params,
|
|
294
|
+
path: [...params.path, "properties", key],
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
// required keys
|
|
298
|
+
const allKeys = new Set(Object.keys(shape));
|
|
299
|
+
const requiredKeys = new Set([...allKeys].filter((key) => {
|
|
300
|
+
const v = def.shape[key]._zod;
|
|
301
|
+
if (ctx.io === "input") {
|
|
302
|
+
return v.optin === undefined;
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
return v.optout === undefined;
|
|
306
|
+
}
|
|
307
|
+
}));
|
|
308
|
+
if (requiredKeys.size > 0) {
|
|
309
|
+
json.required = Array.from(requiredKeys);
|
|
310
|
+
}
|
|
311
|
+
// catchall
|
|
312
|
+
if (def.catchall?._zod.def.type === "never") {
|
|
313
|
+
// strict
|
|
314
|
+
json.additionalProperties = false;
|
|
315
|
+
}
|
|
316
|
+
else if (!def.catchall) {
|
|
317
|
+
// regular
|
|
318
|
+
if (ctx.io === "output")
|
|
319
|
+
json.additionalProperties = false;
|
|
320
|
+
}
|
|
321
|
+
else if (def.catchall) {
|
|
322
|
+
json.additionalProperties = core.process(def.catchall, ctx, {
|
|
323
|
+
...params,
|
|
324
|
+
path: [...params.path, "additionalProperties"],
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
export const unionProcessor = (schema, ctx, json, params) => {
|
|
329
|
+
const def = schema._zod.def;
|
|
330
|
+
// Exclusive unions (inclusive === false) use oneOf (exactly one match) instead of anyOf (one or more matches)
|
|
331
|
+
// This includes both z.xor() and discriminated unions
|
|
332
|
+
const isExclusive = def.inclusive === false;
|
|
333
|
+
const options = def.options.map((x, i) => core.process(x, ctx, {
|
|
334
|
+
...params,
|
|
335
|
+
path: [...params.path, isExclusive ? "oneOf" : "anyOf", i],
|
|
336
|
+
}));
|
|
337
|
+
if (isExclusive) {
|
|
338
|
+
json.oneOf = options;
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
json.anyOf = options;
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
export const intersectionProcessor = (schema, ctx, json, params) => {
|
|
345
|
+
const def = schema._zod.def;
|
|
346
|
+
const a = core.process(def.left, ctx, {
|
|
347
|
+
...params,
|
|
348
|
+
path: [...params.path, "allOf", 0],
|
|
349
|
+
});
|
|
350
|
+
const b = core.process(def.right, ctx, {
|
|
351
|
+
...params,
|
|
352
|
+
path: [...params.path, "allOf", 1],
|
|
353
|
+
});
|
|
354
|
+
const isSimpleIntersection = (val) => "allOf" in val && Object.keys(val).length === 1;
|
|
355
|
+
const allOf = [
|
|
356
|
+
...(isSimpleIntersection(a) ? a.allOf : [a]),
|
|
357
|
+
...(isSimpleIntersection(b) ? b.allOf : [b]),
|
|
358
|
+
];
|
|
359
|
+
json.allOf = allOf;
|
|
360
|
+
};
|
|
361
|
+
export const tupleProcessor = (schema, ctx, _json, params) => {
|
|
362
|
+
const json = _json;
|
|
363
|
+
const def = schema._zod.def;
|
|
364
|
+
json.type = "array";
|
|
365
|
+
const prefixPath = ctx.target === "draft-2020-12" ? "prefixItems" : "items";
|
|
366
|
+
const restPath = ctx.target === "draft-2020-12"
|
|
367
|
+
? "items"
|
|
368
|
+
: ctx.target === "openapi-3.0"
|
|
369
|
+
? "items"
|
|
370
|
+
: "additionalItems";
|
|
371
|
+
const prefixItems = def.items.map((x, i) => core.process(x, ctx, {
|
|
372
|
+
...params,
|
|
373
|
+
path: [...params.path, prefixPath, i],
|
|
374
|
+
}));
|
|
375
|
+
const rest = def.rest
|
|
376
|
+
? core.process(def.rest, ctx, {
|
|
377
|
+
...params,
|
|
378
|
+
path: [
|
|
379
|
+
...params.path,
|
|
380
|
+
restPath,
|
|
381
|
+
...(ctx.target === "openapi-3.0" ? [def.items.length] : []),
|
|
382
|
+
],
|
|
383
|
+
})
|
|
384
|
+
: null;
|
|
385
|
+
if (ctx.target === "draft-2020-12") {
|
|
386
|
+
json.prefixItems = prefixItems;
|
|
387
|
+
if (rest) {
|
|
388
|
+
json.items = rest;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
else if (ctx.target === "openapi-3.0") {
|
|
392
|
+
json.items = {
|
|
393
|
+
anyOf: prefixItems,
|
|
394
|
+
};
|
|
395
|
+
if (rest) {
|
|
396
|
+
json.items.anyOf.push(rest);
|
|
397
|
+
}
|
|
398
|
+
json.minItems = prefixItems.length;
|
|
399
|
+
if (!rest) {
|
|
400
|
+
json.maxItems = prefixItems.length;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
json.items = prefixItems;
|
|
405
|
+
if (rest) {
|
|
406
|
+
json.additionalItems = rest;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
// length
|
|
410
|
+
const { minimum, maximum } = schema._zod.bag;
|
|
411
|
+
if (typeof minimum === "number")
|
|
412
|
+
json.minItems = minimum;
|
|
413
|
+
if (typeof maximum === "number")
|
|
414
|
+
json.maxItems = maximum;
|
|
415
|
+
};
|
|
416
|
+
export const recordProcessor = (schema, ctx, _json, params) => {
|
|
417
|
+
const json = _json;
|
|
418
|
+
const def = schema._zod.def;
|
|
419
|
+
json.type = "object";
|
|
420
|
+
// For looseRecord with regex patterns, use patternProperties
|
|
421
|
+
// This correctly represents "only validate keys matching the pattern" semantics
|
|
422
|
+
// and composes well with allOf (intersections)
|
|
423
|
+
const keyType = def.keyType;
|
|
424
|
+
const keyBag = keyType._zod.bag;
|
|
425
|
+
const patterns = keyBag?.patterns;
|
|
426
|
+
if (def.mode === "loose" && patterns && patterns.size > 0) {
|
|
427
|
+
// Use patternProperties for looseRecord with regex patterns
|
|
428
|
+
const valueSchema = core.process(def.valueType, ctx, {
|
|
429
|
+
...params,
|
|
430
|
+
path: [...params.path, "patternProperties", "*"],
|
|
431
|
+
});
|
|
432
|
+
json.patternProperties = {};
|
|
433
|
+
for (const pattern of patterns) {
|
|
434
|
+
json.patternProperties[pattern.source] = valueSchema;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
else {
|
|
438
|
+
// Default behavior: use propertyNames + additionalProperties
|
|
439
|
+
if (ctx.target === "draft-07" || ctx.target === "draft-2020-12") {
|
|
440
|
+
json.propertyNames = core.process(def.keyType, ctx, {
|
|
441
|
+
...params,
|
|
442
|
+
path: [...params.path, "propertyNames"],
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
json.additionalProperties = core.process(def.valueType, ctx, {
|
|
446
|
+
...params,
|
|
447
|
+
path: [...params.path, "additionalProperties"],
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
// Add required for keys with discrete values (enum, literal, etc.)
|
|
451
|
+
const keyValues = keyType._zod.values;
|
|
452
|
+
if (keyValues) {
|
|
453
|
+
const validKeyValues = [...keyValues].filter((v) => typeof v === "string" || typeof v === "number");
|
|
454
|
+
if (validKeyValues.length > 0) {
|
|
455
|
+
json.required = validKeyValues;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
export const nullableProcessor = (schema, ctx, json, params) => {
|
|
460
|
+
const def = schema._zod.def;
|
|
461
|
+
const inner = core.process(def.innerType, ctx, params);
|
|
462
|
+
const seen = ctx.seen.get(schema);
|
|
463
|
+
if (ctx.target === "openapi-3.0") {
|
|
464
|
+
seen.ref = def.innerType;
|
|
465
|
+
json.nullable = true;
|
|
466
|
+
}
|
|
467
|
+
else {
|
|
468
|
+
json.anyOf = [inner, { type: "null" }];
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
export const nonoptionalProcessor = (schema, ctx, _json, params) => {
|
|
472
|
+
const def = schema._zod.def;
|
|
473
|
+
core.process(def.innerType, ctx, params);
|
|
474
|
+
const seen = ctx.seen.get(schema);
|
|
475
|
+
seen.ref = def.innerType;
|
|
476
|
+
};
|
|
477
|
+
export const defaultProcessor = (schema, ctx, json, params) => {
|
|
478
|
+
const def = schema._zod.def;
|
|
479
|
+
core.process(def.innerType, ctx, params);
|
|
480
|
+
const seen = ctx.seen.get(schema);
|
|
481
|
+
seen.ref = def.innerType;
|
|
482
|
+
json.default = JSON.parse(JSON.stringify(def.defaultValue));
|
|
483
|
+
};
|
|
484
|
+
export const prefaultProcessor = (schema, ctx, json, params) => {
|
|
485
|
+
const def = schema._zod.def;
|
|
486
|
+
core.process(def.innerType, ctx, params);
|
|
487
|
+
const seen = ctx.seen.get(schema);
|
|
488
|
+
seen.ref = def.innerType;
|
|
489
|
+
if (ctx.io === "input")
|
|
490
|
+
json._prefault = JSON.parse(JSON.stringify(def.defaultValue));
|
|
491
|
+
};
|
|
492
|
+
export const catchProcessor = (schema, ctx, json, params) => {
|
|
493
|
+
const def = schema._zod.def;
|
|
494
|
+
core.process(def.innerType, ctx, params);
|
|
495
|
+
const seen = ctx.seen.get(schema);
|
|
496
|
+
seen.ref = def.innerType;
|
|
497
|
+
let catchValue;
|
|
498
|
+
try {
|
|
499
|
+
catchValue = def.catchValue(undefined);
|
|
500
|
+
}
|
|
501
|
+
catch {
|
|
502
|
+
throw new Error("Dynamic catch values are not supported in JSON Schema");
|
|
503
|
+
}
|
|
504
|
+
json.default = catchValue;
|
|
505
|
+
};
|
|
506
|
+
export const pipeProcessor = (schema, ctx, _json, params) => {
|
|
507
|
+
const def = schema._zod.def;
|
|
508
|
+
const innerType = ctx.io === "input"
|
|
509
|
+
? def.in._zod.def.type === "transform"
|
|
510
|
+
? def.out
|
|
511
|
+
: def.in
|
|
512
|
+
: def.out;
|
|
513
|
+
core.process(innerType, ctx, params);
|
|
514
|
+
const seen = ctx.seen.get(schema);
|
|
515
|
+
seen.ref = innerType;
|
|
516
|
+
};
|
|
517
|
+
export const readonlyProcessor = (schema, ctx, json, params) => {
|
|
518
|
+
const def = schema._zod.def;
|
|
519
|
+
core.process(def.innerType, ctx, params);
|
|
520
|
+
const seen = ctx.seen.get(schema);
|
|
521
|
+
seen.ref = def.innerType;
|
|
522
|
+
json.readOnly = true;
|
|
523
|
+
};
|
|
524
|
+
export const promiseProcessor = (schema, ctx, _json, params) => {
|
|
525
|
+
const def = schema._zod.def;
|
|
526
|
+
core.process(def.innerType, ctx, params);
|
|
527
|
+
const seen = ctx.seen.get(schema);
|
|
528
|
+
seen.ref = def.innerType;
|
|
529
|
+
};
|
|
530
|
+
export const optionalProcessor = (schema, ctx, _json, params) => {
|
|
531
|
+
const def = schema._zod.def;
|
|
532
|
+
core.process(def.innerType, ctx, params);
|
|
533
|
+
const seen = ctx.seen.get(schema);
|
|
534
|
+
seen.ref = def.innerType;
|
|
535
|
+
};
|
|
536
|
+
export const lazyProcessor = (schema, ctx, _json, params) => {
|
|
537
|
+
const innerType = schema._zod.innerType;
|
|
538
|
+
core.process(innerType, ctx, params);
|
|
539
|
+
const seen = ctx.seen.get(schema);
|
|
540
|
+
seen.ref = innerType;
|
|
541
|
+
};
|
|
542
|
+
// ==================== ALL PROCESSORS ====================
|
|
543
|
+
export const allProcessors = {
|
|
544
|
+
string: stringProcessor,
|
|
545
|
+
number: numberProcessor,
|
|
546
|
+
boolean: booleanProcessor,
|
|
547
|
+
bigint: bigintProcessor,
|
|
548
|
+
symbol: symbolProcessor,
|
|
549
|
+
null: nullProcessor,
|
|
550
|
+
undefined: undefinedProcessor,
|
|
551
|
+
void: voidProcessor,
|
|
552
|
+
never: neverProcessor,
|
|
553
|
+
any: anyProcessor,
|
|
554
|
+
unknown: unknownProcessor,
|
|
555
|
+
date: dateProcessor,
|
|
556
|
+
enum: enumProcessor,
|
|
557
|
+
literal: literalProcessor,
|
|
558
|
+
nan: nanProcessor,
|
|
559
|
+
template_literal: templateLiteralProcessor,
|
|
560
|
+
file: fileProcessor,
|
|
561
|
+
success: successProcessor,
|
|
562
|
+
custom: customProcessor,
|
|
563
|
+
function: functionProcessor,
|
|
564
|
+
transform: transformProcessor,
|
|
565
|
+
map: mapProcessor,
|
|
566
|
+
set: setProcessor,
|
|
567
|
+
array: arrayProcessor,
|
|
568
|
+
object: objectProcessor,
|
|
569
|
+
union: unionProcessor,
|
|
570
|
+
intersection: intersectionProcessor,
|
|
571
|
+
tuple: tupleProcessor,
|
|
572
|
+
record: recordProcessor,
|
|
573
|
+
nullable: nullableProcessor,
|
|
574
|
+
nonoptional: nonoptionalProcessor,
|
|
575
|
+
default: defaultProcessor,
|
|
576
|
+
prefault: prefaultProcessor,
|
|
577
|
+
catch: catchProcessor,
|
|
578
|
+
pipe: pipeProcessor,
|
|
579
|
+
readonly: readonlyProcessor,
|
|
580
|
+
promise: promiseProcessor,
|
|
581
|
+
optional: optionalProcessor,
|
|
582
|
+
lazy: lazyProcessor,
|
|
583
|
+
};
|
|
584
|
+
export function toJSONSchema(input, params) {
|
|
585
|
+
if ("_idmap" in input) {
|
|
586
|
+
// Registry case
|
|
587
|
+
const registry = input;
|
|
588
|
+
const ctx = core.initializeContext({
|
|
589
|
+
...params,
|
|
590
|
+
processors: allProcessors,
|
|
591
|
+
});
|
|
592
|
+
const defs = {};
|
|
593
|
+
// First pass: process all schemas to build the seen map
|
|
594
|
+
for (const entry of registry._idmap.entries()) {
|
|
595
|
+
const [_, schema] = entry;
|
|
596
|
+
core.process(schema, ctx);
|
|
597
|
+
}
|
|
598
|
+
const schemas = {};
|
|
599
|
+
const external = {
|
|
600
|
+
registry,
|
|
601
|
+
uri: params?.uri,
|
|
602
|
+
defs,
|
|
603
|
+
};
|
|
604
|
+
// Update the context with external configuration
|
|
605
|
+
ctx.external = external;
|
|
606
|
+
// Second pass: emit each schema
|
|
607
|
+
for (const entry of registry._idmap.entries()) {
|
|
608
|
+
const [key, schema] = entry;
|
|
609
|
+
core.extractDefs(ctx, schema);
|
|
610
|
+
schemas[key] = core.finalize(ctx, schema);
|
|
611
|
+
}
|
|
612
|
+
if (Object.keys(defs).length > 0) {
|
|
613
|
+
const defsSegment = ctx.target === "draft-2020-12" ? "$defs" : "definitions";
|
|
614
|
+
schemas.__shared = {
|
|
615
|
+
[defsSegment]: defs,
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
return { schemas };
|
|
619
|
+
}
|
|
620
|
+
// Single schema case
|
|
621
|
+
const ctx = core.initializeContext({
|
|
622
|
+
...params,
|
|
623
|
+
processors: allProcessors,
|
|
624
|
+
});
|
|
625
|
+
core.process(input, ctx);
|
|
626
|
+
core.extractDefs(ctx, input);
|
|
627
|
+
return core.finalize(ctx, input);
|
|
628
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/** biome-ignore-all assist/source/organizeImports: re-exporting */
|
|
2
|
+
export { type ZodCoercedString, iso, ZodISODate, ZodISODateTime, ZodISODuration, ZodISOTime, email, ZodEmail, guid as originalGuid, ZodGUID as OriginalZodGUID, uuid as originalUuid, ZodUUID as OriginalZodUUID, uuidv4 as originalUuidv4, uuidv6 as originalUuidv6, uuidv7 as originalUuidv7, url, ZodURL, httpUrl, emoji, ZodEmoji, nanoid, ZodNanoID, cuid, ZodCUID, cuid2, ZodCUID2, ulid, ZodULID, xid, ZodXID, ksuid, ZodKSUID, ipv4, ZodIPv4, mac, ZodMAC, ipv6, ZodIPv6, cidrv4, ZodCIDRv4, cidrv6, ZodCIDRv6, base64, ZodBase64, base64url, ZodBase64URL, e164, ZodE164, jwt, ZodJWT, stringFormat, ZodStringFormat, ZodCustomStringFormat, hostname, hex, hash, number, ZodNumber, ZodNumberFormat, type _ZodNumber, type ZodCoercedNumber, int, type ZodInt, float32, type ZodFloat32, float64, type ZodFloat64, int32, type ZodInt32, uint32, type ZodUInt32, boolean, type _ZodBoolean, ZodBoolean, type ZodCoercedBoolean, bigint, type _ZodBigInt, ZodBigInt, ZodBigIntFormat, type ZodCoercedBigInt, int64, uint64, symbol, ZodSymbol, undefined, ZodUndefined, any, ZodAny, type ZodTypeAny, unknown, ZodUnknown, never, ZodNever, void, ZodVoid, null, ZodNull, date as originalDate, type _ZodDate as _OriginalZodDate, ZodDate as OriginalZodDate, type ZodCoercedDate, array, ZodArray, keyof, object, ZodObject, type ZodRawShape, strictObject, looseObject, union, ZodUnion, discriminatedUnion, ZodDiscriminatedUnion, intersection, ZodIntersection, record, ZodRecord, partialRecord, map, ZodMap, set, ZodSet, enum, ZodEnum, nativeEnum, literal, ZodLiteral, file, ZodFile, transform, ZodTransform, optional, ZodOptional, nullable, ZodNullable, nullish, _default, ZodDefault, prefault, ZodPrefault, nonoptional, ZodNonOptional, success, ZodSuccess, catch, ZodCatch, nan, ZodNaN, pipe, ZodPipe, codec, ZodCodec, readonly, ZodReadonly, templateLiteral, ZodTemplateLiteral, lazy, ZodLazy, promise, ZodPromise, function, _function, ZodFunction, custom, ZodCustom, clone, coerce, check, ZodError, ZodRealError, type ZodErrorMap, type ZodFlattenedError, type ZodFormattedError, formatError, getErrorMap, setErrorMap, decode, decodeAsync, safeDecode, safeDecodeAsync, encode, encodeAsync, safeEncode, safeEncodeAsync, parse, parseAsync, safeParse, safeParseAsync, type ZodSafeParseError, type ZodSafeParseResult, type ZodSafeParseSuccess, type SafeExtendShape, type output, $output, type input, $input, type BRAND, $brand, type GlobalMeta, meta, ZodFirstPartyTypeKind, type ZodFirstPartySchemaTypes, type inferFlattenedErrors, type inferFormattedError, type Infer, type infer, NEVER, type RefinementCtx, ZodType, type _ZodType, type Schema, type ZodSchema, TimePrecision, type TypeOf, type ZodJSONSchema, type ZodJSONSchemaInternals, config, core, endsWith, startsWith, describe, flattenError, globalRegistry, gt, gte, lt, lte, includes, instanceof, json, length, locales, lowercase, toLowerCase, uppercase, toUpperCase, minLength, maxLength, minSize, maxSize, size, multipleOf, negative, nonnegative, positive, nonpositive, property, normalize, overwrite, preprocess, prettifyError, refine, superRefine, regexes, mime, regex, slugify, stringbool, trim, registry, toJSONSchema, treeifyError, util, z, type IssueData, ZodIssueCode, type ZodIssue, } from "zod/v4";
|