@orpc/zod 0.0.0-next.fd1db03 → 0.0.0-next.fd6982a

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/dist/index.js DELETED
@@ -1,973 +0,0 @@
1
- // ../../node_modules/.pnpm/is-what@5.0.2/node_modules/is-what/dist/getType.js
2
- function getType(payload) {
3
- return Object.prototype.toString.call(payload).slice(8, -1);
4
- }
5
-
6
- // ../../node_modules/.pnpm/is-what@5.0.2/node_modules/is-what/dist/isPlainObject.js
7
- function isPlainObject(payload) {
8
- if (getType(payload) !== "Object")
9
- return false;
10
- const prototype = Object.getPrototypeOf(payload);
11
- return !!prototype && prototype.constructor === Object && prototype === Object.prototype;
12
- }
13
-
14
- // ../../node_modules/.pnpm/radash@12.1.0/node_modules/radash/dist/esm/async.mjs
15
- var guard = (func, shouldGuard) => {
16
- const _guard = (err) => {
17
- if (shouldGuard && !shouldGuard(err))
18
- throw err;
19
- return void 0;
20
- };
21
- const isPromise2 = (result) => result instanceof Promise;
22
- try {
23
- const result = func();
24
- return isPromise2(result) ? result.catch(_guard) : result;
25
- } catch (err) {
26
- return _guard(err);
27
- }
28
- };
29
-
30
- // src/coercer.ts
31
- import {
32
- ZodFirstPartyTypeKind
33
- } from "zod";
34
- var ZodCoercer = class {
35
- coerce(schema, value) {
36
- if (!schema || schema["~standard"].vendor !== "zod") {
37
- return value;
38
- }
39
- const zodSchema = schema;
40
- const coerced = zodCoerceInternal(zodSchema, value, { bracketNotation: true });
41
- return coerced;
42
- }
43
- };
44
- function zodCoerceInternal(schema, value, options) {
45
- const isRoot = options?.isRoot ?? true;
46
- const options_ = { ...options, isRoot: false };
47
- if (isRoot && options?.bracketNotation && Array.isArray(value) && value.length === 1) {
48
- const newValue = zodCoerceInternal(schema, value[0], options_);
49
- if (schema.safeParse(newValue).success) {
50
- return newValue;
51
- }
52
- return zodCoerceInternal(schema, value, options_);
53
- }
54
- const customType = getCustomZodType(schema._def);
55
- if (customType === "Invalid Date") {
56
- if (typeof value === "string" && value.toLocaleLowerCase() === "invalid date") {
57
- return /* @__PURE__ */ new Date("Invalid Date");
58
- }
59
- } else if (customType === "RegExp") {
60
- if (typeof value === "string" && value.startsWith("/")) {
61
- const match = value.match(/^\/(.*)\/([a-z]*)$/);
62
- if (match) {
63
- const [, pattern, flags] = match;
64
- return new RegExp(pattern, flags);
65
- }
66
- }
67
- } else if (customType === "URL") {
68
- if (typeof value === "string") {
69
- const url2 = guard(() => new URL(value));
70
- if (url2 !== void 0) {
71
- return url2;
72
- }
73
- }
74
- }
75
- if (schema._def.typeName === void 0) {
76
- return value;
77
- }
78
- const typeName = schema._def.typeName;
79
- if (typeName === ZodFirstPartyTypeKind.ZodNumber) {
80
- if (options_?.bracketNotation && typeof value === "string") {
81
- const num = Number(value);
82
- if (!Number.isNaN(num)) {
83
- return num;
84
- }
85
- }
86
- } else if (typeName === ZodFirstPartyTypeKind.ZodNaN) {
87
- if (typeof value === "string" && value.toLocaleLowerCase() === "nan") {
88
- return Number.NaN;
89
- }
90
- } else if (typeName === ZodFirstPartyTypeKind.ZodBoolean) {
91
- if (options_?.bracketNotation && typeof value === "string") {
92
- const lower = value.toLowerCase();
93
- if (lower === "false" || lower === "off" || lower === "f") {
94
- return false;
95
- }
96
- if (lower === "true" || lower === "on" || lower === "t") {
97
- return true;
98
- }
99
- }
100
- } else if (typeName === ZodFirstPartyTypeKind.ZodNull) {
101
- if (options_?.bracketNotation && typeof value === "string" && value.toLowerCase() === "null") {
102
- return null;
103
- }
104
- } else if (typeName === ZodFirstPartyTypeKind.ZodUndefined || typeName === ZodFirstPartyTypeKind.ZodVoid) {
105
- if (typeof value === "string" && value.toLowerCase() === "undefined") {
106
- return void 0;
107
- }
108
- } else if (typeName === ZodFirstPartyTypeKind.ZodDate) {
109
- if (typeof value === "string" && (value.includes("-") || value.includes(":") || value.toLocaleLowerCase() === "invalid date")) {
110
- return new Date(value);
111
- }
112
- } else if (typeName === ZodFirstPartyTypeKind.ZodBigInt) {
113
- if (typeof value === "string") {
114
- const num = guard(() => BigInt(value));
115
- if (num !== void 0) {
116
- return num;
117
- }
118
- }
119
- } else if (typeName === ZodFirstPartyTypeKind.ZodArray || typeName === ZodFirstPartyTypeKind.ZodTuple) {
120
- const schema_ = schema;
121
- if (Array.isArray(value)) {
122
- return value.map((v) => zodCoerceInternal(schema_._def.type, v, options_));
123
- }
124
- if (options_?.bracketNotation) {
125
- if (value === void 0) {
126
- return [];
127
- }
128
- if (isPlainObject(value) && Object.keys(value).every((k) => /^[1-9]\d*$/.test(k) || k === "0")) {
129
- const indexes = Object.keys(value).map((k) => Number(k)).sort((a, b) => a - b);
130
- const arr = Array.from({ length: (indexes.at(-1) ?? -1) + 1 });
131
- for (const i of indexes) {
132
- arr[i] = zodCoerceInternal(schema_._def.type, value[i], options_);
133
- }
134
- return arr;
135
- }
136
- }
137
- } else if (typeName === ZodFirstPartyTypeKind.ZodObject) {
138
- const schema_ = schema;
139
- if (isPlainObject(value)) {
140
- const newObj = {};
141
- const keys = /* @__PURE__ */ new Set([
142
- ...Object.keys(value),
143
- ...Object.keys(schema_.shape)
144
- ]);
145
- for (const k of keys) {
146
- if (!(k in value))
147
- continue;
148
- const v = value[k];
149
- newObj[k] = zodCoerceInternal(
150
- schema_.shape[k] ?? schema_._def.catchall,
151
- v,
152
- options_
153
- );
154
- }
155
- return newObj;
156
- }
157
- if (options_?.bracketNotation) {
158
- if (value === void 0) {
159
- return {};
160
- }
161
- if (Array.isArray(value) && value.length === 1) {
162
- const emptySchema = schema_.shape[""] ?? schema_._def.catchall;
163
- return { "": zodCoerceInternal(emptySchema, value[0], options_) };
164
- }
165
- }
166
- } else if (typeName === ZodFirstPartyTypeKind.ZodSet) {
167
- const schema_ = schema;
168
- if (Array.isArray(value)) {
169
- return new Set(
170
- value.map((v) => zodCoerceInternal(schema_._def.valueType, v, options_))
171
- );
172
- }
173
- if (options_?.bracketNotation) {
174
- if (value === void 0) {
175
- return /* @__PURE__ */ new Set();
176
- }
177
- if (isPlainObject(value) && Object.keys(value).every((k) => /^[1-9]\d*$/.test(k) || k === "0")) {
178
- const indexes = Object.keys(value).map((k) => Number(k)).sort((a, b) => a - b);
179
- const arr = Array.from({ length: (indexes.at(-1) ?? -1) + 1 });
180
- for (const i of indexes) {
181
- arr[i] = zodCoerceInternal(schema_._def.valueType, value[i], options_);
182
- }
183
- return new Set(arr);
184
- }
185
- }
186
- } else if (typeName === ZodFirstPartyTypeKind.ZodMap) {
187
- const schema_ = schema;
188
- if (Array.isArray(value) && value.every((i) => Array.isArray(i) && i.length === 2)) {
189
- return new Map(
190
- value.map(([k, v]) => [
191
- zodCoerceInternal(schema_._def.keyType, k, options_),
192
- zodCoerceInternal(schema_._def.valueType, v, options_)
193
- ])
194
- );
195
- }
196
- if (options_?.bracketNotation) {
197
- if (value === void 0) {
198
- return /* @__PURE__ */ new Map();
199
- }
200
- if (isPlainObject(value)) {
201
- const arr = Array.from({ length: Object.keys(value).length }).fill(void 0).map(
202
- (_, i) => isPlainObject(value[i]) && Object.keys(value[i]).length === 2 && "0" in value[i] && "1" in value[i] ? [value[i]["0"], value[i]["1"]] : void 0
203
- );
204
- if (arr.every((v) => !!v)) {
205
- return new Map(
206
- arr.map(([k, v]) => [
207
- zodCoerceInternal(schema_._def.keyType, k, options_),
208
- zodCoerceInternal(schema_._def.valueType, v, options_)
209
- ])
210
- );
211
- }
212
- }
213
- }
214
- } else if (typeName === ZodFirstPartyTypeKind.ZodRecord) {
215
- const schema_ = schema;
216
- if (isPlainObject(value)) {
217
- const newObj = {};
218
- for (const [k, v] of Object.entries(value)) {
219
- const key = zodCoerceInternal(schema_._def.keyType, k, options_);
220
- const val = zodCoerceInternal(schema_._def.valueType, v, options_);
221
- newObj[key] = val;
222
- }
223
- return newObj;
224
- }
225
- } else if (typeName === ZodFirstPartyTypeKind.ZodUnion || typeName === ZodFirstPartyTypeKind.ZodDiscriminatedUnion) {
226
- const schema_ = schema;
227
- if (schema_.safeParse(value).success) {
228
- return value;
229
- }
230
- const results = [];
231
- for (const s of schema_._def.options) {
232
- const newValue = zodCoerceInternal(s, value, { ...options_, isRoot });
233
- if (newValue === value)
234
- continue;
235
- const result = schema_.safeParse(newValue);
236
- if (result.success) {
237
- return newValue;
238
- }
239
- results.push([newValue, result.error.issues.length]);
240
- }
241
- if (results.length === 0) {
242
- return value;
243
- }
244
- return results.sort((a, b) => a[1] - b[1])[0]?.[0];
245
- } else if (typeName === ZodFirstPartyTypeKind.ZodIntersection) {
246
- const schema_ = schema;
247
- return zodCoerceInternal(
248
- schema_._def.right,
249
- zodCoerceInternal(schema_._def.left, value, { ...options_, isRoot }),
250
- { ...options_, isRoot }
251
- );
252
- } else if (typeName === ZodFirstPartyTypeKind.ZodReadonly) {
253
- const schema_ = schema;
254
- return zodCoerceInternal(schema_._def.innerType, value, { ...options_, isRoot });
255
- } else if (typeName === ZodFirstPartyTypeKind.ZodPipeline) {
256
- const schema_ = schema;
257
- return zodCoerceInternal(schema_._def.in, value, { ...options_, isRoot });
258
- } else if (typeName === ZodFirstPartyTypeKind.ZodLazy) {
259
- const schema_ = schema;
260
- return zodCoerceInternal(schema_._def.getter(), value, { ...options_, isRoot });
261
- } else if (typeName === ZodFirstPartyTypeKind.ZodEffects) {
262
- const schema_ = schema;
263
- return zodCoerceInternal(schema_._def.schema, value, { ...options_, isRoot });
264
- } else if (typeName === ZodFirstPartyTypeKind.ZodBranded) {
265
- const schema_ = schema;
266
- return zodCoerceInternal(schema_._def.type, value, { ...options_, isRoot });
267
- } else if (typeName === ZodFirstPartyTypeKind.ZodCatch) {
268
- const schema_ = schema;
269
- return zodCoerceInternal(schema_._def.innerType, value, { ...options_, isRoot });
270
- } else if (typeName === ZodFirstPartyTypeKind.ZodDefault) {
271
- const schema_ = schema;
272
- return zodCoerceInternal(schema_._def.innerType, value, { ...options_, isRoot });
273
- } else if (typeName === ZodFirstPartyTypeKind.ZodNullable) {
274
- const schema_ = schema;
275
- if (value === null) {
276
- return null;
277
- }
278
- if (typeof value === "string" && value.toLowerCase() === "null") {
279
- return schema_.safeParse(value).success ? value : null;
280
- }
281
- return zodCoerceInternal(schema_._def.innerType, value, { ...options_, isRoot });
282
- } else if (typeName === ZodFirstPartyTypeKind.ZodOptional) {
283
- const schema_ = schema;
284
- if (value === void 0) {
285
- return void 0;
286
- }
287
- if (typeof value === "string" && value.toLowerCase() === "undefined") {
288
- return schema_.safeParse(value).success ? value : void 0;
289
- }
290
- return zodCoerceInternal(schema_._def.innerType, value, { ...options_, isRoot });
291
- } else if (typeName === ZodFirstPartyTypeKind.ZodNativeEnum) {
292
- const schema_ = schema;
293
- if (Object.values(schema_._def.values).includes(value)) {
294
- return value;
295
- }
296
- if (options?.bracketNotation && typeof value === "string") {
297
- for (const expectedValue of Object.values(schema_._def.values)) {
298
- if (expectedValue.toString() === value) {
299
- return expectedValue;
300
- }
301
- }
302
- }
303
- } else if (typeName === ZodFirstPartyTypeKind.ZodLiteral) {
304
- const schema_ = schema;
305
- const expectedValue = schema_._def.value;
306
- if (typeof value === "string" && typeof expectedValue !== "string") {
307
- if (typeof expectedValue === "bigint") {
308
- const num = guard(() => BigInt(value));
309
- if (num !== void 0) {
310
- return num;
311
- }
312
- } else if (expectedValue === void 0) {
313
- if (value.toLocaleLowerCase() === "undefined") {
314
- return void 0;
315
- }
316
- } else if (options?.bracketNotation) {
317
- if (typeof expectedValue === "number") {
318
- const num = Number(value);
319
- if (!Number.isNaN(num)) {
320
- return num;
321
- }
322
- } else if (typeof expectedValue === "boolean") {
323
- const lower = value.toLowerCase();
324
- if (lower === "false" || lower === "off" || lower === "f") {
325
- return false;
326
- }
327
- if (lower === "true" || lower === "on" || lower === "t") {
328
- return true;
329
- }
330
- } else if (expectedValue === null) {
331
- if (value.toLocaleLowerCase() === "null") {
332
- return null;
333
- }
334
- }
335
- }
336
- }
337
- } else {
338
- const _expected = typeName;
339
- }
340
- return value;
341
- }
342
-
343
- // src/converter.ts
344
- import { JSONSchemaFormat } from "@orpc/openapi";
345
-
346
- // ../../node_modules/.pnpm/escape-string-regexp@5.0.0/node_modules/escape-string-regexp/index.js
347
- function escapeStringRegexp(string) {
348
- if (typeof string !== "string") {
349
- throw new TypeError("Expected a string");
350
- }
351
- return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
352
- }
353
-
354
- // src/converter.ts
355
- import {
356
- ZodFirstPartyTypeKind as ZodFirstPartyTypeKind2
357
- } from "zod";
358
-
359
- // src/schemas.ts
360
- import wcmatch from "wildcard-match";
361
- import {
362
- custom
363
- } from "zod";
364
- var customZodTypeSymbol = Symbol("customZodTypeSymbol");
365
- var customZodFileMimeTypeSymbol = Symbol("customZodFileMimeTypeSymbol");
366
- var CUSTOM_JSON_SCHEMA_SYMBOL = Symbol("CUSTOM_JSON_SCHEMA");
367
- var CUSTOM_JSON_SCHEMA_INPUT_SYMBOL = Symbol("CUSTOM_JSON_SCHEMA_INPUT");
368
- var CUSTOM_JSON_SCHEMA_OUTPUT_SYMBOL = Symbol("CUSTOM_JSON_SCHEMA_OUTPUT");
369
- function getCustomZodType(def) {
370
- return customZodTypeSymbol in def ? def[customZodTypeSymbol] : void 0;
371
- }
372
- function getCustomZodFileMimeType(def) {
373
- return customZodFileMimeTypeSymbol in def ? def[customZodFileMimeTypeSymbol] : void 0;
374
- }
375
- function getCustomJSONSchema(def, options) {
376
- if (options?.mode === "input" && CUSTOM_JSON_SCHEMA_INPUT_SYMBOL in def) {
377
- return def[CUSTOM_JSON_SCHEMA_INPUT_SYMBOL];
378
- }
379
- if (options?.mode === "output" && CUSTOM_JSON_SCHEMA_OUTPUT_SYMBOL in def) {
380
- return def[CUSTOM_JSON_SCHEMA_OUTPUT_SYMBOL];
381
- }
382
- if (CUSTOM_JSON_SCHEMA_SYMBOL in def) {
383
- return def[CUSTOM_JSON_SCHEMA_SYMBOL];
384
- }
385
- return void 0;
386
- }
387
- function composeParams(options) {
388
- return (val) => {
389
- const defaultMessage = typeof options.defaultMessage === "function" ? options.defaultMessage(val) : options.defaultMessage;
390
- if (!options.params) {
391
- return {
392
- message: defaultMessage
393
- };
394
- }
395
- if (typeof options.params === "function") {
396
- return {
397
- message: defaultMessage,
398
- ...options.params(val)
399
- };
400
- }
401
- if (typeof options.params === "object") {
402
- return {
403
- message: defaultMessage,
404
- ...options.params
405
- };
406
- }
407
- return {
408
- message: options.params
409
- };
410
- };
411
- }
412
- function file(params) {
413
- const schema = custom(
414
- (val) => val instanceof File,
415
- composeParams({ params, defaultMessage: "Input is not a file" })
416
- );
417
- Object.assign(schema._def, {
418
- [customZodTypeSymbol]: "File"
419
- });
420
- return Object.assign(schema, {
421
- type: (mimeType, params2) => {
422
- const isMatch = wcmatch(mimeType);
423
- const refinedSchema = schema.refine(
424
- (val) => isMatch(val.type.split(";")[0]),
425
- composeParams({
426
- params: params2,
427
- defaultMessage: (val) => `Expected a file of type ${mimeType} but got a file of type ${val.type || "unknown"}`
428
- })
429
- );
430
- Object.assign(refinedSchema._def, {
431
- [customZodTypeSymbol]: "File",
432
- [customZodFileMimeTypeSymbol]: mimeType
433
- });
434
- return refinedSchema;
435
- }
436
- });
437
- }
438
- function blob(params) {
439
- const schema = custom(
440
- (val) => val instanceof Blob,
441
- composeParams({ params, defaultMessage: "Input is not a blob" })
442
- );
443
- Object.assign(schema._def, {
444
- [customZodTypeSymbol]: "Blob"
445
- });
446
- return schema;
447
- }
448
- function invalidDate(params) {
449
- const schema = custom(
450
- (val) => val instanceof Date && Number.isNaN(val.getTime()),
451
- composeParams({ params, defaultMessage: "Input is not an invalid date" })
452
- );
453
- Object.assign(schema._def, {
454
- [customZodTypeSymbol]: "Invalid Date"
455
- });
456
- return schema;
457
- }
458
- function regexp(options) {
459
- const schema = custom(
460
- (val) => val instanceof RegExp,
461
- composeParams({ params: options, defaultMessage: "Input is not a regexp" })
462
- );
463
- Object.assign(schema._def, {
464
- [customZodTypeSymbol]: "RegExp"
465
- });
466
- return schema;
467
- }
468
- function url(options) {
469
- const schema = custom(
470
- (val) => val instanceof URL,
471
- composeParams({ params: options, defaultMessage: "Input is not a URL" })
472
- );
473
- Object.assign(schema._def, {
474
- [customZodTypeSymbol]: "URL"
475
- });
476
- return schema;
477
- }
478
- function openapi(schema, custom2, options) {
479
- const newSchema = schema.refine(() => true);
480
- const SYMBOL = options?.mode === "input" ? CUSTOM_JSON_SCHEMA_INPUT_SYMBOL : options?.mode === "output" ? CUSTOM_JSON_SCHEMA_OUTPUT_SYMBOL : CUSTOM_JSON_SCHEMA_SYMBOL;
481
- Object.assign(newSchema._def, {
482
- [SYMBOL]: custom2
483
- });
484
- return newSchema;
485
- }
486
- var oz = {
487
- openapi,
488
- file,
489
- blob,
490
- invalidDate,
491
- regexp,
492
- url
493
- };
494
-
495
- // src/converter.ts
496
- var NON_LOGIC_KEYWORDS = [
497
- // Core Documentation Keywords
498
- "$anchor",
499
- "$comment",
500
- "$defs",
501
- "$id",
502
- "title",
503
- "description",
504
- // Value Keywords
505
- "default",
506
- "deprecated",
507
- "examples",
508
- // Metadata Keywords
509
- "$schema",
510
- "definitions",
511
- // Legacy, but still used
512
- "readOnly",
513
- "writeOnly",
514
- // Display and UI Hints
515
- "contentMediaType",
516
- "contentEncoding",
517
- "format",
518
- // Custom Extensions
519
- "$vocabulary",
520
- "$dynamicAnchor",
521
- "$dynamicRef"
522
- ];
523
- var UNSUPPORTED_JSON_SCHEMA = { not: {} };
524
- var UNDEFINED_JSON_SCHEMA = { const: "undefined" };
525
- function zodToJsonSchema(schema, options) {
526
- if (schema["~standard"].vendor !== "zod") {
527
- console.warn(`Generate JSON schema not support ${schema["~standard"].vendor} yet`);
528
- return {};
529
- }
530
- const schema__ = schema;
531
- if (!options?.isHandledZodDescription && "description" in schema__._def) {
532
- const json = zodToJsonSchema(schema__, {
533
- ...options,
534
- isHandledZodDescription: true
535
- });
536
- return {
537
- description: schema__._def.description,
538
- ...json
539
- };
540
- }
541
- if (!options?.isHandledCustomJSONSchema) {
542
- const customJSONSchema = getCustomJSONSchema(schema__._def, options);
543
- if (customJSONSchema) {
544
- const json = zodToJsonSchema(schema__, {
545
- ...options,
546
- isHandledCustomJSONSchema: true
547
- });
548
- return {
549
- ...json,
550
- ...customJSONSchema
551
- };
552
- }
553
- }
554
- const childOptions = { ...options, isHandledCustomJSONSchema: false };
555
- const customType = getCustomZodType(schema__._def);
556
- switch (customType) {
557
- case "Blob": {
558
- return { type: "string", contentMediaType: "*/*" };
559
- }
560
- case "File": {
561
- const mimeType = getCustomZodFileMimeType(schema__._def) ?? "*/*";
562
- return { type: "string", contentMediaType: mimeType };
563
- }
564
- case "Invalid Date": {
565
- return { const: "Invalid Date" };
566
- }
567
- case "RegExp": {
568
- return {
569
- type: "string",
570
- pattern: "^\\/(.*)\\/([a-z]*)$"
571
- };
572
- }
573
- case "URL": {
574
- return { type: "string", format: JSONSchemaFormat.URI };
575
- }
576
- }
577
- const _expectedCustomType = customType;
578
- const typeName = schema__._def.typeName;
579
- switch (typeName) {
580
- case ZodFirstPartyTypeKind2.ZodString: {
581
- const schema_ = schema__;
582
- const json = { type: "string" };
583
- for (const check of schema_._def.checks) {
584
- switch (check.kind) {
585
- case "base64":
586
- json.contentEncoding = "base64";
587
- break;
588
- case "cuid":
589
- json.pattern = "^[0-9A-HJKMNP-TV-Z]{26}$";
590
- break;
591
- case "email":
592
- json.format = JSONSchemaFormat.Email;
593
- break;
594
- case "url":
595
- json.format = JSONSchemaFormat.URI;
596
- break;
597
- case "uuid":
598
- json.format = JSONSchemaFormat.UUID;
599
- break;
600
- case "regex":
601
- json.pattern = check.regex.source;
602
- break;
603
- case "min":
604
- json.minLength = check.value;
605
- break;
606
- case "max":
607
- json.maxLength = check.value;
608
- break;
609
- case "length":
610
- json.minLength = check.value;
611
- json.maxLength = check.value;
612
- break;
613
- case "includes":
614
- json.pattern = escapeStringRegexp(check.value);
615
- break;
616
- case "startsWith":
617
- json.pattern = `^${escapeStringRegexp(check.value)}`;
618
- break;
619
- case "endsWith":
620
- json.pattern = `${escapeStringRegexp(check.value)}$`;
621
- break;
622
- case "emoji":
623
- json.pattern = "^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$";
624
- break;
625
- case "nanoid":
626
- json.pattern = "^[a-zA-Z0-9_-]{21}$";
627
- break;
628
- case "cuid2":
629
- json.pattern = "^[0-9a-z]+$";
630
- break;
631
- case "ulid":
632
- json.pattern = "^[0-9A-HJKMNP-TV-Z]{26}$";
633
- break;
634
- case "datetime":
635
- json.format = JSONSchemaFormat.DateTime;
636
- break;
637
- case "date":
638
- json.format = JSONSchemaFormat.Date;
639
- break;
640
- case "time":
641
- json.format = JSONSchemaFormat.Time;
642
- break;
643
- case "duration":
644
- json.format = JSONSchemaFormat.Duration;
645
- break;
646
- case "ip":
647
- json.format = JSONSchemaFormat.IPv4;
648
- break;
649
- case "jwt":
650
- json.pattern = "^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]*$";
651
- break;
652
- case "base64url":
653
- json.pattern = "^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$";
654
- break;
655
- default: {
656
- const _expect = check.kind;
657
- }
658
- }
659
- }
660
- return json;
661
- }
662
- case ZodFirstPartyTypeKind2.ZodNumber: {
663
- const schema_ = schema__;
664
- const json = { type: "number" };
665
- for (const check of schema_._def.checks) {
666
- switch (check.kind) {
667
- case "int":
668
- json.type = "integer";
669
- break;
670
- case "min":
671
- json.minimum = check.value;
672
- break;
673
- case "max":
674
- json.maximum = check.value;
675
- break;
676
- case "multipleOf":
677
- json.multipleOf = check.value;
678
- break;
679
- default: {
680
- const _expect = check.kind;
681
- }
682
- }
683
- }
684
- return json;
685
- }
686
- case ZodFirstPartyTypeKind2.ZodNaN: {
687
- return { const: "NaN" };
688
- }
689
- case ZodFirstPartyTypeKind2.ZodBigInt: {
690
- const json = { type: "string", pattern: "^-?[0-9]+$" };
691
- return json;
692
- }
693
- case ZodFirstPartyTypeKind2.ZodBoolean: {
694
- return { type: "boolean" };
695
- }
696
- case ZodFirstPartyTypeKind2.ZodDate: {
697
- const schema2 = { type: "string", format: JSONSchemaFormat.Date };
698
- return schema2;
699
- }
700
- case ZodFirstPartyTypeKind2.ZodNull: {
701
- return { type: "null" };
702
- }
703
- case ZodFirstPartyTypeKind2.ZodVoid:
704
- case ZodFirstPartyTypeKind2.ZodUndefined: {
705
- return UNDEFINED_JSON_SCHEMA;
706
- }
707
- case ZodFirstPartyTypeKind2.ZodLiteral: {
708
- const schema_ = schema__;
709
- return { const: schema_._def.value };
710
- }
711
- case ZodFirstPartyTypeKind2.ZodEnum: {
712
- const schema_ = schema__;
713
- return {
714
- enum: schema_._def.values
715
- };
716
- }
717
- case ZodFirstPartyTypeKind2.ZodNativeEnum: {
718
- const schema_ = schema__;
719
- return {
720
- enum: Object.values(schema_._def.values)
721
- };
722
- }
723
- case ZodFirstPartyTypeKind2.ZodArray: {
724
- const schema_ = schema__;
725
- const def = schema_._def;
726
- const json = { type: "array" };
727
- if (def.exactLength) {
728
- json.maxItems = def.exactLength.value;
729
- json.minItems = def.exactLength.value;
730
- }
731
- if (def.minLength) {
732
- json.minItems = def.minLength.value;
733
- }
734
- if (def.maxLength) {
735
- json.maxItems = def.maxLength.value;
736
- }
737
- return json;
738
- }
739
- case ZodFirstPartyTypeKind2.ZodTuple: {
740
- const schema_ = schema__;
741
- const prefixItems = [];
742
- const json = { type: "array" };
743
- for (const item of schema_._def.items) {
744
- prefixItems.push(zodToJsonSchema(item, childOptions));
745
- }
746
- if (prefixItems?.length) {
747
- json.prefixItems = prefixItems;
748
- }
749
- if (schema_._def.rest) {
750
- const items = zodToJsonSchema(schema_._def.rest, childOptions);
751
- if (items) {
752
- json.items = items;
753
- }
754
- }
755
- return json;
756
- }
757
- case ZodFirstPartyTypeKind2.ZodObject: {
758
- const schema_ = schema__;
759
- const json = { type: "object" };
760
- const properties = {};
761
- const required = [];
762
- for (const [key, value] of Object.entries(schema_.shape)) {
763
- const { schema: schema2, matches } = extractJSONSchema(
764
- zodToJsonSchema(value, childOptions),
765
- (schema3) => schema3 === UNDEFINED_JSON_SCHEMA
766
- );
767
- if (schema2) {
768
- properties[key] = schema2;
769
- }
770
- if (matches.length === 0) {
771
- required.push(key);
772
- }
773
- }
774
- if (Object.keys(properties).length) {
775
- json.properties = properties;
776
- }
777
- if (required.length) {
778
- json.required = required;
779
- }
780
- const additionalProperties = zodToJsonSchema(
781
- schema_._def.catchall,
782
- childOptions
783
- );
784
- if (schema_._def.unknownKeys === "strict") {
785
- json.additionalProperties = additionalProperties === UNSUPPORTED_JSON_SCHEMA ? false : additionalProperties;
786
- } else {
787
- if (additionalProperties && additionalProperties !== UNSUPPORTED_JSON_SCHEMA) {
788
- json.additionalProperties = additionalProperties;
789
- }
790
- }
791
- return json;
792
- }
793
- case ZodFirstPartyTypeKind2.ZodRecord: {
794
- const schema_ = schema__;
795
- const json = { type: "object" };
796
- json.additionalProperties = zodToJsonSchema(
797
- schema_._def.valueType,
798
- childOptions
799
- );
800
- return json;
801
- }
802
- case ZodFirstPartyTypeKind2.ZodSet: {
803
- const schema_ = schema__;
804
- return {
805
- type: "array",
806
- items: zodToJsonSchema(schema_._def.valueType, childOptions)
807
- };
808
- }
809
- case ZodFirstPartyTypeKind2.ZodMap: {
810
- const schema_ = schema__;
811
- return {
812
- type: "array",
813
- items: {
814
- type: "array",
815
- prefixItems: [
816
- zodToJsonSchema(schema_._def.keyType, childOptions),
817
- zodToJsonSchema(schema_._def.valueType, childOptions)
818
- ],
819
- maxItems: 2,
820
- minItems: 2
821
- }
822
- };
823
- }
824
- case ZodFirstPartyTypeKind2.ZodUnion:
825
- case ZodFirstPartyTypeKind2.ZodDiscriminatedUnion: {
826
- const schema_ = schema__;
827
- const anyOf = [];
828
- for (const s of schema_._def.options) {
829
- anyOf.push(zodToJsonSchema(s, childOptions));
830
- }
831
- return { anyOf };
832
- }
833
- case ZodFirstPartyTypeKind2.ZodIntersection: {
834
- const schema_ = schema__;
835
- const allOf = [];
836
- for (const s of [schema_._def.left, schema_._def.right]) {
837
- allOf.push(zodToJsonSchema(s, childOptions));
838
- }
839
- return { allOf };
840
- }
841
- case ZodFirstPartyTypeKind2.ZodLazy: {
842
- const schema_ = schema__;
843
- const maxLazyDepth = childOptions?.maxLazyDepth ?? 5;
844
- const lazyDepth = childOptions?.lazyDepth ?? 0;
845
- if (lazyDepth > maxLazyDepth) {
846
- return {};
847
- }
848
- return zodToJsonSchema(schema_._def.getter(), {
849
- ...childOptions,
850
- lazyDepth: lazyDepth + 1
851
- });
852
- }
853
- case ZodFirstPartyTypeKind2.ZodUnknown:
854
- case ZodFirstPartyTypeKind2.ZodAny:
855
- case void 0: {
856
- return {};
857
- }
858
- case ZodFirstPartyTypeKind2.ZodOptional: {
859
- const schema_ = schema__;
860
- const inner = zodToJsonSchema(schema_._def.innerType, childOptions);
861
- return {
862
- anyOf: [UNDEFINED_JSON_SCHEMA, inner]
863
- };
864
- }
865
- case ZodFirstPartyTypeKind2.ZodReadonly: {
866
- const schema_ = schema__;
867
- return zodToJsonSchema(schema_._def.innerType, childOptions);
868
- }
869
- case ZodFirstPartyTypeKind2.ZodDefault: {
870
- const schema_ = schema__;
871
- return zodToJsonSchema(schema_._def.innerType, childOptions);
872
- }
873
- case ZodFirstPartyTypeKind2.ZodEffects: {
874
- const schema_ = schema__;
875
- if (schema_._def.effect.type === "transform" && childOptions?.mode === "output") {
876
- return {};
877
- }
878
- return zodToJsonSchema(schema_._def.schema, childOptions);
879
- }
880
- case ZodFirstPartyTypeKind2.ZodCatch: {
881
- const schema_ = schema__;
882
- return zodToJsonSchema(schema_._def.innerType, childOptions);
883
- }
884
- case ZodFirstPartyTypeKind2.ZodBranded: {
885
- const schema_ = schema__;
886
- return zodToJsonSchema(schema_._def.type, childOptions);
887
- }
888
- case ZodFirstPartyTypeKind2.ZodPipeline: {
889
- const schema_ = schema__;
890
- return zodToJsonSchema(
891
- childOptions?.mode === "output" ? schema_._def.out : schema_._def.in,
892
- childOptions
893
- );
894
- }
895
- case ZodFirstPartyTypeKind2.ZodNullable: {
896
- const schema_ = schema__;
897
- const inner = zodToJsonSchema(schema_._def.innerType, childOptions);
898
- return {
899
- anyOf: [{ type: "null" }, inner]
900
- };
901
- }
902
- }
903
- const _expected = typeName;
904
- return UNSUPPORTED_JSON_SCHEMA;
905
- }
906
- function extractJSONSchema(schema, check, matches = []) {
907
- if (check(schema)) {
908
- matches.push(schema);
909
- return { schema: void 0, matches };
910
- }
911
- if (typeof schema === "boolean") {
912
- return { schema, matches };
913
- }
914
- if (schema.anyOf && Object.keys(schema).every(
915
- (k) => k === "anyOf" || NON_LOGIC_KEYWORDS.includes(k)
916
- )) {
917
- const anyOf = schema.anyOf.map((s) => extractJSONSchema(s, check, matches).schema).filter((v) => !!v);
918
- if (anyOf.length === 1 && typeof anyOf[0] === "object") {
919
- return { schema: { ...schema, anyOf: void 0, ...anyOf[0] }, matches };
920
- }
921
- return {
922
- schema: {
923
- ...schema,
924
- anyOf
925
- },
926
- matches
927
- };
928
- }
929
- if (schema.oneOf && Object.keys(schema).every(
930
- (k) => k === "oneOf" || NON_LOGIC_KEYWORDS.includes(k)
931
- )) {
932
- const oneOf = schema.oneOf.map((s) => extractJSONSchema(s, check, matches).schema).filter((v) => !!v);
933
- if (oneOf.length === 1 && typeof oneOf[0] === "object") {
934
- return { schema: { ...schema, oneOf: void 0, ...oneOf[0] }, matches };
935
- }
936
- return {
937
- schema: {
938
- ...schema,
939
- oneOf
940
- },
941
- matches
942
- };
943
- }
944
- return { schema, matches };
945
- }
946
- var ZodToJsonSchemaConverter = class {
947
- condition(schema) {
948
- return Boolean(schema && schema["~standard"].vendor === "zod");
949
- }
950
- convert(schema, options) {
951
- const jsonSchema = schema;
952
- return zodToJsonSchema(jsonSchema, { mode: options.strategy });
953
- }
954
- };
955
- export {
956
- NON_LOGIC_KEYWORDS,
957
- UNDEFINED_JSON_SCHEMA,
958
- UNSUPPORTED_JSON_SCHEMA,
959
- ZodCoercer,
960
- ZodToJsonSchemaConverter,
961
- blob,
962
- file,
963
- getCustomJSONSchema,
964
- getCustomZodFileMimeType,
965
- getCustomZodType,
966
- invalidDate,
967
- openapi,
968
- oz,
969
- regexp,
970
- url,
971
- zodToJsonSchema
972
- };
973
- //# sourceMappingURL=index.js.map