@orpc/zod 0.0.0-next.b15d206 → 0.0.0-next.b36125c

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