lkd-web-kit 0.4.8 → 0.4.9

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.
Files changed (132) hide show
  1. package/dist/hooks/useZodConfig.cjs +2 -2
  2. package/dist/hooks/useZodConfig.js +2 -2
  3. package/dist/index.d.ts +2 -2
  4. package/package.json +7 -7
  5. package/dist/node_modules/zod/v4/classic/coerce.cjs +0 -28
  6. package/dist/node_modules/zod/v4/classic/coerce.js +0 -20
  7. package/dist/node_modules/zod/v4/classic/compat.cjs +0 -37
  8. package/dist/node_modules/zod/v4/classic/compat.js +0 -30
  9. package/dist/node_modules/zod/v4/classic/errors.cjs +0 -49
  10. package/dist/node_modules/zod/v4/classic/errors.js +0 -44
  11. package/dist/node_modules/zod/v4/classic/external.cjs +0 -231
  12. package/dist/node_modules/zod/v4/classic/external.js +0 -28
  13. package/dist/node_modules/zod/v4/classic/index.cjs +0 -10
  14. package/dist/node_modules/zod/v4/classic/index.js +0 -5
  15. package/dist/node_modules/zod/v4/classic/iso.cjs +0 -46
  16. package/dist/node_modules/zod/v4/classic/iso.js +0 -35
  17. package/dist/node_modules/zod/v4/classic/parse.cjs +0 -16
  18. package/dist/node_modules/zod/v4/classic/parse.js +0 -9
  19. package/dist/node_modules/zod/v4/classic/schemas.cjs +0 -1155
  20. package/dist/node_modules/zod/v4/classic/schemas.js +0 -1006
  21. package/dist/node_modules/zod/v4/core/api.cjs +0 -1016
  22. package/dist/node_modules/zod/v4/core/api.js +0 -903
  23. package/dist/node_modules/zod/v4/core/checks.cjs +0 -587
  24. package/dist/node_modules/zod/v4/core/checks.js +0 -562
  25. package/dist/node_modules/zod/v4/core/core.cjs +0 -72
  26. package/dist/node_modules/zod/v4/core/core.js +0 -63
  27. package/dist/node_modules/zod/v4/core/doc.cjs +0 -41
  28. package/dist/node_modules/zod/v4/core/doc.js +0 -37
  29. package/dist/node_modules/zod/v4/core/errors.cjs +0 -204
  30. package/dist/node_modules/zod/v4/core/errors.js +0 -194
  31. package/dist/node_modules/zod/v4/core/function.cjs +0 -81
  32. package/dist/node_modules/zod/v4/core/function.js +0 -76
  33. package/dist/node_modules/zod/v4/core/index.cjs +0 -261
  34. package/dist/node_modules/zod/v4/core/index.js +0 -20
  35. package/dist/node_modules/zod/v4/core/json-schema.cjs +0 -2
  36. package/dist/node_modules/zod/v4/core/json-schema.js +0 -1
  37. package/dist/node_modules/zod/v4/core/parse.cjs +0 -71
  38. package/dist/node_modules/zod/v4/core/parse.js +0 -60
  39. package/dist/node_modules/zod/v4/core/regexes.cjs +0 -139
  40. package/dist/node_modules/zod/v4/core/regexes.js +0 -95
  41. package/dist/node_modules/zod/v4/core/registries.cjs +0 -52
  42. package/dist/node_modules/zod/v4/core/registries.js +0 -44
  43. package/dist/node_modules/zod/v4/core/schemas.cjs +0 -1793
  44. package/dist/node_modules/zod/v4/core/schemas.js +0 -1720
  45. package/dist/node_modules/zod/v4/core/to-json-schema.cjs +0 -823
  46. package/dist/node_modules/zod/v4/core/to-json-schema.js +0 -818
  47. package/dist/node_modules/zod/v4/core/util.cjs +0 -543
  48. package/dist/node_modules/zod/v4/core/util.js +0 -491
  49. package/dist/node_modules/zod/v4/core/versions.cjs +0 -11
  50. package/dist/node_modules/zod/v4/core/versions.js +0 -7
  51. package/dist/node_modules/zod/v4/index.cjs +0 -10
  52. package/dist/node_modules/zod/v4/index.js +0 -5
  53. package/dist/node_modules/zod/v4/locales/ar.cjs +0 -121
  54. package/dist/node_modules/zod/v4/locales/ar.js +0 -119
  55. package/dist/node_modules/zod/v4/locales/az.cjs +0 -120
  56. package/dist/node_modules/zod/v4/locales/az.js +0 -118
  57. package/dist/node_modules/zod/v4/locales/be.cjs +0 -169
  58. package/dist/node_modules/zod/v4/locales/be.js +0 -167
  59. package/dist/node_modules/zod/v4/locales/ca.cjs +0 -123
  60. package/dist/node_modules/zod/v4/locales/ca.js +0 -121
  61. package/dist/node_modules/zod/v4/locales/cs.cjs +0 -140
  62. package/dist/node_modules/zod/v4/locales/cs.js +0 -138
  63. package/dist/node_modules/zod/v4/locales/de.cjs +0 -121
  64. package/dist/node_modules/zod/v4/locales/de.js +0 -119
  65. package/dist/node_modules/zod/v4/locales/en.cjs +0 -125
  66. package/dist/node_modules/zod/v4/locales/en.js +0 -120
  67. package/dist/node_modules/zod/v4/locales/eo.cjs +0 -124
  68. package/dist/node_modules/zod/v4/locales/eo.js +0 -119
  69. package/dist/node_modules/zod/v4/locales/es.cjs +0 -122
  70. package/dist/node_modules/zod/v4/locales/es.js +0 -120
  71. package/dist/node_modules/zod/v4/locales/fa.cjs +0 -127
  72. package/dist/node_modules/zod/v4/locales/fa.js +0 -125
  73. package/dist/node_modules/zod/v4/locales/fi.cjs +0 -127
  74. package/dist/node_modules/zod/v4/locales/fi.js +0 -125
  75. package/dist/node_modules/zod/v4/locales/fr-CA.cjs +0 -122
  76. package/dist/node_modules/zod/v4/locales/fr-CA.js +0 -120
  77. package/dist/node_modules/zod/v4/locales/fr.cjs +0 -121
  78. package/dist/node_modules/zod/v4/locales/fr.js +0 -119
  79. package/dist/node_modules/zod/v4/locales/he.cjs +0 -122
  80. package/dist/node_modules/zod/v4/locales/he.js +0 -120
  81. package/dist/node_modules/zod/v4/locales/hu.cjs +0 -122
  82. package/dist/node_modules/zod/v4/locales/hu.js +0 -120
  83. package/dist/node_modules/zod/v4/locales/id.cjs +0 -121
  84. package/dist/node_modules/zod/v4/locales/id.js +0 -119
  85. package/dist/node_modules/zod/v4/locales/index.cjs +0 -85
  86. package/dist/node_modules/zod/v4/locales/index.js +0 -39
  87. package/dist/node_modules/zod/v4/locales/it.cjs +0 -122
  88. package/dist/node_modules/zod/v4/locales/it.js +0 -120
  89. package/dist/node_modules/zod/v4/locales/ja.cjs +0 -120
  90. package/dist/node_modules/zod/v4/locales/ja.js +0 -118
  91. package/dist/node_modules/zod/v4/locales/kh.cjs +0 -122
  92. package/dist/node_modules/zod/v4/locales/kh.js +0 -120
  93. package/dist/node_modules/zod/v4/locales/ko.cjs +0 -126
  94. package/dist/node_modules/zod/v4/locales/ko.js +0 -124
  95. package/dist/node_modules/zod/v4/locales/mk.cjs +0 -123
  96. package/dist/node_modules/zod/v4/locales/mk.js +0 -121
  97. package/dist/node_modules/zod/v4/locales/ms.cjs +0 -121
  98. package/dist/node_modules/zod/v4/locales/ms.js +0 -119
  99. package/dist/node_modules/zod/v4/locales/nl.cjs +0 -122
  100. package/dist/node_modules/zod/v4/locales/nl.js +0 -120
  101. package/dist/node_modules/zod/v4/locales/no.cjs +0 -121
  102. package/dist/node_modules/zod/v4/locales/no.js +0 -119
  103. package/dist/node_modules/zod/v4/locales/ota.cjs +0 -122
  104. package/dist/node_modules/zod/v4/locales/ota.js +0 -120
  105. package/dist/node_modules/zod/v4/locales/pl.cjs +0 -122
  106. package/dist/node_modules/zod/v4/locales/pl.js +0 -120
  107. package/dist/node_modules/zod/v4/locales/ps.cjs +0 -127
  108. package/dist/node_modules/zod/v4/locales/ps.js +0 -125
  109. package/dist/node_modules/zod/v4/locales/pt.cjs +0 -121
  110. package/dist/node_modules/zod/v4/locales/pt.js +0 -119
  111. package/dist/node_modules/zod/v4/locales/ru.cjs +0 -169
  112. package/dist/node_modules/zod/v4/locales/ru.js +0 -167
  113. package/dist/node_modules/zod/v4/locales/sl.cjs +0 -122
  114. package/dist/node_modules/zod/v4/locales/sl.js +0 -120
  115. package/dist/node_modules/zod/v4/locales/sv.cjs +0 -123
  116. package/dist/node_modules/zod/v4/locales/sv.js +0 -121
  117. package/dist/node_modules/zod/v4/locales/ta.cjs +0 -122
  118. package/dist/node_modules/zod/v4/locales/ta.js +0 -120
  119. package/dist/node_modules/zod/v4/locales/th.cjs +0 -122
  120. package/dist/node_modules/zod/v4/locales/th.js +0 -120
  121. package/dist/node_modules/zod/v4/locales/tr.cjs +0 -123
  122. package/dist/node_modules/zod/v4/locales/tr.js +0 -118
  123. package/dist/node_modules/zod/v4/locales/ua.cjs +0 -122
  124. package/dist/node_modules/zod/v4/locales/ua.js +0 -120
  125. package/dist/node_modules/zod/v4/locales/ur.cjs +0 -122
  126. package/dist/node_modules/zod/v4/locales/ur.js +0 -120
  127. package/dist/node_modules/zod/v4/locales/vi.cjs +0 -121
  128. package/dist/node_modules/zod/v4/locales/vi.js +0 -119
  129. package/dist/node_modules/zod/v4/locales/zh-CN.cjs +0 -121
  130. package/dist/node_modules/zod/v4/locales/zh-CN.js +0 -119
  131. package/dist/node_modules/zod/v4/locales/zh-TW.cjs +0 -122
  132. package/dist/node_modules/zod/v4/locales/zh-TW.js +0 -120
@@ -1,823 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const registries = require('./registries.cjs');
6
- const util = require('./util.cjs');
7
-
8
- class JSONSchemaGenerator {
9
- constructor(params) {
10
- this.counter = 0;
11
- this.metadataRegistry = params?.metadata ?? registries.globalRegistry;
12
- this.target = params?.target ?? "draft-2020-12";
13
- this.unrepresentable = params?.unrepresentable ?? "throw";
14
- this.override = params?.override ?? (() => { });
15
- this.io = params?.io ?? "output";
16
- this.seen = new Map();
17
- }
18
- process(schema, _params = { path: [], schemaPath: [] }) {
19
- var _a;
20
- const def = schema._zod.def;
21
- const formatMap = {
22
- guid: "uuid",
23
- url: "uri",
24
- datetime: "date-time",
25
- json_string: "json-string",
26
- regex: "", // do not set
27
- };
28
- // check for schema in seens
29
- const seen = this.seen.get(schema);
30
- if (seen) {
31
- seen.count++;
32
- // check if cycle
33
- const isCycle = _params.schemaPath.includes(schema);
34
- if (isCycle) {
35
- seen.cycle = _params.path;
36
- }
37
- return seen.schema;
38
- }
39
- // initialize
40
- const result = { schema: {}, count: 1, cycle: undefined, path: _params.path };
41
- this.seen.set(schema, result);
42
- // custom method overrides default behavior
43
- const overrideSchema = schema._zod.toJSONSchema?.();
44
- if (overrideSchema) {
45
- result.schema = overrideSchema;
46
- }
47
- else {
48
- const params = {
49
- ..._params,
50
- schemaPath: [..._params.schemaPath, schema],
51
- path: _params.path,
52
- };
53
- const parent = schema._zod.parent;
54
- if (parent) {
55
- // schema was cloned from another schema
56
- result.ref = parent;
57
- this.process(parent, params);
58
- this.seen.get(parent).isParent = true;
59
- }
60
- else {
61
- const _json = result.schema;
62
- switch (def.type) {
63
- case "string": {
64
- const json = _json;
65
- json.type = "string";
66
- const { minimum, maximum, format, patterns, contentEncoding } = schema._zod
67
- .bag;
68
- if (typeof minimum === "number")
69
- json.minLength = minimum;
70
- if (typeof maximum === "number")
71
- json.maxLength = maximum;
72
- // custom pattern overrides format
73
- if (format) {
74
- json.format = formatMap[format] ?? format;
75
- if (json.format === "")
76
- delete json.format; // empty format is not valid
77
- }
78
- if (contentEncoding)
79
- json.contentEncoding = contentEncoding;
80
- if (patterns && patterns.size > 0) {
81
- const regexes = [...patterns];
82
- if (regexes.length === 1)
83
- json.pattern = regexes[0].source;
84
- else if (regexes.length > 1) {
85
- result.schema.allOf = [
86
- ...regexes.map((regex) => ({
87
- ...(this.target === "draft-7" ? { type: "string" } : {}),
88
- pattern: regex.source,
89
- })),
90
- ];
91
- }
92
- }
93
- break;
94
- }
95
- case "number": {
96
- const json = _json;
97
- const { minimum, maximum, format, multipleOf, exclusiveMaximum, exclusiveMinimum } = schema._zod.bag;
98
- if (typeof format === "string" && format.includes("int"))
99
- json.type = "integer";
100
- else
101
- json.type = "number";
102
- if (typeof exclusiveMinimum === "number")
103
- json.exclusiveMinimum = exclusiveMinimum;
104
- if (typeof minimum === "number") {
105
- json.minimum = minimum;
106
- if (typeof exclusiveMinimum === "number") {
107
- if (exclusiveMinimum >= minimum)
108
- delete json.minimum;
109
- else
110
- delete json.exclusiveMinimum;
111
- }
112
- }
113
- if (typeof exclusiveMaximum === "number")
114
- json.exclusiveMaximum = exclusiveMaximum;
115
- if (typeof maximum === "number") {
116
- json.maximum = maximum;
117
- if (typeof exclusiveMaximum === "number") {
118
- if (exclusiveMaximum <= maximum)
119
- delete json.maximum;
120
- else
121
- delete json.exclusiveMaximum;
122
- }
123
- }
124
- if (typeof multipleOf === "number")
125
- json.multipleOf = multipleOf;
126
- break;
127
- }
128
- case "boolean": {
129
- const json = _json;
130
- json.type = "boolean";
131
- break;
132
- }
133
- case "bigint": {
134
- if (this.unrepresentable === "throw") {
135
- throw new Error("BigInt cannot be represented in JSON Schema");
136
- }
137
- break;
138
- }
139
- case "symbol": {
140
- if (this.unrepresentable === "throw") {
141
- throw new Error("Symbols cannot be represented in JSON Schema");
142
- }
143
- break;
144
- }
145
- case "null": {
146
- _json.type = "null";
147
- break;
148
- }
149
- case "any": {
150
- break;
151
- }
152
- case "unknown": {
153
- break;
154
- }
155
- case "undefined":
156
- case "never": {
157
- _json.not = {};
158
- break;
159
- }
160
- case "void": {
161
- if (this.unrepresentable === "throw") {
162
- throw new Error("Void cannot be represented in JSON Schema");
163
- }
164
- break;
165
- }
166
- case "date": {
167
- if (this.unrepresentable === "throw") {
168
- throw new Error("Date cannot be represented in JSON Schema");
169
- }
170
- break;
171
- }
172
- case "array": {
173
- const json = _json;
174
- const { minimum, maximum } = schema._zod.bag;
175
- if (typeof minimum === "number")
176
- json.minItems = minimum;
177
- if (typeof maximum === "number")
178
- json.maxItems = maximum;
179
- json.type = "array";
180
- json.items = this.process(def.element, { ...params, path: [...params.path, "items"] });
181
- break;
182
- }
183
- case "object": {
184
- const json = _json;
185
- json.type = "object";
186
- json.properties = {};
187
- const shape = def.shape; // params.shapeCache.get(schema)!;
188
- for (const key in shape) {
189
- json.properties[key] = this.process(shape[key], {
190
- ...params,
191
- path: [...params.path, "properties", key],
192
- });
193
- }
194
- // required keys
195
- const allKeys = new Set(Object.keys(shape));
196
- // const optionalKeys = new Set(def.optional);
197
- const requiredKeys = new Set([...allKeys].filter((key) => {
198
- const v = def.shape[key]._zod;
199
- if (this.io === "input") {
200
- return v.optin === undefined;
201
- }
202
- else {
203
- return v.optout === undefined;
204
- }
205
- }));
206
- if (requiredKeys.size > 0) {
207
- json.required = Array.from(requiredKeys);
208
- }
209
- // catchall
210
- if (def.catchall?._zod.def.type === "never") {
211
- // strict
212
- json.additionalProperties = false;
213
- }
214
- else if (!def.catchall) {
215
- // regular
216
- if (this.io === "output")
217
- json.additionalProperties = false;
218
- }
219
- else if (def.catchall) {
220
- json.additionalProperties = this.process(def.catchall, {
221
- ...params,
222
- path: [...params.path, "additionalProperties"],
223
- });
224
- }
225
- break;
226
- }
227
- case "union": {
228
- const json = _json;
229
- json.anyOf = def.options.map((x, i) => this.process(x, {
230
- ...params,
231
- path: [...params.path, "anyOf", i],
232
- }));
233
- break;
234
- }
235
- case "intersection": {
236
- const json = _json;
237
- const a = this.process(def.left, {
238
- ...params,
239
- path: [...params.path, "allOf", 0],
240
- });
241
- const b = this.process(def.right, {
242
- ...params,
243
- path: [...params.path, "allOf", 1],
244
- });
245
- const isSimpleIntersection = (val) => "allOf" in val && Object.keys(val).length === 1;
246
- const allOf = [
247
- ...(isSimpleIntersection(a) ? a.allOf : [a]),
248
- ...(isSimpleIntersection(b) ? b.allOf : [b]),
249
- ];
250
- json.allOf = allOf;
251
- break;
252
- }
253
- case "tuple": {
254
- const json = _json;
255
- json.type = "array";
256
- const prefixItems = def.items.map((x, i) => this.process(x, { ...params, path: [...params.path, "prefixItems", i] }));
257
- if (this.target === "draft-2020-12") {
258
- json.prefixItems = prefixItems;
259
- }
260
- else {
261
- json.items = prefixItems;
262
- }
263
- if (def.rest) {
264
- const rest = this.process(def.rest, {
265
- ...params,
266
- path: [...params.path, "items"],
267
- });
268
- if (this.target === "draft-2020-12") {
269
- json.items = rest;
270
- }
271
- else {
272
- json.additionalItems = rest;
273
- }
274
- }
275
- // additionalItems
276
- if (def.rest) {
277
- json.items = this.process(def.rest, {
278
- ...params,
279
- path: [...params.path, "items"],
280
- });
281
- }
282
- // length
283
- const { minimum, maximum } = schema._zod.bag;
284
- if (typeof minimum === "number")
285
- json.minItems = minimum;
286
- if (typeof maximum === "number")
287
- json.maxItems = maximum;
288
- break;
289
- }
290
- case "record": {
291
- const json = _json;
292
- json.type = "object";
293
- json.propertyNames = this.process(def.keyType, { ...params, path: [...params.path, "propertyNames"] });
294
- json.additionalProperties = this.process(def.valueType, {
295
- ...params,
296
- path: [...params.path, "additionalProperties"],
297
- });
298
- break;
299
- }
300
- case "map": {
301
- if (this.unrepresentable === "throw") {
302
- throw new Error("Map cannot be represented in JSON Schema");
303
- }
304
- break;
305
- }
306
- case "set": {
307
- if (this.unrepresentable === "throw") {
308
- throw new Error("Set cannot be represented in JSON Schema");
309
- }
310
- break;
311
- }
312
- case "enum": {
313
- const json = _json;
314
- const values = util.getEnumValues(def.entries);
315
- // Number enums can have both string and number values
316
- if (values.every((v) => typeof v === "number"))
317
- json.type = "number";
318
- if (values.every((v) => typeof v === "string"))
319
- json.type = "string";
320
- json.enum = values;
321
- break;
322
- }
323
- case "literal": {
324
- const json = _json;
325
- const vals = [];
326
- for (const val of def.values) {
327
- if (val === undefined) {
328
- if (this.unrepresentable === "throw") {
329
- throw new Error("Literal `undefined` cannot be represented in JSON Schema");
330
- }
331
- }
332
- else if (typeof val === "bigint") {
333
- if (this.unrepresentable === "throw") {
334
- throw new Error("BigInt literals cannot be represented in JSON Schema");
335
- }
336
- else {
337
- vals.push(Number(val));
338
- }
339
- }
340
- else {
341
- vals.push(val);
342
- }
343
- }
344
- if (vals.length === 0) ;
345
- else if (vals.length === 1) {
346
- const val = vals[0];
347
- json.type = val === null ? "null" : typeof val;
348
- json.const = val;
349
- }
350
- else {
351
- if (vals.every((v) => typeof v === "number"))
352
- json.type = "number";
353
- if (vals.every((v) => typeof v === "string"))
354
- json.type = "string";
355
- if (vals.every((v) => typeof v === "boolean"))
356
- json.type = "string";
357
- if (vals.every((v) => v === null))
358
- json.type = "null";
359
- json.enum = vals;
360
- }
361
- break;
362
- }
363
- case "file": {
364
- const json = _json;
365
- const file = {
366
- type: "string",
367
- format: "binary",
368
- contentEncoding: "binary",
369
- };
370
- const { minimum, maximum, mime } = schema._zod.bag;
371
- if (minimum !== undefined)
372
- file.minLength = minimum;
373
- if (maximum !== undefined)
374
- file.maxLength = maximum;
375
- if (mime) {
376
- if (mime.length === 1) {
377
- file.contentMediaType = mime[0];
378
- Object.assign(json, file);
379
- }
380
- else {
381
- json.anyOf = mime.map((m) => {
382
- const mFile = { ...file, contentMediaType: m };
383
- return mFile;
384
- });
385
- }
386
- }
387
- else {
388
- Object.assign(json, file);
389
- }
390
- // if (this.unrepresentable === "throw") {
391
- // throw new Error("File cannot be represented in JSON Schema");
392
- // }
393
- break;
394
- }
395
- case "transform": {
396
- if (this.unrepresentable === "throw") {
397
- throw new Error("Transforms cannot be represented in JSON Schema");
398
- }
399
- break;
400
- }
401
- case "nullable": {
402
- const inner = this.process(def.innerType, params);
403
- _json.anyOf = [inner, { type: "null" }];
404
- break;
405
- }
406
- case "nonoptional": {
407
- this.process(def.innerType, params);
408
- result.ref = def.innerType;
409
- break;
410
- }
411
- case "success": {
412
- const json = _json;
413
- json.type = "boolean";
414
- break;
415
- }
416
- case "default": {
417
- this.process(def.innerType, params);
418
- result.ref = def.innerType;
419
- _json.default = JSON.parse(JSON.stringify(def.defaultValue));
420
- break;
421
- }
422
- case "prefault": {
423
- this.process(def.innerType, params);
424
- result.ref = def.innerType;
425
- if (this.io === "input")
426
- _json._prefault = JSON.parse(JSON.stringify(def.defaultValue));
427
- break;
428
- }
429
- case "catch": {
430
- // use conditionals
431
- this.process(def.innerType, params);
432
- result.ref = def.innerType;
433
- let catchValue;
434
- try {
435
- catchValue = def.catchValue(undefined);
436
- }
437
- catch {
438
- throw new Error("Dynamic catch values are not supported in JSON Schema");
439
- }
440
- _json.default = catchValue;
441
- break;
442
- }
443
- case "nan": {
444
- if (this.unrepresentable === "throw") {
445
- throw new Error("NaN cannot be represented in JSON Schema");
446
- }
447
- break;
448
- }
449
- case "template_literal": {
450
- const json = _json;
451
- const pattern = schema._zod.pattern;
452
- if (!pattern)
453
- throw new Error("Pattern not found in template literal");
454
- json.type = "string";
455
- json.pattern = pattern.source;
456
- break;
457
- }
458
- case "pipe": {
459
- const innerType = this.io === "input" ? (def.in._zod.def.type === "transform" ? def.out : def.in) : def.out;
460
- this.process(innerType, params);
461
- result.ref = innerType;
462
- break;
463
- }
464
- case "readonly": {
465
- this.process(def.innerType, params);
466
- result.ref = def.innerType;
467
- _json.readOnly = true;
468
- break;
469
- }
470
- // passthrough types
471
- case "promise": {
472
- this.process(def.innerType, params);
473
- result.ref = def.innerType;
474
- break;
475
- }
476
- case "optional": {
477
- this.process(def.innerType, params);
478
- result.ref = def.innerType;
479
- break;
480
- }
481
- case "lazy": {
482
- const innerType = schema._zod.innerType;
483
- this.process(innerType, params);
484
- result.ref = innerType;
485
- break;
486
- }
487
- case "custom": {
488
- if (this.unrepresentable === "throw") {
489
- throw new Error("Custom types cannot be represented in JSON Schema");
490
- }
491
- break;
492
- }
493
- }
494
- }
495
- }
496
- // metadata
497
- const meta = this.metadataRegistry.get(schema);
498
- if (meta)
499
- Object.assign(result.schema, meta);
500
- if (this.io === "input" && isTransforming(schema)) {
501
- // examples/defaults only apply to output type of pipe
502
- delete result.schema.examples;
503
- delete result.schema.default;
504
- }
505
- // set prefault as default
506
- if (this.io === "input" && result.schema._prefault)
507
- (_a = result.schema).default ?? (_a.default = result.schema._prefault);
508
- delete result.schema._prefault;
509
- // pulling fresh from this.seen in case it was overwritten
510
- const _result = this.seen.get(schema);
511
- return _result.schema;
512
- }
513
- emit(schema, _params) {
514
- const params = {
515
- cycles: _params?.cycles ?? "ref",
516
- reused: _params?.reused ?? "inline",
517
- // unrepresentable: _params?.unrepresentable ?? "throw",
518
- // uri: _params?.uri ?? ((id) => `${id}`),
519
- external: _params?.external ?? undefined,
520
- };
521
- // iterate over seen map;
522
- const root = this.seen.get(schema);
523
- if (!root)
524
- throw new Error("Unprocessed schema. This is a bug in Zod.");
525
- // initialize result with root schema fields
526
- // Object.assign(result, seen.cached);
527
- const makeURI = (entry) => {
528
- // comparing the seen objects because sometimes
529
- // multiple schemas map to the same seen object.
530
- // e.g. lazy
531
- // external is configured
532
- const defsSegment = this.target === "draft-2020-12" ? "$defs" : "definitions";
533
- if (params.external) {
534
- const externalId = params.external.registry.get(entry[0])?.id; // ?? "__shared";// `__schema${this.counter++}`;
535
- // check if schema is in the external registry
536
- if (externalId)
537
- return { ref: params.external.uri(externalId) };
538
- // otherwise, add to __shared
539
- const id = entry[1].defId ?? entry[1].schema.id ?? `schema${this.counter++}`;
540
- entry[1].defId = id;
541
- return { defId: id, ref: `${params.external.uri("__shared")}#/${defsSegment}/${id}` };
542
- }
543
- if (entry[1] === root) {
544
- return { ref: "#" };
545
- }
546
- // self-contained schema
547
- const uriPrefix = `#`;
548
- const defUriPrefix = `${uriPrefix}/${defsSegment}/`;
549
- const defId = entry[1].schema.id ?? `__schema${this.counter++}`;
550
- return { defId, ref: defUriPrefix + defId };
551
- };
552
- // stored cached version in `def` property
553
- // remove all properties, set $ref
554
- const extractToDef = (entry) => {
555
- if (entry[1].schema.$ref) {
556
- return;
557
- }
558
- const seen = entry[1];
559
- const { ref, defId } = makeURI(entry);
560
- seen.def = { ...seen.schema };
561
- // defId won't be set if the schema is a reference to an external schema
562
- if (defId)
563
- seen.defId = defId;
564
- // wipe away all properties except $ref
565
- const schema = seen.schema;
566
- for (const key in schema) {
567
- delete schema[key];
568
- }
569
- schema.$ref = ref;
570
- };
571
- // extract schemas into $defs
572
- for (const entry of this.seen.entries()) {
573
- const seen = entry[1];
574
- // convert root schema to # $ref
575
- // also prevents root schema from being extracted
576
- if (schema === entry[0]) {
577
- // do not copy to defs...this is the root schema
578
- extractToDef(entry);
579
- continue;
580
- }
581
- // extract schemas that are in the external registry
582
- if (params.external) {
583
- const ext = params.external.registry.get(entry[0])?.id;
584
- if (schema !== entry[0] && ext) {
585
- extractToDef(entry);
586
- continue;
587
- }
588
- }
589
- // extract schemas with `id` meta
590
- const id = this.metadataRegistry.get(entry[0])?.id;
591
- if (id) {
592
- extractToDef(entry);
593
- continue;
594
- }
595
- // break cycles
596
- if (seen.cycle) {
597
- if (params.cycles === "throw") {
598
- throw new Error("Cycle detected: " +
599
- `#/${seen.cycle?.join("/")}/<root>` +
600
- '\n\nSet the `cycles` parameter to `"ref"` to resolve cyclical schemas with defs.');
601
- }
602
- else if (params.cycles === "ref") {
603
- extractToDef(entry);
604
- }
605
- continue;
606
- }
607
- // extract reused schemas
608
- if (seen.count > 1) {
609
- if (params.reused === "ref") {
610
- extractToDef(entry);
611
- // biome-ignore lint:
612
- continue;
613
- }
614
- }
615
- }
616
- // flatten _refs
617
- const flattenRef = (zodSchema, params) => {
618
- const seen = this.seen.get(zodSchema);
619
- const schema = seen.def ?? seen.schema;
620
- const _cached = { ...schema };
621
- // already seen
622
- if (seen.ref === null) {
623
- return;
624
- }
625
- // flatten ref if defined
626
- const ref = seen.ref;
627
- seen.ref = null; // prevent recursion
628
- if (ref) {
629
- flattenRef(ref, params);
630
- // merge referenced schema into current
631
- const refSchema = this.seen.get(ref).schema;
632
- if (refSchema.$ref && params.target === "draft-7") {
633
- schema.allOf = schema.allOf ?? [];
634
- schema.allOf.push(refSchema);
635
- }
636
- else {
637
- Object.assign(schema, refSchema);
638
- Object.assign(schema, _cached); // prevent overwriting any fields in the original schema
639
- }
640
- }
641
- // execute overrides
642
- if (!seen.isParent)
643
- this.override({
644
- zodSchema: zodSchema,
645
- jsonSchema: schema,
646
- path: seen.path ?? [],
647
- });
648
- };
649
- for (const entry of [...this.seen.entries()].reverse()) {
650
- flattenRef(entry[0], { target: this.target });
651
- }
652
- const result = {};
653
- if (this.target === "draft-2020-12") {
654
- result.$schema = "https://json-schema.org/draft/2020-12/schema";
655
- }
656
- else if (this.target === "draft-7") {
657
- result.$schema = "http://json-schema.org/draft-07/schema#";
658
- }
659
- else {
660
- console.warn(`Invalid target: ${this.target}`);
661
- }
662
- Object.assign(result, root.def);
663
- // build defs object
664
- const defs = params.external?.defs ?? {};
665
- for (const entry of this.seen.entries()) {
666
- const seen = entry[1];
667
- if (seen.def && seen.defId) {
668
- defs[seen.defId] = seen.def;
669
- }
670
- }
671
- // set definitions in result
672
- if (!params.external && Object.keys(defs).length > 0) {
673
- if (this.target === "draft-2020-12") {
674
- result.$defs = defs;
675
- }
676
- else {
677
- result.definitions = defs;
678
- }
679
- }
680
- try {
681
- // this "finalizes" this schema and ensures all cycles are removed
682
- // each call to .emit() is functionally independent
683
- // though the seen map is shared
684
- return JSON.parse(JSON.stringify(result));
685
- }
686
- catch (_err) {
687
- throw new Error("Error converting schema to JSON.");
688
- }
689
- }
690
- }
691
- function toJSONSchema(input, _params) {
692
- if (input instanceof registries.$ZodRegistry) {
693
- const gen = new JSONSchemaGenerator(_params);
694
- const defs = {};
695
- for (const entry of input._idmap.entries()) {
696
- const [_, schema] = entry;
697
- gen.process(schema);
698
- }
699
- const schemas = {};
700
- const external = {
701
- registry: input,
702
- uri: _params?.uri || ((id) => id),
703
- defs,
704
- };
705
- for (const entry of input._idmap.entries()) {
706
- const [key, schema] = entry;
707
- schemas[key] = gen.emit(schema, {
708
- ..._params,
709
- external,
710
- });
711
- }
712
- if (Object.keys(defs).length > 0) {
713
- const defsSegment = gen.target === "draft-2020-12" ? "$defs" : "definitions";
714
- schemas.__shared = {
715
- [defsSegment]: defs,
716
- };
717
- }
718
- return { schemas };
719
- }
720
- const gen = new JSONSchemaGenerator(_params);
721
- gen.process(input);
722
- return gen.emit(input, _params);
723
- }
724
- function isTransforming(_schema, _ctx) {
725
- const ctx = _ctx ?? { seen: new Set() };
726
- if (ctx.seen.has(_schema))
727
- return false;
728
- ctx.seen.add(_schema);
729
- const schema = _schema;
730
- const def = schema._zod.def;
731
- switch (def.type) {
732
- case "string":
733
- case "number":
734
- case "bigint":
735
- case "boolean":
736
- case "date":
737
- case "symbol":
738
- case "undefined":
739
- case "null":
740
- case "any":
741
- case "unknown":
742
- case "never":
743
- case "void":
744
- case "literal":
745
- case "enum":
746
- case "nan":
747
- case "file":
748
- case "template_literal":
749
- return false;
750
- case "array": {
751
- return isTransforming(def.element, ctx);
752
- }
753
- case "object": {
754
- for (const key in def.shape) {
755
- if (isTransforming(def.shape[key], ctx))
756
- return true;
757
- }
758
- return false;
759
- }
760
- case "union": {
761
- for (const option of def.options) {
762
- if (isTransforming(option, ctx))
763
- return true;
764
- }
765
- return false;
766
- }
767
- case "intersection": {
768
- return isTransforming(def.left, ctx) || isTransforming(def.right, ctx);
769
- }
770
- case "tuple": {
771
- for (const item of def.items) {
772
- if (isTransforming(item, ctx))
773
- return true;
774
- }
775
- if (def.rest && isTransforming(def.rest, ctx))
776
- return true;
777
- return false;
778
- }
779
- case "record": {
780
- return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
781
- }
782
- case "map": {
783
- return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
784
- }
785
- case "set": {
786
- return isTransforming(def.valueType, ctx);
787
- }
788
- // inner types
789
- case "promise":
790
- case "optional":
791
- case "nonoptional":
792
- case "nullable":
793
- case "readonly":
794
- return isTransforming(def.innerType, ctx);
795
- case "lazy":
796
- return isTransforming(def.getter(), ctx);
797
- case "default": {
798
- return isTransforming(def.innerType, ctx);
799
- }
800
- case "prefault": {
801
- return isTransforming(def.innerType, ctx);
802
- }
803
- case "custom": {
804
- return false;
805
- }
806
- case "transform": {
807
- return true;
808
- }
809
- case "pipe": {
810
- return isTransforming(def.in, ctx) || isTransforming(def.out, ctx);
811
- }
812
- case "success": {
813
- return false;
814
- }
815
- case "catch": {
816
- return false;
817
- }
818
- }
819
- throw new Error(`Unknown schema type: ${def.type}`);
820
- }
821
-
822
- exports.JSONSchemaGenerator = JSONSchemaGenerator;
823
- exports.toJSONSchema = toJSONSchema;