@wener/common 1.0.5 → 2.0.1

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 (152) hide show
  1. package/lib/cn/DivisionCode.js +55 -32
  2. package/lib/cn/DivisionCode.test.js +140 -0
  3. package/lib/cn/Mod11Checksum.js +80 -37
  4. package/lib/cn/Mod31Checksum.js +89 -40
  5. package/lib/cn/ResidentIdentityCardNumber.js +16 -16
  6. package/lib/cn/ResidentIdentityCardNumber.test.js +21 -0
  7. package/lib/cn/UnifiedSocialCreditCode.js +32 -26
  8. package/lib/cn/UnifiedSocialCreditCode.test.js +16 -0
  9. package/lib/cn/formatDate.js +5 -7
  10. package/lib/cn/index.js +0 -1
  11. package/lib/cn/parseSex.js +0 -2
  12. package/lib/cn/types.d.js +0 -2
  13. package/lib/consola/createStandardConsolaReporter.js +6 -6
  14. package/lib/consola/formatLogObject.js +133 -32
  15. package/lib/consola/index.js +0 -1
  16. package/lib/data/formatSort.js +5 -6
  17. package/lib/data/formatSort.test.js +34 -0
  18. package/lib/data/index.js +0 -1
  19. package/lib/data/maybeNumber.js +11 -5
  20. package/lib/data/parseSort.js +28 -22
  21. package/lib/data/parseSort.test.js +188 -0
  22. package/lib/data/resolvePagination.js +27 -16
  23. package/lib/data/resolvePagination.test.js +232 -0
  24. package/lib/data/types.d.js +0 -2
  25. package/lib/index.js +0 -1
  26. package/lib/jsonschema/JsonSchema.js +78 -52
  27. package/lib/jsonschema/JsonSchema.test.js +137 -0
  28. package/lib/jsonschema/index.js +0 -1
  29. package/lib/jsonschema/types.d.js +5 -3
  30. package/lib/meta/defineFileType.js +103 -20
  31. package/lib/meta/defineInit.js +250 -31
  32. package/lib/meta/defineMetadata.js +140 -24
  33. package/lib/meta/defineMetadata.test.js +13 -0
  34. package/lib/meta/index.js +0 -1
  35. package/lib/password/PHC.js +87 -63
  36. package/lib/password/PHC.test.js +539 -0
  37. package/lib/password/Password.js +291 -29
  38. package/lib/password/Password.test.js +362 -0
  39. package/lib/password/createArgon2PasswordAlgorithm.js +191 -35
  40. package/lib/password/createBase64PasswordAlgorithm.js +141 -8
  41. package/lib/password/createBcryptPasswordAlgorithm.js +168 -13
  42. package/lib/password/createPBKDF2PasswordAlgorithm.js +228 -46
  43. package/lib/password/createScryptPasswordAlgorithm.js +211 -55
  44. package/lib/password/index.js +0 -1
  45. package/lib/password/server/index.js +0 -1
  46. package/lib/resource/Identifiable.js +1 -0
  47. package/lib/resource/getTitleOfResource.js +10 -0
  48. package/lib/resource/index.js +1 -0
  49. package/lib/resource/schema/AnyResourceSchema.js +89 -0
  50. package/lib/resource/schema/BaseResourceSchema.js +29 -0
  51. package/lib/resource/schema/ResourceActionType.js +118 -0
  52. package/lib/resource/schema/ResourceStatus.js +93 -0
  53. package/lib/resource/schema/ResourceType.js +24 -0
  54. package/lib/resource/schema/SchemaRegistry.js +38 -0
  55. package/lib/resource/schema/SexType.js +10 -0
  56. package/lib/resource/schema/types.js +89 -0
  57. package/lib/resource/schema/types.test.js +14 -0
  58. package/lib/schema/TypeSchema.d.js +1 -0
  59. package/lib/schema/createSchemaData.js +173 -0
  60. package/lib/schema/findJsonSchemaByPath.js +49 -0
  61. package/lib/schema/getSchemaCache.js +11 -0
  62. package/lib/schema/getSchemaOptions.js +24 -0
  63. package/lib/schema/index.js +5 -0
  64. package/lib/schema/toJsonSchema.js +441 -0
  65. package/lib/schema/toJsonSchema.test.js +27 -0
  66. package/lib/schema/validate.js +124 -0
  67. package/lib/search/AdvanceSearch.js +0 -1
  68. package/lib/search/AdvanceSearch.test.js +435 -0
  69. package/lib/search/formatAdvanceSearch.js +41 -27
  70. package/lib/search/index.js +0 -1
  71. package/lib/search/optimizeAdvanceSearch.js +79 -25
  72. package/lib/search/parseAdvanceSearch.js +5 -5
  73. package/lib/search/parser.d.js +0 -2
  74. package/lib/search/parser.js +97 -74
  75. package/lib/search/types.d.js +0 -2
  76. package/lib/tools/generateSchema.js +197 -39
  77. package/lib/tools/renderJsonSchemaToMarkdownDoc.js +143 -55
  78. package/package.json +23 -11
  79. package/src/data/maybeNumber.ts +5 -1
  80. package/src/data/resolvePagination.test.ts +1 -1
  81. package/src/data/resolvePagination.ts +18 -7
  82. package/src/data/types.d.ts +12 -0
  83. package/src/jsonschema/JsonSchema.test.ts +17 -0
  84. package/src/jsonschema/JsonSchema.ts +2 -2
  85. package/src/jsonschema/types.d.ts +63 -12
  86. package/src/resource/Identifiable.ts +3 -0
  87. package/src/resource/getTitleOfResource.tsx +6 -0
  88. package/src/resource/index.ts +3 -0
  89. package/src/resource/schema/AnyResourceSchema.ts +113 -0
  90. package/src/resource/schema/BaseResourceSchema.ts +32 -0
  91. package/src/resource/schema/ResourceActionType.ts +123 -0
  92. package/src/resource/schema/ResourceStatus.ts +94 -0
  93. package/src/resource/schema/ResourceType.ts +25 -0
  94. package/src/resource/schema/SchemaRegistry.ts +42 -0
  95. package/src/resource/schema/SexType.ts +13 -0
  96. package/src/resource/schema/types.test.ts +18 -0
  97. package/src/resource/schema/types.ts +105 -0
  98. package/src/schema/TypeSchema.d.ts +32 -0
  99. package/src/schema/createSchemaData.ts +81 -0
  100. package/src/schema/findJsonSchemaByPath.ts +37 -0
  101. package/src/schema/getSchemaCache.ts +21 -0
  102. package/src/schema/getSchemaOptions.ts +24 -0
  103. package/src/schema/index.ts +6 -0
  104. package/src/schema/toJsonSchema.test.ts +37 -0
  105. package/src/schema/toJsonSchema.ts +200 -0
  106. package/src/schema/validate.ts +135 -0
  107. package/src/tools/generateSchema.ts +28 -28
  108. package/lib/cn/DivisionCode.js.map +0 -1
  109. package/lib/cn/Mod11Checksum.js.map +0 -1
  110. package/lib/cn/Mod31Checksum.js.map +0 -1
  111. package/lib/cn/ResidentIdentityCardNumber.js.map +0 -1
  112. package/lib/cn/UnifiedSocialCreditCode.js.map +0 -1
  113. package/lib/cn/formatDate.js.map +0 -1
  114. package/lib/cn/index.js.map +0 -1
  115. package/lib/cn/parseSex.js.map +0 -1
  116. package/lib/cn/types.d.js.map +0 -1
  117. package/lib/consola/createStandardConsolaReporter.js.map +0 -1
  118. package/lib/consola/formatLogObject.js.map +0 -1
  119. package/lib/consola/index.js.map +0 -1
  120. package/lib/data/formatSort.js.map +0 -1
  121. package/lib/data/index.js.map +0 -1
  122. package/lib/data/maybeNumber.js.map +0 -1
  123. package/lib/data/parseSort.js.map +0 -1
  124. package/lib/data/resolvePagination.js.map +0 -1
  125. package/lib/data/types.d.js.map +0 -1
  126. package/lib/index.js.map +0 -1
  127. package/lib/jsonschema/JsonSchema.js.map +0 -1
  128. package/lib/jsonschema/index.js.map +0 -1
  129. package/lib/jsonschema/types.d.js.map +0 -1
  130. package/lib/meta/defineFileType.js.map +0 -1
  131. package/lib/meta/defineInit.js.map +0 -1
  132. package/lib/meta/defineMetadata.js.map +0 -1
  133. package/lib/meta/index.js.map +0 -1
  134. package/lib/password/PHC.js.map +0 -1
  135. package/lib/password/Password.js.map +0 -1
  136. package/lib/password/createArgon2PasswordAlgorithm.js.map +0 -1
  137. package/lib/password/createBase64PasswordAlgorithm.js.map +0 -1
  138. package/lib/password/createBcryptPasswordAlgorithm.js.map +0 -1
  139. package/lib/password/createPBKDF2PasswordAlgorithm.js.map +0 -1
  140. package/lib/password/createScryptPasswordAlgorithm.js.map +0 -1
  141. package/lib/password/index.js.map +0 -1
  142. package/lib/password/server/index.js.map +0 -1
  143. package/lib/search/AdvanceSearch.js.map +0 -1
  144. package/lib/search/formatAdvanceSearch.js.map +0 -1
  145. package/lib/search/index.js.map +0 -1
  146. package/lib/search/optimizeAdvanceSearch.js.map +0 -1
  147. package/lib/search/parseAdvanceSearch.js.map +0 -1
  148. package/lib/search/parser.d.js.map +0 -1
  149. package/lib/search/parser.js.map +0 -1
  150. package/lib/search/types.d.js.map +0 -1
  151. package/lib/tools/generateSchema.js.map +0 -1
  152. package/lib/tools/renderJsonSchemaToMarkdownDoc.js.map +0 -1
@@ -0,0 +1,441 @@
1
+ function _array_like_to_array(arr, len) {
2
+ if (len == null || len > arr.length)
3
+ len = arr.length;
4
+ for (var i = 0, arr2 = new Array(len); i < len; i++)
5
+ arr2[i] = arr[i];
6
+ return arr2;
7
+ }
8
+ function _array_with_holes(arr) {
9
+ if (Array.isArray(arr))
10
+ return arr;
11
+ }
12
+ function _define_property(obj, key, value) {
13
+ if (key in obj) {
14
+ Object.defineProperty(obj, key, {
15
+ value: value,
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true
19
+ });
20
+ }
21
+ else {
22
+ obj[key] = value;
23
+ }
24
+ return obj;
25
+ }
26
+ function _iterable_to_array_limit(arr, i) {
27
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
28
+ if (_i == null)
29
+ return;
30
+ var _arr = [];
31
+ var _n = true;
32
+ var _d = false;
33
+ var _s, _e;
34
+ try {
35
+ for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
36
+ _arr.push(_s.value);
37
+ if (i && _arr.length === i)
38
+ break;
39
+ }
40
+ }
41
+ catch (err) {
42
+ _d = true;
43
+ _e = err;
44
+ }
45
+ finally {
46
+ try {
47
+ if (!_n && _i["return"] != null)
48
+ _i["return"]();
49
+ }
50
+ finally {
51
+ if (_d)
52
+ throw _e;
53
+ }
54
+ }
55
+ return _arr;
56
+ }
57
+ function _non_iterable_rest() {
58
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
59
+ }
60
+ function _object_spread(target) {
61
+ for (var i = 1; i < arguments.length; i++) {
62
+ var source = arguments[i] != null ? arguments[i] : {};
63
+ var ownKeys = Object.keys(source);
64
+ if (typeof Object.getOwnPropertySymbols === "function") {
65
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
66
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
67
+ }));
68
+ }
69
+ ownKeys.forEach(function (key) {
70
+ _define_property(target, key, source[key]);
71
+ });
72
+ }
73
+ return target;
74
+ }
75
+ function ownKeys(object, enumerableOnly) {
76
+ var keys = Object.keys(object);
77
+ if (Object.getOwnPropertySymbols) {
78
+ var symbols = Object.getOwnPropertySymbols(object);
79
+ if (enumerableOnly) {
80
+ symbols = symbols.filter(function (sym) {
81
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
82
+ });
83
+ }
84
+ keys.push.apply(keys, symbols);
85
+ }
86
+ return keys;
87
+ }
88
+ function _object_spread_props(target, source) {
89
+ source = source != null ? source : {};
90
+ if (Object.getOwnPropertyDescriptors) {
91
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
92
+ }
93
+ else {
94
+ ownKeys(Object(source)).forEach(function (key) {
95
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
96
+ });
97
+ }
98
+ return target;
99
+ }
100
+ function _sliced_to_array(arr, i) {
101
+ return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
102
+ }
103
+ function _type_of(obj) {
104
+ "@swc/helpers - typeof";
105
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
106
+ }
107
+ function _unsupported_iterable_to_array(o, minLen) {
108
+ if (!o)
109
+ return;
110
+ if (typeof o === "string")
111
+ return _array_like_to_array(o, minLen);
112
+ var n = Object.prototype.toString.call(o).slice(8, -1);
113
+ if (n === "Object" && o.constructor)
114
+ n = o.constructor.name;
115
+ if (n === "Map" || n === "Set")
116
+ return Array.from(n);
117
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))
118
+ return _array_like_to_array(o, minLen);
119
+ }
120
+ import { omit, remove } from "es-toolkit";
121
+ import { match, P } from "ts-pattern";
122
+ import { z } from "zod/v4";
123
+ import { getSchemaCache } from "./getSchemaCache.js";
124
+ import { isTypeBoxSchema, isZodSchema } from "./validate.js";
125
+ export function toJsonSchema(schema) {
126
+ if (isZodSchema(schema)) {
127
+ return getSchemaCache(schema, "jsonschema", function () {
128
+ // zod v4
129
+ var js = z.toJSONSchema(schema, {
130
+ unrepresentable: "any",
131
+ override: function (param) {
132
+ var zodSchema = param.zodSchema, js = param.jsonSchema;
133
+ var def = zodSchema._zod.def;
134
+ var meta = z.globalRegistry.get(zodSchema);
135
+ if (meta) {
136
+ Object.assign(js, meta);
137
+ }
138
+ switch (def.type) {
139
+ case "union":
140
+ if (zodSchema._zod.traits.has("ZodDiscriminatedUnion")) {
141
+ resolveDiscriminator(js);
142
+ }
143
+ break;
144
+ case "nonoptional":
145
+ // js._ref maybe true
146
+ js.nullable = false;
147
+ break;
148
+ case "nullable":
149
+ case "optional":
150
+ // prefer nullable
151
+ match(js).with({
152
+ anyOf: [
153
+ P.select(),
154
+ {
155
+ type: "null"
156
+ }
157
+ ]
158
+ }, function (select) {
159
+ delete js["anyOf"];
160
+ Object.assign(js, select);
161
+ js.nullable = true;
162
+ }).otherwise(function (js) {
163
+ js.nullable = true;
164
+ });
165
+ break;
166
+ }
167
+ }
168
+ });
169
+ // remove redundant nullable, ensure required is sorted
170
+ visit(js, function (v) {
171
+ if (v.nullable === false) {
172
+ delete v.nullable;
173
+ }
174
+ if (v.required) {
175
+ v.required.sort();
176
+ }
177
+ });
178
+ // maybe freeze
179
+ return js;
180
+ });
181
+ }
182
+ if (isTypeBoxSchema(schema)) {
183
+ return schema;
184
+ }
185
+ return schema;
186
+ }
187
+ function visit(js, f) {
188
+ var _visit = function (js, f, parent, path, k) {
189
+ if (!js) {
190
+ return;
191
+ }
192
+ f(js);
193
+ if (js.properties) {
194
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
195
+ try {
196
+ for (var _iterator = Object.entries(js.properties)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
197
+ var _step_value = _sliced_to_array(_step.value, 2), k1 = _step_value[0], v = _step_value[1];
198
+ if (v) {
199
+ _visit(v, f, js, path.concat(k1), "properties");
200
+ }
201
+ }
202
+ }
203
+ catch (err) {
204
+ _didIteratorError = true;
205
+ _iteratorError = err;
206
+ }
207
+ finally {
208
+ try {
209
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
210
+ _iterator.return();
211
+ }
212
+ }
213
+ finally {
214
+ if (_didIteratorError) {
215
+ throw _iteratorError;
216
+ }
217
+ }
218
+ }
219
+ }
220
+ else if (js.items) {
221
+ if (Array.isArray(js.items)) {
222
+ var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
223
+ try {
224
+ for (var _iterator1 = js.items[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true) {
225
+ var v1 = _step1.value;
226
+ _visit(v1, f, js, path, "items");
227
+ }
228
+ }
229
+ catch (err) {
230
+ _didIteratorError1 = true;
231
+ _iteratorError1 = err;
232
+ }
233
+ finally {
234
+ try {
235
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
236
+ _iterator1.return();
237
+ }
238
+ }
239
+ finally {
240
+ if (_didIteratorError1) {
241
+ throw _iteratorError1;
242
+ }
243
+ }
244
+ }
245
+ }
246
+ else {
247
+ _visit(js.items, f, js, path, "items");
248
+ }
249
+ }
250
+ else if (js.anyOf) {
251
+ var _iteratorNormalCompletion2 = true, _didIteratorError2 = false, _iteratorError2 = undefined;
252
+ try {
253
+ for (var _iterator2 = js.anyOf[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
254
+ var v2 = _step2.value;
255
+ _visit(v2, f, js, path, "anyOf");
256
+ }
257
+ }
258
+ catch (err) {
259
+ _didIteratorError2 = true;
260
+ _iteratorError2 = err;
261
+ }
262
+ finally {
263
+ try {
264
+ if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
265
+ _iterator2.return();
266
+ }
267
+ }
268
+ finally {
269
+ if (_didIteratorError2) {
270
+ throw _iteratorError2;
271
+ }
272
+ }
273
+ }
274
+ }
275
+ else if (js.oneOf) {
276
+ var _iteratorNormalCompletion3 = true, _didIteratorError3 = false, _iteratorError3 = undefined;
277
+ try {
278
+ for (var _iterator3 = js.oneOf[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
279
+ var v3 = _step3.value;
280
+ _visit(v3, f, js, path, "oneOf");
281
+ }
282
+ }
283
+ catch (err) {
284
+ _didIteratorError3 = true;
285
+ _iteratorError3 = err;
286
+ }
287
+ finally {
288
+ try {
289
+ if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
290
+ _iterator3.return();
291
+ }
292
+ }
293
+ finally {
294
+ if (_didIteratorError3) {
295
+ throw _iteratorError3;
296
+ }
297
+ }
298
+ }
299
+ }
300
+ else if (js.allOf) {
301
+ var _iteratorNormalCompletion4 = true, _didIteratorError4 = false, _iteratorError4 = undefined;
302
+ try {
303
+ for (var _iterator4 = js.allOf[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
304
+ var v4 = _step4.value;
305
+ _visit(v4, f, js, path, "allOf");
306
+ }
307
+ }
308
+ catch (err) {
309
+ _didIteratorError4 = true;
310
+ _iteratorError4 = err;
311
+ }
312
+ finally {
313
+ try {
314
+ if (!_iteratorNormalCompletion4 && _iterator4.return != null) {
315
+ _iterator4.return();
316
+ }
317
+ }
318
+ finally {
319
+ if (_didIteratorError4) {
320
+ throw _iteratorError4;
321
+ }
322
+ }
323
+ }
324
+ }
325
+ };
326
+ _visit(js, f, undefined, []);
327
+ }
328
+ function resolveJsonSchemaDef(js, ctx) {
329
+ return match(js).with({
330
+ anyOf: [
331
+ P.select(),
332
+ {
333
+ type: "null"
334
+ }
335
+ ]
336
+ }, function (select) {
337
+ return _object_spread_props(_object_spread({}, omit(js, [
338
+ "anyOf"
339
+ ]), select), {
340
+ nullable: true
341
+ });
342
+ }).with({
343
+ properties: P.nonNullable
344
+ }, function (schema) {
345
+ for (var key in schema.properties) {
346
+ var prop = schema.properties[key];
347
+ if (prop) {
348
+ schema.properties[key] = resolveJsonSchemaDef(prop, {
349
+ parent: schema,
350
+ key: key
351
+ });
352
+ }
353
+ }
354
+ return schema;
355
+ }).otherwise(function () {
356
+ return js;
357
+ });
358
+ }
359
+ function resolveDiscriminator(jsd) {
360
+ if (!(jsd.anyOf && jsd.anyOf.length > 1)) {
361
+ return;
362
+ }
363
+ if (jsd.discriminator) {
364
+ return;
365
+ }
366
+ var names = [];
367
+ {
368
+ // candidate for discriminator
369
+ var v = jsd.anyOf[0];
370
+ if (v && v.type === "object" && v.properties) {
371
+ if (Array.isArray(v.required)) {
372
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
373
+ try {
374
+ for (var _iterator = v.required[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
375
+ var k = _step.value;
376
+ if (v.properties[k].const !== undefined) {
377
+ names.push(k);
378
+ }
379
+ }
380
+ }
381
+ catch (err) {
382
+ _didIteratorError = true;
383
+ _iteratorError = err;
384
+ }
385
+ finally {
386
+ try {
387
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
388
+ _iterator.return();
389
+ }
390
+ }
391
+ finally {
392
+ if (_didIteratorError) {
393
+ throw _iteratorError;
394
+ }
395
+ }
396
+ }
397
+ }
398
+ }
399
+ }
400
+ if (names.length >= 1) {
401
+ var _loop = function (i) {
402
+ if (i === 0) {
403
+ // skip first
404
+ return "continue";
405
+ }
406
+ if (!names.length) {
407
+ return "break";
408
+ }
409
+ var v = jsd.anyOf[i];
410
+ if (v && v.type === "object" && v.properties) {
411
+ var props = v.properties;
412
+ remove(names, function (k) {
413
+ // dont care the mapping
414
+ var p = props[k];
415
+ switch (_type_of(p.const)) {
416
+ case "string":
417
+ case "number":
418
+ case "boolean":
419
+ return false;
420
+ default:
421
+ return true;
422
+ }
423
+ });
424
+ }
425
+ else {
426
+ names = [];
427
+ return "break";
428
+ }
429
+ };
430
+ for (var i = 0; i < jsd.anyOf.length; i++) {
431
+ var _ret = _loop(i);
432
+ if (_ret === "break")
433
+ break;
434
+ }
435
+ }
436
+ if (names.length === 1) {
437
+ jsd.discriminator = {
438
+ propertyName: names[0]
439
+ };
440
+ }
441
+ }
@@ -0,0 +1,27 @@
1
+ import { inspect } from "node:util";
2
+ import { describe, expect, it } from "vitest";
3
+ import { z } from "zod/v4";
4
+ import { toJsonSchema } from "./toJsonSchema.js";
5
+ describe("toJsonSchema", function () {
6
+ it("should handle discriminatedUnion", function () {
7
+ console.log(inspect(toJsonSchema(z.discriminatedUnion("type", [
8
+ z.object({
9
+ type: z.literal("string"),
10
+ value: z.string()
11
+ }),
12
+ z.object({
13
+ type: z.literal("number"),
14
+ value: z.number()
15
+ })
16
+ ])), {
17
+ depth: 10,
18
+ colors: true
19
+ }));
20
+ });
21
+ it("should cache", function () {
22
+ var zs = z.object({
23
+ name: z.string()
24
+ });
25
+ expect(toJsonSchema(zs)).toBe(toJsonSchema(zs));
26
+ });
27
+ });
@@ -0,0 +1,124 @@
1
+ function _type_of(obj) {
2
+ "@swc/helpers - typeof";
3
+ return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
4
+ }
5
+ import { Kind as TypeBoxKind } from '@sinclair/typebox';
6
+ import '@sinclair/typebox';
7
+ import { TypeCompiler } from '@sinclair/typebox/compiler';
8
+ import { ifPresent } from '@wener/utils';
9
+ import { Ajv } from 'ajv';
10
+ import addFormats from 'ajv-formats';
11
+ /*
12
+ https://github.com/react-hook-form/resolvers
13
+ https://github.com/decs/typeschema/tree/main/packages
14
+ */ export function validate(schema, data) {
15
+ if (isZodSchema(schema)) {
16
+ var result = schema.safeParse(data);
17
+ if (result.success) {
18
+ return {
19
+ success: true,
20
+ data: result.data
21
+ };
22
+ }
23
+ return {
24
+ issues: result.error.issues.map(function(param) {
25
+ var message = param.message, path = param.path, code = param.code;
26
+ return {
27
+ message: message,
28
+ path: path,
29
+ code: code
30
+ };
31
+ }),
32
+ // message: z.prettifyError(result.error),
33
+ success: false
34
+ };
35
+ }
36
+ if (isTypeBoxSchema(schema)) {
37
+ var checker = TypeCompiler.Compile(schema);
38
+ if (checker.Check(data)) {
39
+ return {
40
+ success: true,
41
+ data: data
42
+ };
43
+ }
44
+ return {
45
+ issues: Array.from(checker.Errors(data)).map(function(param) {
46
+ var message = param.message, path = param.path;
47
+ return {
48
+ message: message,
49
+ path: [
50
+ path
51
+ ]
52
+ };
53
+ }),
54
+ // message: '',
55
+ success: false
56
+ };
57
+ }
58
+ if (isJsonSchema(schema)) {
59
+ var _validator_errors;
60
+ var ajv = new Ajv({
61
+ allErrors: true,
62
+ validateSchema: true
63
+ });
64
+ addFormats(ajv);
65
+ var validator = ajv.compile(schema);
66
+ if (validator(data)) {
67
+ return {
68
+ data: data,
69
+ success: true
70
+ };
71
+ }
72
+ var issues = ((_validator_errors = validator.errors) === null || _validator_errors === void 0 ? void 0 : _validator_errors.map(function(error) {
73
+ var message = error.message || 'Unknown error';
74
+ var path = error.instancePath.split('/').filter(Boolean).map(function(p) {
75
+ return p;
76
+ });
77
+ return {
78
+ message: message,
79
+ path: path
80
+ };
81
+ })) || [];
82
+ return {
83
+ success: false,
84
+ // message: 'Invalid data',
85
+ issues: issues
86
+ };
87
+ }
88
+ return {
89
+ success: false,
90
+ // message: 'Invalid schema',
91
+ issues: [
92
+ {
93
+ message: 'Unknown schema type'
94
+ }
95
+ ]
96
+ };
97
+ }
98
+ export function parseData(schema, data) {
99
+ var result = validate(schema, data);
100
+ if (result.success) {
101
+ return result.data;
102
+ }
103
+ throw Object.assign(new Error(result.issues.map(function(v) {
104
+ var _v_path;
105
+ return "".concat(ifPresent((_v_path = v.path) === null || _v_path === void 0 ? void 0 : _v_path.join(), function(v) {
106
+ return "[".concat(v, "]");
107
+ }), ": ").concat(v.message);
108
+ }).join('; ')), {
109
+ issues: result.issues
110
+ });
111
+ }
112
+ function formatIssues(schema, issues) {
113
+ return '';
114
+ }
115
+ export function isZodSchema(schema) {
116
+ return (typeof schema === "undefined" ? "undefined" : _type_of(schema)) === 'object' && 'parse' in schema;
117
+ }
118
+ export function isTypeBoxSchema(schema) {
119
+ return (typeof schema === "undefined" ? "undefined" : _type_of(schema)) === 'object' && TypeBoxKind in schema;
120
+ }
121
+ export function isJsonSchema(schema) {
122
+ var sc = schema;
123
+ return (typeof schema === "undefined" ? "undefined" : _type_of(schema)) === 'object' && Boolean(sc.type || sc.properties || sc.anyOf || sc.oneOf || sc.$ref || sc.items);
124
+ }
@@ -7,4 +7,3 @@ import { parseAdvanceSearch } from "./parseAdvanceSearch.js";
7
7
  AdvanceSearch.optimize = optimizeAdvanceSearch;
8
8
  })(AdvanceSearch || (AdvanceSearch = {}));
9
9
  export var AdvanceSearch;
10
- //# sourceMappingURL=AdvanceSearch.js.map