@orpc/zod 0.0.0-next.f99e554 → 0.0.0-next.fb0d07c
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/README.md +142 -0
- package/dist/index.d.mts +82 -0
- package/dist/index.d.ts +82 -0
- package/dist/index.mjs +822 -0
- package/dist/zod4/index.d.mts +302 -0
- package/dist/zod4/index.d.ts +302 -0
- package/dist/zod4/index.mjs +660 -0
- package/package.json +19 -12
- package/dist/index.js +0 -973
- package/dist/src/coercer.d.ts +0 -6
- package/dist/src/converter.d.ts +0 -50
- package/dist/src/index.d.ts +0 -4
- package/dist/src/schemas.d.ts +0 -31
@@ -0,0 +1,660 @@
|
|
1
|
+
import { isObject, guard, intercept } from '@orpc/shared';
|
2
|
+
import { JSONSchemaFormat, JSONSchemaContentEncoding } from '@orpc/openapi';
|
3
|
+
import { registry, globalRegistry } from 'zod/v4/core';
|
4
|
+
|
5
|
+
class experimental_ZodSmartCoercionPlugin {
|
6
|
+
init(options) {
|
7
|
+
options.clientInterceptors ??= [];
|
8
|
+
options.clientInterceptors.unshift((options2) => {
|
9
|
+
const inputSchema = options2.procedure["~orpc"].inputSchema;
|
10
|
+
if (!inputSchema || inputSchema["~standard"].vendor !== "zod") {
|
11
|
+
return options2.next();
|
12
|
+
}
|
13
|
+
const coercedInput = this.#coerce(inputSchema, options2.input);
|
14
|
+
return options2.next({ ...options2, input: coercedInput });
|
15
|
+
});
|
16
|
+
}
|
17
|
+
#coerce(schema, value) {
|
18
|
+
switch (schema._zod.def.type) {
|
19
|
+
case "number": {
|
20
|
+
if (typeof value === "string") {
|
21
|
+
return this.#stringToNumber(value);
|
22
|
+
}
|
23
|
+
return value;
|
24
|
+
}
|
25
|
+
case "bigint": {
|
26
|
+
if (typeof value === "string") {
|
27
|
+
return this.#stringToBigInt(value);
|
28
|
+
}
|
29
|
+
return value;
|
30
|
+
}
|
31
|
+
case "boolean":
|
32
|
+
case "success": {
|
33
|
+
if (typeof value === "string") {
|
34
|
+
return this.#stringToBoolean(value);
|
35
|
+
}
|
36
|
+
return value;
|
37
|
+
}
|
38
|
+
case "date": {
|
39
|
+
if (typeof value === "string") {
|
40
|
+
return this.#stringToDate(value);
|
41
|
+
}
|
42
|
+
return value;
|
43
|
+
}
|
44
|
+
case "literal":
|
45
|
+
case "enum": {
|
46
|
+
const literal = schema;
|
47
|
+
if (!literal._zod.values.has(value) && typeof value === "string") {
|
48
|
+
const num = this.#stringToNumber(value);
|
49
|
+
if (literal._zod.values.has(num)) {
|
50
|
+
return num;
|
51
|
+
}
|
52
|
+
const bool = this.#stringToBoolean(value);
|
53
|
+
if (literal._zod.values.has(bool)) {
|
54
|
+
return bool;
|
55
|
+
}
|
56
|
+
const bigint = this.#stringToBigInt(value);
|
57
|
+
if (literal._zod.values.has(bigint)) {
|
58
|
+
return bigint;
|
59
|
+
}
|
60
|
+
}
|
61
|
+
return value;
|
62
|
+
}
|
63
|
+
case "array": {
|
64
|
+
const array = schema;
|
65
|
+
if (value === void 0) {
|
66
|
+
return [];
|
67
|
+
}
|
68
|
+
if (Array.isArray(value)) {
|
69
|
+
return value.map((v) => this.#coerce(array._zod.def.element, v));
|
70
|
+
}
|
71
|
+
return value;
|
72
|
+
}
|
73
|
+
case "tuple": {
|
74
|
+
const tuple = schema;
|
75
|
+
if (value === void 0) {
|
76
|
+
return [];
|
77
|
+
}
|
78
|
+
if (Array.isArray(value)) {
|
79
|
+
return value.map((v, i) => {
|
80
|
+
const s = tuple._zod.def.items[i] ?? tuple._zod.def.rest;
|
81
|
+
return s ? this.#coerce(s, v) : v;
|
82
|
+
});
|
83
|
+
}
|
84
|
+
return value;
|
85
|
+
}
|
86
|
+
case "set": {
|
87
|
+
const set = schema;
|
88
|
+
if (value === void 0) {
|
89
|
+
return /* @__PURE__ */ new Set();
|
90
|
+
}
|
91
|
+
if (Array.isArray(value)) {
|
92
|
+
return new Set(
|
93
|
+
value.map((v) => this.#coerce(set._zod.def.valueType, v))
|
94
|
+
);
|
95
|
+
}
|
96
|
+
if (value instanceof Set) {
|
97
|
+
return new Set(
|
98
|
+
Array.from(value).map((v) => this.#coerce(set._zod.def.valueType, v))
|
99
|
+
);
|
100
|
+
}
|
101
|
+
return value;
|
102
|
+
}
|
103
|
+
case "object":
|
104
|
+
case "interface": {
|
105
|
+
const object = schema;
|
106
|
+
if (value === void 0) {
|
107
|
+
return {};
|
108
|
+
}
|
109
|
+
if (isObject(value)) {
|
110
|
+
const newObj = {};
|
111
|
+
const keys = /* @__PURE__ */ new Set([
|
112
|
+
...Object.keys(value),
|
113
|
+
...Object.keys(object._zod.def.shape)
|
114
|
+
]);
|
115
|
+
for (const k of keys) {
|
116
|
+
const s = object._zod.def.shape[k] ?? object._zod.def.catchall;
|
117
|
+
newObj[k] = s ? this.#coerce(s, value[k]) : value[k];
|
118
|
+
}
|
119
|
+
return newObj;
|
120
|
+
}
|
121
|
+
return value;
|
122
|
+
}
|
123
|
+
case "record": {
|
124
|
+
const record = schema;
|
125
|
+
if (value === void 0) {
|
126
|
+
return {};
|
127
|
+
}
|
128
|
+
if (isObject(value)) {
|
129
|
+
const newObj = {};
|
130
|
+
for (const [k, v] of Object.entries(value)) {
|
131
|
+
const key = this.#coerce(record._zod.def.keyType, k);
|
132
|
+
const val = this.#coerce(record._zod.def.valueType, v);
|
133
|
+
newObj[key] = val;
|
134
|
+
}
|
135
|
+
return newObj;
|
136
|
+
}
|
137
|
+
return value;
|
138
|
+
}
|
139
|
+
case "map": {
|
140
|
+
const map = schema;
|
141
|
+
if (value === void 0) {
|
142
|
+
return /* @__PURE__ */ new Map();
|
143
|
+
}
|
144
|
+
if (Array.isArray(value) && value.every((i) => Array.isArray(i) && i.length <= 2)) {
|
145
|
+
return new Map(
|
146
|
+
value.map(([k, v]) => [
|
147
|
+
this.#coerce(map._zod.def.keyType, k),
|
148
|
+
this.#coerce(map._zod.def.valueType, v)
|
149
|
+
])
|
150
|
+
);
|
151
|
+
}
|
152
|
+
if (value instanceof Map) {
|
153
|
+
return new Map(
|
154
|
+
Array.from(value).map(([k, v]) => [
|
155
|
+
this.#coerce(map._zod.def.keyType, k),
|
156
|
+
this.#coerce(map._zod.def.valueType, v)
|
157
|
+
])
|
158
|
+
);
|
159
|
+
}
|
160
|
+
return value;
|
161
|
+
}
|
162
|
+
case "union": {
|
163
|
+
const union = schema;
|
164
|
+
if (union._zod.def.options.length === 1) {
|
165
|
+
return this.#coerce(union._zod.def.options[0], value);
|
166
|
+
}
|
167
|
+
if (isObject(value)) {
|
168
|
+
for (const option of union._zod.def.options) {
|
169
|
+
if (option._zod.disc && this.#matchDiscriminators(value, option._zod.disc)) {
|
170
|
+
return this.#coerce(option, value);
|
171
|
+
}
|
172
|
+
}
|
173
|
+
}
|
174
|
+
return value;
|
175
|
+
}
|
176
|
+
case "intersection": {
|
177
|
+
const intersection = schema;
|
178
|
+
return this.#coerce(
|
179
|
+
intersection._zod.def.right,
|
180
|
+
this.#coerce(intersection._zod.def.left, value)
|
181
|
+
);
|
182
|
+
}
|
183
|
+
case "optional": {
|
184
|
+
const optional = schema;
|
185
|
+
if (value === void 0) {
|
186
|
+
return void 0;
|
187
|
+
}
|
188
|
+
return this.#coerce(optional._zod.def.innerType, value);
|
189
|
+
}
|
190
|
+
case "nonoptional": {
|
191
|
+
const nonoptional = schema;
|
192
|
+
return this.#coerce(nonoptional._zod.def.innerType, value);
|
193
|
+
}
|
194
|
+
case "nullable": {
|
195
|
+
const nullable = schema;
|
196
|
+
if (value === null) {
|
197
|
+
return null;
|
198
|
+
}
|
199
|
+
return this.#coerce(nullable._zod.def.innerType, value);
|
200
|
+
}
|
201
|
+
case "readonly": {
|
202
|
+
const readonly_ = schema;
|
203
|
+
return this.#coerce(readonly_._zod.def.innerType, value);
|
204
|
+
}
|
205
|
+
case "pipe": {
|
206
|
+
const pipe = schema;
|
207
|
+
return this.#coerce(pipe._zod.def.in, value);
|
208
|
+
}
|
209
|
+
case "default":
|
210
|
+
case "prefault": {
|
211
|
+
const default_ = schema;
|
212
|
+
if (value === void 0) {
|
213
|
+
return value;
|
214
|
+
}
|
215
|
+
return this.#coerce(default_._zod.def.innerType, value);
|
216
|
+
}
|
217
|
+
case "catch": {
|
218
|
+
const catch_ = schema;
|
219
|
+
return this.#coerce(catch_._zod.def.innerType, value);
|
220
|
+
}
|
221
|
+
case "lazy": {
|
222
|
+
const lazy = schema;
|
223
|
+
if (value !== void 0) {
|
224
|
+
return this.#coerce(lazy._zod.def.getter(), value);
|
225
|
+
}
|
226
|
+
return value;
|
227
|
+
}
|
228
|
+
default: {
|
229
|
+
schema._zod.def.type;
|
230
|
+
return value;
|
231
|
+
}
|
232
|
+
}
|
233
|
+
}
|
234
|
+
#stringToNumber(value) {
|
235
|
+
const num = Number(value);
|
236
|
+
return Number.isNaN(num) || num.toString() !== value ? value : num;
|
237
|
+
}
|
238
|
+
#stringToBigInt(value) {
|
239
|
+
return guard(() => BigInt(value)) ?? value;
|
240
|
+
}
|
241
|
+
#stringToBoolean(value) {
|
242
|
+
const lower = value.toLowerCase();
|
243
|
+
if (lower === "false" || lower === "off" || lower === "f") {
|
244
|
+
return false;
|
245
|
+
}
|
246
|
+
if (lower === "true" || lower === "on" || lower === "t") {
|
247
|
+
return true;
|
248
|
+
}
|
249
|
+
return value;
|
250
|
+
}
|
251
|
+
#stringToDate(value) {
|
252
|
+
const date = new Date(value);
|
253
|
+
if (!Number.isNaN(date.getTime()) && date.toISOString().startsWith(value)) {
|
254
|
+
return date;
|
255
|
+
}
|
256
|
+
return value;
|
257
|
+
}
|
258
|
+
/**
|
259
|
+
* This function is inspired from Zod, because it's not exported
|
260
|
+
* https://github.com/colinhacks/zod/blob/v4/packages/core/src/schemas.ts#L1903C1-L1921C2
|
261
|
+
*/
|
262
|
+
#matchDiscriminators(input, discs) {
|
263
|
+
for (const [key, value] of discs) {
|
264
|
+
const data = input[key];
|
265
|
+
if (value.values.size && !value.values.has(data)) {
|
266
|
+
return false;
|
267
|
+
}
|
268
|
+
if (value.maps.length === 0) {
|
269
|
+
continue;
|
270
|
+
}
|
271
|
+
if (!isObject(data)) {
|
272
|
+
return false;
|
273
|
+
}
|
274
|
+
for (const m of value.maps) {
|
275
|
+
if (!this.#matchDiscriminators(data, m)) {
|
276
|
+
return false;
|
277
|
+
}
|
278
|
+
}
|
279
|
+
}
|
280
|
+
return true;
|
281
|
+
}
|
282
|
+
}
|
283
|
+
|
284
|
+
const experimental_JSON_SCHEMA_REGISTRY = registry();
|
285
|
+
const experimental_JSON_SCHEMA_INPUT_REGISTRY = registry();
|
286
|
+
const experimental_JSON_SCHEMA_OUTPUT_REGISTRY = registry();
|
287
|
+
|
288
|
+
class experimental_ZodToJsonSchemaConverter {
|
289
|
+
maxLazyDepth;
|
290
|
+
anyJsonSchema;
|
291
|
+
unsupportedJsonSchema;
|
292
|
+
undefinedJsonSchema;
|
293
|
+
interceptors;
|
294
|
+
constructor(options = {}) {
|
295
|
+
this.maxLazyDepth = options.maxLazyDepth ?? 2;
|
296
|
+
this.anyJsonSchema = options.anyJsonSchema ?? {};
|
297
|
+
this.unsupportedJsonSchema = options.unsupportedJsonSchema ?? { not: {} };
|
298
|
+
this.undefinedJsonSchema = options.undefinedJsonSchema ?? { not: {} };
|
299
|
+
this.interceptors = options.interceptors ?? [];
|
300
|
+
}
|
301
|
+
condition(schema) {
|
302
|
+
return schema !== void 0 && schema["~standard"].vendor === "zod";
|
303
|
+
}
|
304
|
+
convert(schema, options) {
|
305
|
+
return this.#convert(schema, options, 0);
|
306
|
+
}
|
307
|
+
#convert(schema, options, lazyDepth, isHandledCustomJSONSchema = false) {
|
308
|
+
return intercept(
|
309
|
+
this.interceptors,
|
310
|
+
{ schema, options, lazyDepth, isHandledCustomJSONSchema },
|
311
|
+
({ schema: schema2, options: options2, lazyDepth: lazyDepth2, isHandledCustomJSONSchema: isHandledCustomJSONSchema2 }) => {
|
312
|
+
if (!isHandledCustomJSONSchema2) {
|
313
|
+
const customJSONSchema = this.#getCustomJsonSchema(schema2, options2);
|
314
|
+
if (customJSONSchema) {
|
315
|
+
const [required, json] = this.#convert(schema2, options2, lazyDepth2, true);
|
316
|
+
return [required, { ...json, ...customJSONSchema }];
|
317
|
+
}
|
318
|
+
}
|
319
|
+
switch (schema2._zod.def.type) {
|
320
|
+
case "string": {
|
321
|
+
const string = schema2;
|
322
|
+
const json = { type: "string" };
|
323
|
+
const { minimum, maximum, format, pattern, contentEncoding } = string._zod.bag;
|
324
|
+
if (typeof minimum === "number") {
|
325
|
+
json.minLength = minimum;
|
326
|
+
}
|
327
|
+
if (typeof maximum === "number") {
|
328
|
+
json.maxLength = maximum;
|
329
|
+
}
|
330
|
+
if (typeof contentEncoding === "string") {
|
331
|
+
json.contentEncoding = this.#handleContentEncoding(contentEncoding);
|
332
|
+
}
|
333
|
+
if (typeof format === "string" && format !== "regex" && json.contentEncoding === void 0) {
|
334
|
+
json.format = this.#handleStringFormat(format);
|
335
|
+
}
|
336
|
+
if (pattern instanceof RegExp && json.contentEncoding === void 0 && json.format === void 0) {
|
337
|
+
json.pattern = pattern.source;
|
338
|
+
}
|
339
|
+
if (format === "jwt" && json.contentEncoding === void 0 && json.format === void 0 && json.pattern === void 0) {
|
340
|
+
json.pattern = /^[\w-]+\.[\w-]+\.[\w-]+$/.source;
|
341
|
+
}
|
342
|
+
return [true, json];
|
343
|
+
}
|
344
|
+
case "number": {
|
345
|
+
const number = schema2;
|
346
|
+
const json = { type: "number" };
|
347
|
+
const { minimum, maximum, format, multipleOf, exclusiveMaximum, exclusiveMinimum } = number._zod.bag;
|
348
|
+
if (typeof format === "string" && format?.includes("int")) {
|
349
|
+
json.type = "integer";
|
350
|
+
}
|
351
|
+
if (typeof minimum === "number") {
|
352
|
+
json.minimum = minimum;
|
353
|
+
}
|
354
|
+
if (typeof maximum === "number") {
|
355
|
+
json.maximum = maximum;
|
356
|
+
}
|
357
|
+
if (typeof exclusiveMinimum === "number") {
|
358
|
+
json.exclusiveMinimum = exclusiveMinimum;
|
359
|
+
}
|
360
|
+
if (typeof exclusiveMaximum === "number") {
|
361
|
+
json.exclusiveMaximum = exclusiveMaximum;
|
362
|
+
}
|
363
|
+
if (typeof multipleOf === "number") {
|
364
|
+
json.multipleOf = multipleOf;
|
365
|
+
}
|
366
|
+
return [true, json];
|
367
|
+
}
|
368
|
+
case "boolean": {
|
369
|
+
return [true, { type: "boolean" }];
|
370
|
+
}
|
371
|
+
case "bigint": {
|
372
|
+
return [true, { type: "string", pattern: "^-?[0-9]+$" }];
|
373
|
+
}
|
374
|
+
case "date": {
|
375
|
+
return [true, { type: "string", format: JSONSchemaFormat.DateTime }];
|
376
|
+
}
|
377
|
+
case "null": {
|
378
|
+
return [true, { type: "null" }];
|
379
|
+
}
|
380
|
+
case "undefined":
|
381
|
+
case "void": {
|
382
|
+
return [false, this.undefinedJsonSchema];
|
383
|
+
}
|
384
|
+
case "any": {
|
385
|
+
return [false, this.anyJsonSchema];
|
386
|
+
}
|
387
|
+
case "unknown": {
|
388
|
+
return [false, this.anyJsonSchema];
|
389
|
+
}
|
390
|
+
case "never": {
|
391
|
+
return [true, this.unsupportedJsonSchema];
|
392
|
+
}
|
393
|
+
case "array": {
|
394
|
+
const array = schema2;
|
395
|
+
const json = { type: "array" };
|
396
|
+
const { minimum, maximum } = array._zod.bag;
|
397
|
+
if (typeof minimum === "number") {
|
398
|
+
json.minItems = minimum;
|
399
|
+
}
|
400
|
+
if (typeof maximum === "number") {
|
401
|
+
json.maxItems = maximum;
|
402
|
+
}
|
403
|
+
json.items = this.#handleArrayItemJsonSchema(this.#convert(array._zod.def.element, options2, lazyDepth2), options2);
|
404
|
+
return [true, json];
|
405
|
+
}
|
406
|
+
case "object": {
|
407
|
+
const object = schema2;
|
408
|
+
const json = { type: "object" };
|
409
|
+
for (const [key, value] of Object.entries(object._zod.def.shape)) {
|
410
|
+
const [itemRequired, itemJson] = this.#convert(value, options2, lazyDepth2);
|
411
|
+
json.properties ??= {};
|
412
|
+
json.properties[key] = itemJson;
|
413
|
+
if (itemRequired) {
|
414
|
+
json.required ??= [];
|
415
|
+
json.required.push(key);
|
416
|
+
}
|
417
|
+
}
|
418
|
+
if (object._zod.def.catchall) {
|
419
|
+
if (object._zod.def.catchall._zod.def.type === "never") {
|
420
|
+
json.additionalProperties = false;
|
421
|
+
} else {
|
422
|
+
const [_, addJson] = this.#convert(object._zod.def.catchall, options2, lazyDepth2);
|
423
|
+
json.additionalProperties = addJson;
|
424
|
+
}
|
425
|
+
}
|
426
|
+
return [true, json];
|
427
|
+
}
|
428
|
+
case "union": {
|
429
|
+
const union = schema2;
|
430
|
+
const anyOf = [];
|
431
|
+
let required = true;
|
432
|
+
for (const item of union._zod.def.options) {
|
433
|
+
const [itemRequired, itemJson] = this.#convert(item, options2, lazyDepth2);
|
434
|
+
if (!itemRequired) {
|
435
|
+
required = false;
|
436
|
+
}
|
437
|
+
if (options2.strategy === "input") {
|
438
|
+
if (itemJson !== this.undefinedJsonSchema && itemJson !== this.unsupportedJsonSchema) {
|
439
|
+
anyOf.push(itemJson);
|
440
|
+
}
|
441
|
+
} else {
|
442
|
+
if (itemJson !== this.undefinedJsonSchema) {
|
443
|
+
anyOf.push(itemJson);
|
444
|
+
}
|
445
|
+
}
|
446
|
+
}
|
447
|
+
return [required, anyOf.length === 1 ? anyOf[0] : { anyOf }];
|
448
|
+
}
|
449
|
+
case "intersection": {
|
450
|
+
const intersection = schema2;
|
451
|
+
const json = { allOf: [] };
|
452
|
+
let required = false;
|
453
|
+
for (const item of [intersection._zod.def.left, intersection._zod.def.right]) {
|
454
|
+
const [itemRequired, itemJson] = this.#convert(item, options2, lazyDepth2);
|
455
|
+
json.allOf.push(itemJson);
|
456
|
+
if (itemRequired) {
|
457
|
+
required = true;
|
458
|
+
}
|
459
|
+
}
|
460
|
+
return [required, json];
|
461
|
+
}
|
462
|
+
case "tuple": {
|
463
|
+
const tuple = schema2;
|
464
|
+
const json = { type: "array", prefixItems: [] };
|
465
|
+
for (const item of tuple._zod.def.items) {
|
466
|
+
json.prefixItems.push(this.#handleArrayItemJsonSchema(this.#convert(item, options2, lazyDepth2), options2));
|
467
|
+
}
|
468
|
+
if (tuple._zod.def.rest) {
|
469
|
+
json.items = this.#handleArrayItemJsonSchema(this.#convert(tuple._zod.def.rest, options2, lazyDepth2), options2);
|
470
|
+
}
|
471
|
+
const { minimum, maximum } = tuple._zod.bag;
|
472
|
+
if (typeof minimum === "number") {
|
473
|
+
json.minItems = minimum;
|
474
|
+
}
|
475
|
+
if (typeof maximum === "number") {
|
476
|
+
json.maxItems = maximum;
|
477
|
+
}
|
478
|
+
return [true, json];
|
479
|
+
}
|
480
|
+
case "record": {
|
481
|
+
const record = schema2;
|
482
|
+
const json = { type: "object" };
|
483
|
+
json.propertyNames = this.#convert(record._zod.def.keyType, options2, lazyDepth2)[1];
|
484
|
+
json.additionalProperties = this.#convert(record._zod.def.valueType, options2, lazyDepth2)[1];
|
485
|
+
return [true, json];
|
486
|
+
}
|
487
|
+
case "map": {
|
488
|
+
const map = schema2;
|
489
|
+
return [true, {
|
490
|
+
type: "array",
|
491
|
+
items: {
|
492
|
+
type: "array",
|
493
|
+
prefixItems: [
|
494
|
+
this.#handleArrayItemJsonSchema(this.#convert(map._zod.def.keyType, options2, lazyDepth2), options2),
|
495
|
+
this.#handleArrayItemJsonSchema(this.#convert(map._zod.def.valueType, options2, lazyDepth2), options2)
|
496
|
+
],
|
497
|
+
maxItems: 2,
|
498
|
+
minItems: 2
|
499
|
+
}
|
500
|
+
}];
|
501
|
+
}
|
502
|
+
case "set": {
|
503
|
+
const set = schema2;
|
504
|
+
return [true, {
|
505
|
+
type: "array",
|
506
|
+
uniqueItems: true,
|
507
|
+
items: this.#handleArrayItemJsonSchema(this.#convert(set._zod.def.valueType, options2, lazyDepth2), options2)
|
508
|
+
}];
|
509
|
+
}
|
510
|
+
case "enum": {
|
511
|
+
const enum_ = schema2;
|
512
|
+
return [true, { enum: Object.values(enum_._zod.def.entries) }];
|
513
|
+
}
|
514
|
+
case "literal": {
|
515
|
+
const literal = schema2;
|
516
|
+
let required = true;
|
517
|
+
const values = /* @__PURE__ */ new Set();
|
518
|
+
for (const value of literal._zod.def.values) {
|
519
|
+
if (value === void 0) {
|
520
|
+
required = false;
|
521
|
+
} else {
|
522
|
+
values.add(typeof value === "bigint" ? value.toString() : value);
|
523
|
+
}
|
524
|
+
}
|
525
|
+
const json = values.size === 0 ? this.undefinedJsonSchema : values.size === 1 ? { const: values.values().next().value } : { enum: Array.from(values) };
|
526
|
+
return [required, json];
|
527
|
+
}
|
528
|
+
case "file": {
|
529
|
+
const file = schema2;
|
530
|
+
const oneOf = [];
|
531
|
+
const { mime } = file._zod.bag;
|
532
|
+
if (mime === void 0 || Array.isArray(mime) && mime.every((m) => typeof m === "string")) {
|
533
|
+
for (const type of mime ?? ["*/*"]) {
|
534
|
+
oneOf.push({
|
535
|
+
type: "string",
|
536
|
+
contentMediaType: type
|
537
|
+
});
|
538
|
+
}
|
539
|
+
}
|
540
|
+
return [true, oneOf.length === 1 ? oneOf[0] : { anyOf: oneOf }];
|
541
|
+
}
|
542
|
+
case "transform": {
|
543
|
+
return [false, this.anyJsonSchema];
|
544
|
+
}
|
545
|
+
case "nullable": {
|
546
|
+
const nullable = schema2;
|
547
|
+
const [required, json] = this.#convert(nullable._zod.def.innerType, options2, lazyDepth2);
|
548
|
+
return [required, { anyOf: [json, { type: "null" }] }];
|
549
|
+
}
|
550
|
+
case "nonoptional": {
|
551
|
+
const nonoptional = schema2;
|
552
|
+
const [, json] = this.#convert(nonoptional._zod.def.innerType, options2, lazyDepth2);
|
553
|
+
return [true, json];
|
554
|
+
}
|
555
|
+
case "success": {
|
556
|
+
return [true, { type: "boolean" }];
|
557
|
+
}
|
558
|
+
case "default":
|
559
|
+
case "prefault": {
|
560
|
+
const default_ = schema2;
|
561
|
+
const [, json] = this.#convert(default_._zod.def.innerType, options2, lazyDepth2);
|
562
|
+
return [false, {
|
563
|
+
...json,
|
564
|
+
default: default_._zod.def.defaultValue
|
565
|
+
}];
|
566
|
+
}
|
567
|
+
case "catch": {
|
568
|
+
const catch_ = schema2;
|
569
|
+
return this.#convert(catch_._zod.def.innerType, options2, lazyDepth2);
|
570
|
+
}
|
571
|
+
case "nan": {
|
572
|
+
return [true, options2.strategy === "input" ? this.unsupportedJsonSchema : { type: "null" }];
|
573
|
+
}
|
574
|
+
case "pipe": {
|
575
|
+
const pipe = schema2;
|
576
|
+
return this.#convert(options2.strategy === "input" ? pipe._zod.def.in : pipe._zod.def.out, options2, lazyDepth2);
|
577
|
+
}
|
578
|
+
case "readonly": {
|
579
|
+
const readonly_ = schema2;
|
580
|
+
const [required, json] = this.#convert(readonly_._zod.def.innerType, options2, lazyDepth2);
|
581
|
+
return [required, { ...json, readOnly: true }];
|
582
|
+
}
|
583
|
+
case "template_literal": {
|
584
|
+
const templateLiteral = schema2;
|
585
|
+
return [true, {
|
586
|
+
type: "string",
|
587
|
+
pattern: templateLiteral._zod.pattern.source
|
588
|
+
}];
|
589
|
+
}
|
590
|
+
case "optional": {
|
591
|
+
const optional = schema2;
|
592
|
+
const [, json] = this.#convert(optional._zod.def.innerType, options2, lazyDepth2);
|
593
|
+
return [false, json];
|
594
|
+
}
|
595
|
+
case "lazy": {
|
596
|
+
const lazy = schema2;
|
597
|
+
if (lazyDepth2 >= this.maxLazyDepth) {
|
598
|
+
return [false, this.anyJsonSchema];
|
599
|
+
}
|
600
|
+
return this.#convert(lazy._zod.def.getter(), options2, lazyDepth2 + 1);
|
601
|
+
}
|
602
|
+
default: {
|
603
|
+
schema2._zod.def.type;
|
604
|
+
return [true, this.unsupportedJsonSchema];
|
605
|
+
}
|
606
|
+
}
|
607
|
+
}
|
608
|
+
);
|
609
|
+
}
|
610
|
+
#getCustomJsonSchema(schema, options) {
|
611
|
+
if (options.strategy === "input" && experimental_JSON_SCHEMA_INPUT_REGISTRY.has(schema)) {
|
612
|
+
return experimental_JSON_SCHEMA_INPUT_REGISTRY.get(schema);
|
613
|
+
}
|
614
|
+
if (options.strategy === "output" && experimental_JSON_SCHEMA_OUTPUT_REGISTRY.has(schema)) {
|
615
|
+
return experimental_JSON_SCHEMA_OUTPUT_REGISTRY.get(schema);
|
616
|
+
}
|
617
|
+
if (experimental_JSON_SCHEMA_REGISTRY.has(schema)) {
|
618
|
+
return experimental_JSON_SCHEMA_REGISTRY.get(schema);
|
619
|
+
}
|
620
|
+
const global = globalRegistry.get(schema);
|
621
|
+
if (global) {
|
622
|
+
return {
|
623
|
+
description: global.description,
|
624
|
+
examples: global.examples
|
625
|
+
};
|
626
|
+
}
|
627
|
+
}
|
628
|
+
#handleArrayItemJsonSchema([required, schema], options) {
|
629
|
+
if (required || options.strategy === "input" || schema.default !== void 0) {
|
630
|
+
return schema;
|
631
|
+
}
|
632
|
+
if (schema === this.undefinedJsonSchema) {
|
633
|
+
return { type: "null" };
|
634
|
+
}
|
635
|
+
return {
|
636
|
+
anyOf: [
|
637
|
+
// schema can contain { type: 'null' } so we should use anyOf instead of oneOf
|
638
|
+
schema,
|
639
|
+
{ type: "null" }
|
640
|
+
]
|
641
|
+
};
|
642
|
+
}
|
643
|
+
#handleStringFormat(format) {
|
644
|
+
if (format === "guid") {
|
645
|
+
return JSONSchemaFormat.UUID;
|
646
|
+
}
|
647
|
+
if (format === "url") {
|
648
|
+
return JSONSchemaFormat.URI;
|
649
|
+
}
|
650
|
+
if (format === "datetime") {
|
651
|
+
return JSONSchemaFormat.DateTime;
|
652
|
+
}
|
653
|
+
return Object.values(JSONSchemaFormat).includes(format) ? format : void 0;
|
654
|
+
}
|
655
|
+
#handleContentEncoding(contentEncoding) {
|
656
|
+
return Object.values(JSONSchemaContentEncoding).includes(contentEncoding) ? contentEncoding : void 0;
|
657
|
+
}
|
658
|
+
}
|
659
|
+
|
660
|
+
export { experimental_JSON_SCHEMA_INPUT_REGISTRY, experimental_JSON_SCHEMA_OUTPUT_REGISTRY, experimental_JSON_SCHEMA_REGISTRY, experimental_ZodSmartCoercionPlugin, experimental_ZodToJsonSchemaConverter };
|