lkd-web-kit 0.4.3 → 0.4.5

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