@palmares/schemas 0.1.24 → 0.1.26

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 (305) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/src/adapter/fields/array.cjs +96 -0
  3. package/dist/src/adapter/fields/array.d.ts.map +1 -0
  4. package/dist/src/adapter/fields/array.js +70 -0
  5. package/dist/src/adapter/fields/boolean.cjs +102 -0
  6. package/dist/src/adapter/fields/boolean.d.ts.map +1 -0
  7. package/dist/src/adapter/fields/boolean.js +76 -0
  8. package/dist/src/adapter/fields/datetime.cjs +102 -0
  9. package/dist/src/adapter/fields/datetime.d.ts.map +1 -0
  10. package/dist/src/adapter/fields/datetime.js +76 -0
  11. package/dist/src/adapter/fields/index.cjs +87 -0
  12. package/dist/src/adapter/fields/index.d.ts.map +1 -0
  13. package/dist/src/adapter/fields/index.js +61 -0
  14. package/dist/src/adapter/fields/number.cjs +102 -0
  15. package/dist/src/adapter/fields/number.d.ts.map +1 -0
  16. package/dist/src/adapter/fields/number.js +76 -0
  17. package/dist/src/adapter/fields/object.cjs +108 -0
  18. package/dist/src/adapter/fields/object.d.ts.map +1 -0
  19. package/dist/src/adapter/fields/object.js +82 -0
  20. package/dist/src/adapter/fields/string.cjs +102 -0
  21. package/dist/src/adapter/fields/string.d.ts.map +1 -0
  22. package/dist/src/adapter/fields/string.js +76 -0
  23. package/dist/src/adapter/fields/union.cjs +102 -0
  24. package/dist/src/adapter/fields/union.d.ts.map +1 -0
  25. package/dist/src/adapter/fields/union.js +76 -0
  26. package/dist/src/adapter/index.cjs +65 -0
  27. package/dist/src/adapter/index.d.ts.map +1 -0
  28. package/dist/src/adapter/index.js +40 -0
  29. package/dist/src/adapter/types.cjs +18 -0
  30. package/dist/src/adapter/types.d.ts.map +1 -0
  31. package/dist/src/adapter/types.js +0 -0
  32. package/dist/src/compile.cjs +59 -0
  33. package/dist/src/compile.d.ts.map +1 -0
  34. package/dist/src/compile.js +36 -0
  35. package/dist/src/conf.cjs +53 -0
  36. package/dist/src/conf.d.ts.map +1 -0
  37. package/dist/src/conf.js +27 -0
  38. package/dist/src/constants.cjs +40 -0
  39. package/dist/src/constants.d.ts.map +1 -0
  40. package/dist/src/constants.js +14 -0
  41. package/dist/src/domain.cjs +63 -0
  42. package/dist/src/domain.d.ts.map +1 -0
  43. package/dist/src/domain.js +40 -0
  44. package/dist/src/exceptions.cjs +58 -0
  45. package/dist/src/exceptions.d.ts.map +1 -0
  46. package/dist/src/exceptions.js +33 -0
  47. package/dist/src/index.cjs +6520 -0
  48. package/dist/src/index.d.ts.map +1 -0
  49. package/dist/src/index.js +6456 -0
  50. package/dist/src/middleware.cjs +58 -0
  51. package/dist/src/middleware.d.ts.map +1 -0
  52. package/dist/src/middleware.js +35 -0
  53. package/dist/src/model.cjs +6003 -0
  54. package/dist/src/model.d.ts.map +1 -0
  55. package/dist/src/model.js +5978 -0
  56. package/dist/src/parsers/convert-from-number.cjs +34 -0
  57. package/dist/src/parsers/convert-from-number.d.ts.map +1 -0
  58. package/dist/src/parsers/convert-from-number.js +11 -0
  59. package/dist/src/parsers/convert-from-string.cjs +40 -0
  60. package/dist/src/parsers/convert-from-string.d.ts.map +1 -0
  61. package/dist/src/parsers/convert-from-string.js +17 -0
  62. package/dist/src/parsers/index.cjs +50 -0
  63. package/dist/src/parsers/index.d.ts.map +1 -0
  64. package/dist/src/parsers/index.js +24 -0
  65. package/dist/src/schema/array.cjs +1863 -0
  66. package/dist/src/schema/array.d.ts.map +1 -0
  67. package/dist/src/schema/array.js +1837 -0
  68. package/dist/src/schema/boolean.cjs +1800 -0
  69. package/dist/src/schema/boolean.d.ts.map +1 -0
  70. package/dist/src/schema/boolean.js +1774 -0
  71. package/dist/src/schema/datetime.cjs +1789 -0
  72. package/dist/src/schema/datetime.d.ts.map +1 -0
  73. package/dist/src/schema/datetime.js +1763 -0
  74. package/dist/src/schema/index.cjs +2026 -0
  75. package/dist/src/schema/index.d.ts.map +1 -0
  76. package/dist/src/schema/index.js +2001 -0
  77. package/dist/src/schema/number.cjs +2026 -0
  78. package/dist/src/schema/number.d.ts.map +1 -0
  79. package/dist/src/schema/number.js +2000 -0
  80. package/dist/src/schema/object.cjs +1795 -0
  81. package/dist/src/schema/object.d.ts.map +1 -0
  82. package/dist/src/schema/object.js +1769 -0
  83. package/dist/src/schema/schema.cjs +844 -0
  84. package/dist/src/schema/schema.d.ts.map +1 -0
  85. package/dist/src/schema/schema.js +818 -0
  86. package/dist/src/schema/string.cjs +2126 -0
  87. package/dist/src/schema/string.d.ts.map +1 -0
  88. package/dist/src/schema/string.js +2100 -0
  89. package/dist/src/schema/types.cjs +18 -0
  90. package/dist/src/schema/types.d.ts.map +1 -0
  91. package/dist/src/schema/types.js +0 -0
  92. package/dist/src/schema/union.cjs +1737 -0
  93. package/dist/src/schema/union.d.ts.map +1 -0
  94. package/dist/src/schema/union.js +1711 -0
  95. package/dist/src/types.cjs +18 -0
  96. package/dist/src/types.d.ts.map +1 -0
  97. package/dist/src/types.js +0 -0
  98. package/dist/src/utils.cjs +477 -0
  99. package/dist/src/utils.d.ts.map +1 -0
  100. package/dist/src/utils.js +445 -0
  101. package/dist/src/validators/array.cjs +176 -0
  102. package/dist/src/validators/array.d.ts.map +1 -0
  103. package/dist/src/validators/array.js +150 -0
  104. package/dist/src/validators/boolean.cjs +71 -0
  105. package/dist/src/validators/boolean.d.ts.map +1 -0
  106. package/dist/src/validators/boolean.js +47 -0
  107. package/dist/src/validators/datetime.cjs +129 -0
  108. package/dist/src/validators/datetime.d.ts.map +1 -0
  109. package/dist/src/validators/datetime.js +103 -0
  110. package/dist/src/validators/number.cjs +194 -0
  111. package/dist/src/validators/number.d.ts.map +1 -0
  112. package/dist/src/validators/number.js +166 -0
  113. package/dist/src/validators/object.cjs +69 -0
  114. package/dist/src/validators/object.d.ts.map +1 -0
  115. package/dist/src/validators/object.js +46 -0
  116. package/dist/src/validators/schema.cjs +158 -0
  117. package/dist/src/validators/schema.d.ts.map +1 -0
  118. package/dist/src/validators/schema.js +132 -0
  119. package/dist/src/validators/string.cjs +255 -0
  120. package/dist/src/validators/string.d.ts.map +1 -0
  121. package/dist/src/validators/string.js +224 -0
  122. package/dist/src/validators/types.cjs +18 -0
  123. package/dist/src/validators/types.d.ts.map +1 -0
  124. package/dist/src/validators/types.js +0 -0
  125. package/dist/src/validators/union.cjs +69 -0
  126. package/dist/src/validators/union.d.ts.map +1 -0
  127. package/dist/src/validators/union.js +46 -0
  128. package/dist/src/validators/utils.cjs +165 -0
  129. package/dist/src/validators/utils.d.ts.map +1 -0
  130. package/dist/src/validators/utils.js +142 -0
  131. package/dist/tsconfig.types.tsbuildinfo +1 -0
  132. package/package.json +15 -23
  133. package/dist/cjs/src/adapter/fields/array.js +0 -152
  134. package/dist/cjs/src/adapter/fields/boolean.js +0 -162
  135. package/dist/cjs/src/adapter/fields/datetime.js +0 -162
  136. package/dist/cjs/src/adapter/fields/index.js +0 -311
  137. package/dist/cjs/src/adapter/fields/number.js +0 -162
  138. package/dist/cjs/src/adapter/fields/object.js +0 -171
  139. package/dist/cjs/src/adapter/fields/string.js +0 -162
  140. package/dist/cjs/src/adapter/fields/union.js +0 -162
  141. package/dist/cjs/src/adapter/index.js +0 -201
  142. package/dist/cjs/src/adapter/types.js +0 -4
  143. package/dist/cjs/src/compile.js +0 -262
  144. package/dist/cjs/src/conf.js +0 -26
  145. package/dist/cjs/src/constants.js +0 -36
  146. package/dist/cjs/src/domain.js +0 -156
  147. package/dist/cjs/src/exceptions.js +0 -168
  148. package/dist/cjs/src/index.js +0 -317
  149. package/dist/cjs/src/middleware.js +0 -213
  150. package/dist/cjs/src/model.js +0 -628
  151. package/dist/cjs/src/parsers/convert-from-number.js +0 -15
  152. package/dist/cjs/src/parsers/convert-from-string.js +0 -19
  153. package/dist/cjs/src/parsers/index.js +0 -20
  154. package/dist/cjs/src/schema/array.js +0 -1014
  155. package/dist/cjs/src/schema/boolean.js +0 -883
  156. package/dist/cjs/src/schema/datetime.js +0 -840
  157. package/dist/cjs/src/schema/index.js +0 -12
  158. package/dist/cjs/src/schema/number.js +0 -976
  159. package/dist/cjs/src/schema/object.js +0 -1052
  160. package/dist/cjs/src/schema/schema.js +0 -1840
  161. package/dist/cjs/src/schema/string.js +0 -1038
  162. package/dist/cjs/src/schema/types.js +0 -4
  163. package/dist/cjs/src/schema/union.js +0 -1070
  164. package/dist/cjs/src/types.js +0 -4
  165. package/dist/cjs/src/utils.js +0 -647
  166. package/dist/cjs/src/validators/array.js +0 -462
  167. package/dist/cjs/src/validators/boolean.js +0 -201
  168. package/dist/cjs/src/validators/datetime.js +0 -291
  169. package/dist/cjs/src/validators/number.js +0 -375
  170. package/dist/cjs/src/validators/object.js +0 -291
  171. package/dist/cjs/src/validators/schema.js +0 -322
  172. package/dist/cjs/src/validators/string.js +0 -467
  173. package/dist/cjs/src/validators/types.js +0 -4
  174. package/dist/cjs/src/validators/union.js +0 -233
  175. package/dist/cjs/src/validators/utils.js +0 -499
  176. package/dist/cjs/tsconfig.types.tsbuildinfo +0 -1
  177. package/dist/cjs/types/adapter/fields/array.d.ts.map +0 -1
  178. package/dist/cjs/types/adapter/fields/boolean.d.ts.map +0 -1
  179. package/dist/cjs/types/adapter/fields/datetime.d.ts.map +0 -1
  180. package/dist/cjs/types/adapter/fields/index.d.ts.map +0 -1
  181. package/dist/cjs/types/adapter/fields/number.d.ts.map +0 -1
  182. package/dist/cjs/types/adapter/fields/object.d.ts.map +0 -1
  183. package/dist/cjs/types/adapter/fields/string.d.ts.map +0 -1
  184. package/dist/cjs/types/adapter/fields/union.d.ts.map +0 -1
  185. package/dist/cjs/types/adapter/index.d.ts.map +0 -1
  186. package/dist/cjs/types/adapter/types.d.ts.map +0 -1
  187. package/dist/cjs/types/compile.d.ts.map +0 -1
  188. package/dist/cjs/types/conf.d.ts.map +0 -1
  189. package/dist/cjs/types/constants.d.ts.map +0 -1
  190. package/dist/cjs/types/domain.d.ts.map +0 -1
  191. package/dist/cjs/types/exceptions.d.ts.map +0 -1
  192. package/dist/cjs/types/index.d.ts.map +0 -1
  193. package/dist/cjs/types/middleware.d.ts.map +0 -1
  194. package/dist/cjs/types/model.d.ts.map +0 -1
  195. package/dist/cjs/types/parsers/convert-from-number.d.ts.map +0 -1
  196. package/dist/cjs/types/parsers/convert-from-string.d.ts.map +0 -1
  197. package/dist/cjs/types/parsers/index.d.ts.map +0 -1
  198. package/dist/cjs/types/schema/array.d.ts.map +0 -1
  199. package/dist/cjs/types/schema/boolean.d.ts.map +0 -1
  200. package/dist/cjs/types/schema/datetime.d.ts.map +0 -1
  201. package/dist/cjs/types/schema/index.d.ts.map +0 -1
  202. package/dist/cjs/types/schema/number.d.ts.map +0 -1
  203. package/dist/cjs/types/schema/object.d.ts.map +0 -1
  204. package/dist/cjs/types/schema/schema.d.ts.map +0 -1
  205. package/dist/cjs/types/schema/string.d.ts.map +0 -1
  206. package/dist/cjs/types/schema/types.d.ts.map +0 -1
  207. package/dist/cjs/types/schema/union.d.ts.map +0 -1
  208. package/dist/cjs/types/types.d.ts.map +0 -1
  209. package/dist/cjs/types/utils.d.ts.map +0 -1
  210. package/dist/cjs/types/validators/array.d.ts.map +0 -1
  211. package/dist/cjs/types/validators/boolean.d.ts.map +0 -1
  212. package/dist/cjs/types/validators/datetime.d.ts.map +0 -1
  213. package/dist/cjs/types/validators/number.d.ts.map +0 -1
  214. package/dist/cjs/types/validators/object.d.ts.map +0 -1
  215. package/dist/cjs/types/validators/schema.d.ts.map +0 -1
  216. package/dist/cjs/types/validators/string.d.ts.map +0 -1
  217. package/dist/cjs/types/validators/types.d.ts.map +0 -1
  218. package/dist/cjs/types/validators/union.d.ts.map +0 -1
  219. package/dist/cjs/types/validators/utils.d.ts.map +0 -1
  220. package/dist/esm/src/adapter/fields/array.js +0 -13
  221. package/dist/esm/src/adapter/fields/boolean.js +0 -20
  222. package/dist/esm/src/adapter/fields/datetime.js +0 -20
  223. package/dist/esm/src/adapter/fields/index.js +0 -37
  224. package/dist/esm/src/adapter/fields/number.js +0 -20
  225. package/dist/esm/src/adapter/fields/object.js +0 -26
  226. package/dist/esm/src/adapter/fields/string.js +0 -20
  227. package/dist/esm/src/adapter/fields/union.js +0 -20
  228. package/dist/esm/src/adapter/index.js +0 -21
  229. package/dist/esm/src/adapter/types.js +0 -1
  230. package/dist/esm/src/compile.js +0 -10
  231. package/dist/esm/src/conf.js +0 -18
  232. package/dist/esm/src/constants.js +0 -4
  233. package/dist/esm/src/domain.js +0 -11
  234. package/dist/esm/src/exceptions.js +0 -15
  235. package/dist/esm/src/index.js +0 -126
  236. package/dist/esm/src/middleware.js +0 -29
  237. package/dist/esm/src/model.js +0 -264
  238. package/dist/esm/src/parsers/convert-from-number.js +0 -8
  239. package/dist/esm/src/parsers/convert-from-string.js +0 -14
  240. package/dist/esm/src/parsers/index.js +0 -2
  241. package/dist/esm/src/schema/array.js +0 -487
  242. package/dist/esm/src/schema/boolean.js +0 -520
  243. package/dist/esm/src/schema/datetime.js +0 -477
  244. package/dist/esm/src/schema/index.js +0 -3
  245. package/dist/esm/src/schema/number.js +0 -610
  246. package/dist/esm/src/schema/object.js +0 -521
  247. package/dist/esm/src/schema/schema.js +0 -767
  248. package/dist/esm/src/schema/string.js +0 -668
  249. package/dist/esm/src/schema/types.js +0 -1
  250. package/dist/esm/src/schema/union.js +0 -468
  251. package/dist/esm/src/types.js +0 -14
  252. package/dist/esm/src/utils.js +0 -207
  253. package/dist/esm/src/validators/array.js +0 -140
  254. package/dist/esm/src/validators/boolean.js +0 -37
  255. package/dist/esm/src/validators/datetime.js +0 -89
  256. package/dist/esm/src/validators/number.js +0 -148
  257. package/dist/esm/src/validators/object.js +0 -39
  258. package/dist/esm/src/validators/schema.js +0 -118
  259. package/dist/esm/src/validators/string.js +0 -202
  260. package/dist/esm/src/validators/types.js +0 -1
  261. package/dist/esm/src/validators/union.js +0 -39
  262. package/dist/esm/src/validators/utils.js +0 -147
  263. /package/dist/{cjs/types → src}/adapter/fields/array.d.ts +0 -0
  264. /package/dist/{cjs/types → src}/adapter/fields/boolean.d.ts +0 -0
  265. /package/dist/{cjs/types → src}/adapter/fields/datetime.d.ts +0 -0
  266. /package/dist/{cjs/types → src}/adapter/fields/index.d.ts +0 -0
  267. /package/dist/{cjs/types → src}/adapter/fields/number.d.ts +0 -0
  268. /package/dist/{cjs/types → src}/adapter/fields/object.d.ts +0 -0
  269. /package/dist/{cjs/types → src}/adapter/fields/string.d.ts +0 -0
  270. /package/dist/{cjs/types → src}/adapter/fields/union.d.ts +0 -0
  271. /package/dist/{cjs/types → src}/adapter/index.d.ts +0 -0
  272. /package/dist/{cjs/types → src}/adapter/types.d.ts +0 -0
  273. /package/dist/{cjs/types → src}/compile.d.ts +0 -0
  274. /package/dist/{cjs/types → src}/conf.d.ts +0 -0
  275. /package/dist/{cjs/types → src}/constants.d.ts +0 -0
  276. /package/dist/{cjs/types → src}/domain.d.ts +0 -0
  277. /package/dist/{cjs/types → src}/exceptions.d.ts +0 -0
  278. /package/dist/{cjs/types → src}/index.d.ts +0 -0
  279. /package/dist/{cjs/types → src}/middleware.d.ts +0 -0
  280. /package/dist/{cjs/types → src}/model.d.ts +0 -0
  281. /package/dist/{cjs/types → src}/parsers/convert-from-number.d.ts +0 -0
  282. /package/dist/{cjs/types → src}/parsers/convert-from-string.d.ts +0 -0
  283. /package/dist/{cjs/types → src}/parsers/index.d.ts +0 -0
  284. /package/dist/{cjs/types → src}/schema/array.d.ts +0 -0
  285. /package/dist/{cjs/types → src}/schema/boolean.d.ts +0 -0
  286. /package/dist/{cjs/types → src}/schema/datetime.d.ts +0 -0
  287. /package/dist/{cjs/types → src}/schema/index.d.ts +0 -0
  288. /package/dist/{cjs/types → src}/schema/number.d.ts +0 -0
  289. /package/dist/{cjs/types → src}/schema/object.d.ts +0 -0
  290. /package/dist/{cjs/types → src}/schema/schema.d.ts +0 -0
  291. /package/dist/{cjs/types → src}/schema/string.d.ts +0 -0
  292. /package/dist/{cjs/types → src}/schema/types.d.ts +0 -0
  293. /package/dist/{cjs/types → src}/schema/union.d.ts +0 -0
  294. /package/dist/{cjs/types → src}/types.d.ts +0 -0
  295. /package/dist/{cjs/types → src}/utils.d.ts +0 -0
  296. /package/dist/{cjs/types → src}/validators/array.d.ts +0 -0
  297. /package/dist/{cjs/types → src}/validators/boolean.d.ts +0 -0
  298. /package/dist/{cjs/types → src}/validators/datetime.d.ts +0 -0
  299. /package/dist/{cjs/types → src}/validators/number.d.ts +0 -0
  300. /package/dist/{cjs/types → src}/validators/object.d.ts +0 -0
  301. /package/dist/{cjs/types → src}/validators/schema.d.ts +0 -0
  302. /package/dist/{cjs/types → src}/validators/string.d.ts +0 -0
  303. /package/dist/{cjs/types → src}/validators/types.d.ts +0 -0
  304. /package/dist/{cjs/types → src}/validators/union.d.ts +0 -0
  305. /package/dist/{cjs/types → src}/validators/utils.d.ts +0 -0
@@ -1,1070 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- UnionSchema: function() {
13
- return UnionSchema;
14
- },
15
- union: function() {
16
- return union;
17
- }
18
- });
19
- var _schema = require("./schema");
20
- var _utils = require("../utils");
21
- var _union = require("../validators/union");
22
- var _utils1 = require("../validators/utils");
23
- function _array_like_to_array(arr, len) {
24
- if (len == null || len > arr.length) len = arr.length;
25
- for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
26
- return arr2;
27
- }
28
- function _array_with_holes(arr) {
29
- if (Array.isArray(arr)) return arr;
30
- }
31
- function _assert_this_initialized(self) {
32
- if (self === void 0) {
33
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
34
- }
35
- return self;
36
- }
37
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
38
- try {
39
- var info = gen[key](arg);
40
- var value = info.value;
41
- } catch (error) {
42
- reject(error);
43
- return;
44
- }
45
- if (info.done) {
46
- resolve(value);
47
- } else {
48
- Promise.resolve(value).then(_next, _throw);
49
- }
50
- }
51
- function _async_to_generator(fn) {
52
- return function() {
53
- var self = this, args = arguments;
54
- return new Promise(function(resolve, reject) {
55
- var gen = fn.apply(self, args);
56
- function _next(value) {
57
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
58
- }
59
- function _throw(err) {
60
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
61
- }
62
- _next(undefined);
63
- });
64
- };
65
- }
66
- function _class_call_check(instance, Constructor) {
67
- if (!(instance instanceof Constructor)) {
68
- throw new TypeError("Cannot call a class as a function");
69
- }
70
- }
71
- function _defineProperties(target, props) {
72
- for(var i = 0; i < props.length; i++){
73
- var descriptor = props[i];
74
- descriptor.enumerable = descriptor.enumerable || false;
75
- descriptor.configurable = true;
76
- if ("value" in descriptor) descriptor.writable = true;
77
- Object.defineProperty(target, descriptor.key, descriptor);
78
- }
79
- }
80
- function _create_class(Constructor, protoProps, staticProps) {
81
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
82
- if (staticProps) _defineProperties(Constructor, staticProps);
83
- return Constructor;
84
- }
85
- function _define_property(obj, key, value) {
86
- if (key in obj) {
87
- Object.defineProperty(obj, key, {
88
- value: value,
89
- enumerable: true,
90
- configurable: true,
91
- writable: true
92
- });
93
- } else {
94
- obj[key] = value;
95
- }
96
- return obj;
97
- }
98
- function _get(target, property, receiver) {
99
- if (typeof Reflect !== "undefined" && Reflect.get) {
100
- _get = Reflect.get;
101
- } else {
102
- _get = function get(target, property, receiver) {
103
- var base = _super_prop_base(target, property);
104
- if (!base) return;
105
- var desc = Object.getOwnPropertyDescriptor(base, property);
106
- if (desc.get) {
107
- return desc.get.call(receiver || target);
108
- }
109
- return desc.value;
110
- };
111
- }
112
- return _get(target, property, receiver || target);
113
- }
114
- function _get_prototype_of(o) {
115
- _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
116
- return o.__proto__ || Object.getPrototypeOf(o);
117
- };
118
- return _get_prototype_of(o);
119
- }
120
- function _inherits(subClass, superClass) {
121
- if (typeof superClass !== "function" && superClass !== null) {
122
- throw new TypeError("Super expression must either be null or a function");
123
- }
124
- subClass.prototype = Object.create(superClass && superClass.prototype, {
125
- constructor: {
126
- value: subClass,
127
- writable: true,
128
- configurable: true
129
- }
130
- });
131
- if (superClass) _set_prototype_of(subClass, superClass);
132
- }
133
- function _iterable_to_array_limit(arr, i) {
134
- var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
135
- if (_i == null) return;
136
- var _arr = [];
137
- var _n = true;
138
- var _d = false;
139
- var _s, _e;
140
- try {
141
- for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
142
- _arr.push(_s.value);
143
- if (i && _arr.length === i) break;
144
- }
145
- } catch (err) {
146
- _d = true;
147
- _e = err;
148
- } finally{
149
- try {
150
- if (!_n && _i["return"] != null) _i["return"]();
151
- } finally{
152
- if (_d) throw _e;
153
- }
154
- }
155
- return _arr;
156
- }
157
- function _non_iterable_rest() {
158
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
159
- }
160
- function _possible_constructor_return(self, call) {
161
- if (call && (_type_of(call) === "object" || typeof call === "function")) {
162
- return call;
163
- }
164
- return _assert_this_initialized(self);
165
- }
166
- function _set_prototype_of(o, p) {
167
- _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
168
- o.__proto__ = p;
169
- return o;
170
- };
171
- return _set_prototype_of(o, p);
172
- }
173
- function _sliced_to_array(arr, i) {
174
- return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
175
- }
176
- function _super_prop_base(object, property) {
177
- while(!Object.prototype.hasOwnProperty.call(object, property)){
178
- object = _get_prototype_of(object);
179
- if (object === null) break;
180
- }
181
- return object;
182
- }
183
- function _type_of(obj) {
184
- "@swc/helpers - typeof";
185
- return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
186
- }
187
- function _unsupported_iterable_to_array(o, minLen) {
188
- if (!o) return;
189
- if (typeof o === "string") return _array_like_to_array(o, minLen);
190
- var n = Object.prototype.toString.call(o).slice(8, -1);
191
- if (n === "Object" && o.constructor) n = o.constructor.name;
192
- if (n === "Map" || n === "Set") return Array.from(n);
193
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
194
- }
195
- function _is_native_reflect_construct() {
196
- if (typeof Reflect === "undefined" || !Reflect.construct) return false;
197
- if (Reflect.construct.sham) return false;
198
- if (typeof Proxy === "function") return true;
199
- try {
200
- Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
201
- return true;
202
- } catch (e) {
203
- return false;
204
- }
205
- }
206
- function _create_super(Derived) {
207
- var hasNativeReflectConstruct = _is_native_reflect_construct();
208
- return function _createSuperInternal() {
209
- var Super = _get_prototype_of(Derived), result;
210
- if (hasNativeReflectConstruct) {
211
- var NewTarget = _get_prototype_of(this).constructor;
212
- result = Reflect.construct(Super, arguments, NewTarget);
213
- } else {
214
- result = Super.apply(this, arguments);
215
- }
216
- return _possible_constructor_return(this, result);
217
- };
218
- }
219
- function _ts_generator(thisArg, body) {
220
- var f, y, t, g, _ = {
221
- label: 0,
222
- sent: function() {
223
- if (t[0] & 1) throw t[1];
224
- return t[1];
225
- },
226
- trys: [],
227
- ops: []
228
- };
229
- return g = {
230
- next: verb(0),
231
- "throw": verb(1),
232
- "return": verb(2)
233
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
234
- return this;
235
- }), g;
236
- function verb(n) {
237
- return function(v) {
238
- return step([
239
- n,
240
- v
241
- ]);
242
- };
243
- }
244
- function step(op) {
245
- if (f) throw new TypeError("Generator is already executing.");
246
- while(_)try {
247
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
248
- if (y = 0, t) op = [
249
- op[0] & 2,
250
- t.value
251
- ];
252
- switch(op[0]){
253
- case 0:
254
- case 1:
255
- t = op;
256
- break;
257
- case 4:
258
- _.label++;
259
- return {
260
- value: op[1],
261
- done: false
262
- };
263
- case 5:
264
- _.label++;
265
- y = op[1];
266
- op = [
267
- 0
268
- ];
269
- continue;
270
- case 7:
271
- op = _.ops.pop();
272
- _.trys.pop();
273
- continue;
274
- default:
275
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
276
- _ = 0;
277
- continue;
278
- }
279
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
280
- _.label = op[1];
281
- break;
282
- }
283
- if (op[0] === 6 && _.label < t[1]) {
284
- _.label = t[1];
285
- t = op;
286
- break;
287
- }
288
- if (t && _.label < t[2]) {
289
- _.label = t[2];
290
- _.ops.push(op);
291
- break;
292
- }
293
- if (t[2]) _.ops.pop();
294
- _.trys.pop();
295
- continue;
296
- }
297
- op = body.call(thisArg, _);
298
- } catch (e) {
299
- op = [
300
- 6,
301
- e
302
- ];
303
- y = 0;
304
- } finally{
305
- f = t = 0;
306
- }
307
- if (op[0] & 5) throw op[1];
308
- return {
309
- value: op[0] ? op[1] : void 0,
310
- done: true
311
- };
312
- }
313
- }
314
- var UnionSchema = /*#__PURE__*/ function(Schema) {
315
- "use strict";
316
- _inherits(UnionSchema, Schema);
317
- var _super = _create_super(UnionSchema);
318
- function UnionSchema(schemas) {
319
- _class_call_check(this, UnionSchema);
320
- var _this;
321
- _this = _super.call(this);
322
- _define_property(_assert_this_initialized(_this), "fieldType", 'union');
323
- _define_property(_assert_this_initialized(_this), "__type", {
324
- message: 'Invalid type',
325
- check: function(value) {
326
- return Array.from(_this.__schemas).some(function(schema) {
327
- return schema['__type'].check(value);
328
- });
329
- }
330
- });
331
- _define_property(_assert_this_initialized(_this), "__schemas", new Set());
332
- _this.__schemas = new Set(schemas);
333
- return _this;
334
- }
335
- _create_class(UnionSchema, [
336
- {
337
- key: "__transformToAdapter",
338
- value: function __transformToAdapter(options) {
339
- var _this = this;
340
- return _async_to_generator(function() {
341
- return _ts_generator(this, function(_state) {
342
- return [
343
- 2,
344
- (0, _utils.defaultTransformToAdapter)(function() {
345
- var _ref = _async_to_generator(function(adapter) {
346
- var promises, shouldBeHighPriorityFallback, transformedSchemasAsString, transformedSchemas, shouldBeHandledByFallback, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step;
347
- return _ts_generator(this, function(_state) {
348
- switch(_state.label){
349
- case 0:
350
- promises = [];
351
- shouldBeHighPriorityFallback = adapter.union === undefined;
352
- transformedSchemasAsString = [];
353
- transformedSchemas = [];
354
- shouldBeHandledByFallback = shouldBeHighPriorityFallback;
355
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
356
- try {
357
- _loop = function() {
358
- var schemaToTransform = _step.value;
359
- var awaitableTransformer = function() {
360
- var _ref = _async_to_generator(function() {
361
- var _ref, transformedData, shouldAddFallbackValidationForThisKey, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, transformedSchema;
362
- return _ts_generator(this, function(_state) {
363
- switch(_state.label){
364
- case 0:
365
- return [
366
- 4,
367
- (0, _utils.transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas)(schemaToTransform, options)
368
- ];
369
- case 1:
370
- _ref = _sliced_to_array.apply(void 0, [
371
- _state.sent(),
372
- 2
373
- ]), transformedData = _ref[0], shouldAddFallbackValidationForThisKey = _ref[1];
374
- if (shouldAddFallbackValidationForThisKey) shouldBeHandledByFallback = true;
375
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
376
- try {
377
- for(_iterator = transformedData[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
378
- transformedSchema = _step.value;
379
- transformedSchemasAsString.push(transformedSchema.asString);
380
- transformedSchemas.push(transformedSchema.transformed);
381
- }
382
- } catch (err) {
383
- _didIteratorError = true;
384
- _iteratorError = err;
385
- } finally{
386
- try {
387
- if (!_iteratorNormalCompletion && _iterator.return != null) {
388
- _iterator.return();
389
- }
390
- } finally{
391
- if (_didIteratorError) {
392
- throw _iteratorError;
393
- }
394
- }
395
- }
396
- return [
397
- 2
398
- ];
399
- }
400
- });
401
- });
402
- return function awaitableTransformer() {
403
- return _ref.apply(this, arguments);
404
- };
405
- }();
406
- promises.push(awaitableTransformer());
407
- };
408
- for(_iterator = _this.__schemas.values()[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
409
- } catch (err) {
410
- _didIteratorError = true;
411
- _iteratorError = err;
412
- } finally{
413
- try {
414
- if (!_iteratorNormalCompletion && _iterator.return != null) {
415
- _iterator.return();
416
- }
417
- } finally{
418
- if (_didIteratorError) {
419
- throw _iteratorError;
420
- }
421
- }
422
- }
423
- if (shouldBeHandledByFallback) {
424
- _utils1.Validator.createAndAppendFallback(_this, (0, _union.unionValidation)(Array.from(_this.__schemas)), {
425
- at: 0,
426
- removeCurrent: true
427
- });
428
- }
429
- return [
430
- 4,
431
- Promise.all(promises)
432
- ];
433
- case 1:
434
- _state.sent();
435
- return [
436
- 2,
437
- (0, _utils.defaultTransform)('union', _this, adapter, adapter.union, function(isStringVersion) {
438
- return {
439
- nullable: _this.__nullable,
440
- type: _this.__type,
441
- optional: _this.__optional,
442
- schemas: isStringVersion ? transformedSchemasAsString : transformedSchemas,
443
- parsers: {
444
- nullable: _this.__nullable.allow,
445
- optional: _this.__optional.allow
446
- }
447
- };
448
- }, {}, {
449
- shouldAddStringVersion: options.shouldAddStringVersion,
450
- fallbackIfNotSupported: /*#__PURE__*/ _async_to_generator(function() {
451
- var transformedSchemasAsPromises, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, schema;
452
- return _ts_generator(this, function(_state) {
453
- switch(_state.label){
454
- case 0:
455
- if (options.appendFallbacksBeforeAdapterValidation) options.appendFallbacksBeforeAdapterValidation(_this, 'union', function() {
456
- var _ref = _async_to_generator(function(adapter, fieldAdapter, schema, translatedSchemas, value, path, options) {
457
- var parsedValues, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, translatedSchema, _ref, parsed, errors, err;
458
- return _ts_generator(this, function(_state) {
459
- switch(_state.label){
460
- case 0:
461
- parsedValues = {
462
- parsed: value,
463
- errors: []
464
- };
465
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
466
- _state.label = 1;
467
- case 1:
468
- _state.trys.push([
469
- 1,
470
- 6,
471
- 7,
472
- 8
473
- ]);
474
- _iterator = translatedSchemas[Symbol.iterator]();
475
- _state.label = 2;
476
- case 2:
477
- if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
478
- 3,
479
- 5
480
- ];
481
- translatedSchema = _step.value;
482
- return [
483
- 4,
484
- schema.__validateByAdapter(adapter, fieldAdapter, translatedSchema, value, path, options)
485
- ];
486
- case 3:
487
- _ref = _state.sent(), parsed = _ref.parsed, errors = _ref.errors;
488
- // eslint-disable-next-line ts/no-unnecessary-condition
489
- if ((errors || []).length <= 0) return [
490
- 2,
491
- {
492
- parsed: parsed,
493
- errors: errors
494
- }
495
- ];
496
- else {
497
- parsedValues.parsed = parsed;
498
- // eslint-disable-next-line ts/no-unnecessary-condition
499
- parsedValues.errors = (parsedValues.errors || []).concat(errors || []);
500
- }
501
- _state.label = 4;
502
- case 4:
503
- _iteratorNormalCompletion = true;
504
- return [
505
- 3,
506
- 2
507
- ];
508
- case 5:
509
- return [
510
- 3,
511
- 8
512
- ];
513
- case 6:
514
- err = _state.sent();
515
- _didIteratorError = true;
516
- _iteratorError = err;
517
- return [
518
- 3,
519
- 8
520
- ];
521
- case 7:
522
- try {
523
- if (!_iteratorNormalCompletion && _iterator.return != null) {
524
- _iterator.return();
525
- }
526
- } finally{
527
- if (_didIteratorError) {
528
- throw _iteratorError;
529
- }
530
- }
531
- return [
532
- 7
533
- ];
534
- case 8:
535
- return [
536
- 2,
537
- parsedValues
538
- ];
539
- }
540
- });
541
- });
542
- return function(adapter, fieldAdapter, schema, translatedSchemas, value, path, options) {
543
- return _ref.apply(this, arguments);
544
- };
545
- }());
546
- transformedSchemasAsPromises = [];
547
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
548
- try {
549
- for(_iterator = _this.__schemas[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
550
- schema = _step.value;
551
- transformedSchemasAsPromises.push(schema.__transformToAdapter(options));
552
- }
553
- } catch (err) {
554
- _didIteratorError = true;
555
- _iteratorError = err;
556
- } finally{
557
- try {
558
- if (!_iteratorNormalCompletion && _iterator.return != null) {
559
- _iterator.return();
560
- }
561
- } finally{
562
- if (_didIteratorError) {
563
- throw _iteratorError;
564
- }
565
- }
566
- }
567
- return [
568
- 4,
569
- Promise.all(transformedSchemasAsPromises)
570
- ];
571
- case 1:
572
- return [
573
- 2,
574
- _state.sent().flat()
575
- ];
576
- }
577
- });
578
- })
579
- })
580
- ];
581
- }
582
- });
583
- });
584
- return function(adapter) {
585
- return _ref.apply(this, arguments);
586
- };
587
- }(), _this, _this.__transformedSchemas, options, 'union')
588
- ];
589
- });
590
- })();
591
- }
592
- },
593
- {
594
- /**
595
- * This let's you refine the schema with custom validations. This is useful when you want to validate something that
596
- * is not supported by default by the schema adapter.
597
- *
598
- * @example
599
- * ```typescript
600
- * import * as p from '@palmares/schemas';
601
- *
602
- * const numberSchema = p.number().refine((value) => {
603
- * if (value < 0) return { code: 'invalid_number', message: 'The number should be greater than 0' };
604
- * });
605
- *
606
- * const { errors, parsed } = await numberSchema.parse(-1);
607
- *
608
- * // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
609
- * console.log(errors);
610
- * ```
611
- *
612
- * @param refinementCallback - The callback that will be called to validate the value.
613
- * @param options - Options for the refinement.
614
- * @param options.isAsync - Whether the callback is async or not. Defaults to true.
615
- */ key: "refine",
616
- value: function refine(refinementCallback) {
617
- return _get(_get_prototype_of(UnionSchema.prototype), "refine", this).call(this, refinementCallback);
618
- }
619
- },
620
- {
621
- /**
622
- * Allows the value to be either undefined or null.
623
- *
624
- * @example
625
- * ```typescript
626
- * import * as p from '@palmares/schemas';
627
- *
628
- * const numberSchema = p.number().optional();
629
- *
630
- * const { errors, parsed } = await numberSchema.parse(undefined);
631
- *
632
- * console.log(parsed); // undefined
633
- *
634
- * const { errors, parsed } = await numberSchema.parse(null);
635
- *
636
- * console.log(parsed); // null
637
- *
638
- * const { errors, parsed } = await numberSchema.parse(1);
639
- *
640
- * console.log(parsed); // 1
641
- * ```
642
- *
643
- * @returns - The schema we are working with.
644
- */ key: "optional",
645
- value: function optional(options) {
646
- return _get(_get_prototype_of(UnionSchema.prototype), "optional", this).call(this, options);
647
- }
648
- },
649
- {
650
- /**
651
- * Just adds a message when the value is undefined. It's just a syntax sugar for
652
- *
653
- * ```typescript
654
- * p.string().optional({ message: 'This value cannot be null', allow: false })
655
- * ```
656
- *
657
- * @param options - The options of nonOptional function
658
- * @param options.message - A custom message if the value is undefined.
659
- *
660
- * @returns - The schema.
661
- */ key: "nonOptional",
662
- value: function nonOptional(options) {
663
- return _get(_get_prototype_of(UnionSchema.prototype), "optional", this).call(this, {
664
- message: options === null || options === void 0 ? void 0 : options.message,
665
- allow: false
666
- });
667
- }
668
- },
669
- {
670
- /**
671
- * Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
672
- * is NULL by setting the { message: 'Your custom message', allow: false } on the options.
673
- *
674
- * @example
675
- * ```typescript
676
- * import * as p from '@palmares/schemas';
677
- *
678
- * const numberSchema = p.number().nullable();
679
- *
680
- * const { errors, parsed } = await numberSchema.parse(null);
681
- *
682
- * console.log(parsed); // null
683
- *
684
- * const { errors, parsed } = await numberSchema.parse(undefined);
685
- *
686
- * console.log(errors); // [{ isValid: false, code: 'invalid_type', message: 'Invalid type', path: [] }]
687
- * ```
688
- *
689
- * @param options - The options for the nullable function.
690
- * @param options.message - The message to be shown when the value is not null. Defaults to 'Cannot be null'.
691
- * @param options.allow - Whether the value can be null or not. Defaults to true.
692
- *
693
- * @returns The schema.
694
- */ key: "nullable",
695
- value: function nullable(options) {
696
- return _get(_get_prototype_of(UnionSchema.prototype), "nullable", this).call(this, options);
697
- }
698
- },
699
- {
700
- /**
701
- * Just adds a message when the value is null. It's just a syntax sugar for
702
- *
703
- * ```typescript
704
- * p.string().nullable({ message: 'This value cannot be null', allow: false })
705
- * ```
706
- *
707
- * @param options - The options of nonNullable function
708
- * @param options.message - A custom message if the value is null.
709
- *
710
- * @returns - The schema.
711
- */ key: "nonNullable",
712
- value: function nonNullable(options) {
713
- return _get(_get_prototype_of(UnionSchema.prototype), "nullable", this).call(this, {
714
- message: (options === null || options === void 0 ? void 0 : options.message) || '',
715
- allow: false
716
- });
717
- }
718
- },
719
- {
720
- /**
721
- * This method will remove the value from the representation of the schema. If the value is undefined it will keep
722
- * that way
723
- * otherwise it will set the value to undefined after it's validated.
724
- * This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
725
- * function. This will remove the value from the representation of the schema.
726
- *
727
- * By default, the value will be removed just from the representation, in other words, when you call the {@link data}
728
- * function. But if you want to remove the value from the internal representation, you can pass the argument
729
- * `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
730
- * the argument `toRepresentation` as true as well.
731
- *
732
- * @example
733
- * ```typescript
734
- * import * as p from '@palmares/schemas';
735
- *
736
- * const userSchema = p.object({
737
- * id: p.number().optional(),
738
- * name: p.string(),
739
- * password: p.string().omit()
740
- * });
741
- *
742
- * const user = await userSchema.data({
743
- * id: 1,
744
- * name: 'John Doe',
745
- * password: '123456'
746
- * });
747
- *
748
- * console.log(user); // { id: 1, name: 'John Doe' }
749
- * ```
750
- *
751
- *
752
- * @param args - By default, the value will be removed just from the representation, in other words, when you call
753
- * the {@link data} function. But if you want to remove the value from the internal representation, you can pass
754
- * the argument `toInternal` as true. Then if you still want to remove the value from the representation, you
755
- * will need to pass the argument `toRepresentation` as true as well.
756
- *
757
- * @returns The schema.
758
- */ key: "omit",
759
- value: function omit(args) {
760
- return _get(_get_prototype_of(UnionSchema.prototype), "omit", this).call(this, args);
761
- }
762
- },
763
- {
764
- /**
765
- * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
766
- * source like a database. You should always return the schema after you save the value, that way we will always
767
- * have the correct type of the schema after the save operation.
768
- *
769
- * You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
770
- *
771
- * @example
772
- * ```typescript
773
- * import * as p from '@palmares/schemas';
774
- *
775
- * import { User } from './models';
776
- *
777
- * const userSchema = p.object({
778
- * id: p.number().optional(),
779
- * name: p.string(),
780
- * email: p.string().email(),
781
- * }).onSave(async (value) => {
782
- * // Create or update the user on the database using palmares models or any other library of your choice.
783
- * if (value.id)
784
- * await User.default.set(value, { search: { id: value.id } });
785
- * else
786
- * await User.default.set(value);
787
- *
788
- * return value;
789
- * });
790
- *
791
- *
792
- * // Then, on your controller, do something like this:
793
- * const { isValid, save, errors } = await userSchema.validate(req.body);
794
- * if (isValid) {
795
- * const savedValue = await save();
796
- * return Response.json(savedValue, { status: 201 });
797
- * }
798
- *
799
- * return Response.json({ errors }, { status: 400 });
800
- * ```
801
- *
802
- * @param callback - The callback that will be called to save the value on an external source.
803
- *
804
- * @returns The schema.
805
- */ key: "onSave",
806
- value: function onSave(callback) {
807
- return _get(_get_prototype_of(UnionSchema.prototype), "onSave", this).call(this, callback);
808
- }
809
- },
810
- {
811
- /**
812
- * This function is used to add a default value to the schema. If the value is either undefined or null, the default
813
- * value will be used.
814
- *
815
- * @example
816
- * ```typescript
817
- * import * as p from '@palmares/schemas';
818
- *
819
- * const numberSchema = p.number().default(0);
820
- *
821
- * const { errors, parsed } = await numberSchema.parse(undefined);
822
- *
823
- * console.log(parsed); // 0
824
- * ```
825
- */ key: "default",
826
- value: function _default(defaultValueOrFunction) {
827
- return _get(_get_prototype_of(UnionSchema.prototype), "default", this).call(this, defaultValueOrFunction);
828
- }
829
- },
830
- {
831
- key: "data",
832
- value: /**
833
- * This function is used to transform the value to the representation without validating it.
834
- * This is useful when you want to return a data from a query directly to the user. But for example
835
- * you are returning the data of a user, you can clean the password or any other sensitive data.
836
- *
837
- * @example
838
- * ```typescript
839
- * import * as p from '@palmares/schemas';
840
- *
841
- * const userSchema = p.object({
842
- * id: p.number().optional(),
843
- * name: p.string(),
844
- * email: p.string().email(),
845
- * password: p.string().optional()
846
- * }).toRepresentation(async (value) => {
847
- * return {
848
- * id: value.id,
849
- * name: value.name,
850
- * email: value.email
851
- * }
852
- * });
853
- *
854
- * const user = await userSchema.data({
855
- * id: 1,
856
- * name: 'John Doe',
857
- * email: 'john@gmail.com',
858
- * password: '123456'
859
- * });
860
- * ```
861
- */ function data(value) {
862
- var _this = this;
863
- var _this1 = this, _superprop_get_data = function() {
864
- return _get(_get_prototype_of(UnionSchema.prototype), "data", _this);
865
- };
866
- return _async_to_generator(function() {
867
- var parsedValue, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, schema;
868
- return _ts_generator(this, function(_state) {
869
- switch(_state.label){
870
- case 0:
871
- return [
872
- 4,
873
- _superprop_get_data().call(_this1, value)
874
- ];
875
- case 1:
876
- parsedValue = _state.sent();
877
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
878
- try {
879
- for(_iterator = Array.from(_this1.__schemas)[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
880
- schema = _step.value;
881
- if (schema['__optional'].allow && value === undefined) return [
882
- 2,
883
- schema.data(parsedValue)
884
- ];
885
- if (schema['__nullable'].allow && value === null) return [
886
- 2,
887
- schema.data(parsedValue)
888
- ];
889
- if (schema['__type'].check(parsedValue)) return [
890
- 2,
891
- schema.data(parsedValue)
892
- ];
893
- }
894
- } catch (err) {
895
- _didIteratorError = true;
896
- _iteratorError = err;
897
- } finally{
898
- try {
899
- if (!_iteratorNormalCompletion && _iterator.return != null) {
900
- _iterator.return();
901
- }
902
- } finally{
903
- if (_didIteratorError) {
904
- throw _iteratorError;
905
- }
906
- }
907
- }
908
- return [
909
- 2,
910
- parsedValue
911
- ];
912
- }
913
- });
914
- })();
915
- }
916
- },
917
- {
918
- /**
919
- * This function let's you customize the schema your own way. After we translate the schema on the adapter we call
920
- * this function to let you customize the custom schema your own way. Our API does not support passthrough?
921
- * No problem, you can use this function to customize the zod schema.
922
- *
923
- * @example
924
- * ```typescript
925
- * import * as p from '@palmares/schemas';
926
- *
927
- * const numberSchema = p.number().extends((schema) => {
928
- * return schema.nonnegative();
929
- * });
930
- *
931
- * const { errors, parsed } = await numberSchema.parse(-1);
932
- *
933
- * // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
934
- * console.log(errors);
935
- * ```
936
- *
937
- * @param callback - The callback that will be called to customize the schema.
938
- * @param toStringCallback - The callback that will be called to transform the schema to a string when you want
939
- * to compile the underlying schema to a string so you can save it for future runs.
940
- *
941
- * @returns The schema.
942
- */ key: "extends",
943
- value: function _extends(callback, toStringCallback) {
944
- return _get(_get_prototype_of(UnionSchema.prototype), "extends", this).call(this, callback, toStringCallback);
945
- }
946
- },
947
- {
948
- /**
949
- * This function is used to transform the value to the representation of the schema. When using the {@link data}
950
- * function. With this function you have full control to add data cleaning for example, transforming the data and
951
- * whatever. Another use case is when you want to return deeply nested recursive data. The schema maps to itself.
952
- *
953
- * @example
954
- * ```typescript
955
- * import * as p from '@palmares/schemas';
956
- *
957
- * const recursiveSchema = p.object({
958
- * id: p.number().optional(),
959
- * name: p.string(),
960
- * }).toRepresentation(async (value) => {
961
- * return {
962
- * id: value.id,
963
- * name: value.name,
964
- * children: await Promise.all(value.children.map(async (child) => await recursiveSchema.data(child)))
965
- * }
966
- * });
967
- *
968
- * const data = await recursiveSchema.data({
969
- * id: 1,
970
- * name: 'John Doe',
971
- * });
972
- * ```
973
- *
974
- * @example
975
- * ```
976
- * import * as p from '@palmares/schemas';
977
- *
978
- * const colorToRGBSchema = p.string().toRepresentation(async (value) => {
979
- * switch (value) {
980
- * case 'red': return { r: 255, g: 0, b: 0 };
981
- * case 'green': return { r: 0, g: 255, b: 0 };
982
- * case 'blue': return { r: 0, g: 0, b: 255 };
983
- * default: return { r: 0, g: 0, b: 0 };
984
- * }
985
- * });
986
- * ```
987
- * @param toRepresentationCallback - The callback that will be called to transform the value to the representation.
988
- *
989
- * @returns The schema with a new return type
990
- */ key: "toRepresentation",
991
- value: function toRepresentation(toRepresentationCallback) {
992
- return _get(_get_prototype_of(UnionSchema.prototype), "toRepresentation", this).call(this, toRepresentationCallback);
993
- }
994
- },
995
- {
996
- /**
997
- * This function is used to transform the value to the internal representation of the schema. This is useful when you
998
- * want to transform the value to a type that the schema adapter can understand. For example, you might want to
999
- * transform a string to a date. This is the function you use.
1000
- *
1001
- * @example
1002
- * ```typescript
1003
- * import * as p from '@palmares/schemas';
1004
- *
1005
- * const dateSchema = p.string().toInternal((value) => {
1006
- * return new Date(value);
1007
- * });
1008
- *
1009
- * const date = await dateSchema.parse('2021-01-01');
1010
- *
1011
- * console.log(date); // Date object
1012
- *
1013
- * const rgbToColorSchema = p.object({
1014
- * r: p.number().min(0).max(255),
1015
- * g: p.number().min(0).max(255),
1016
- * b: p.number().min(0).max(255),
1017
- * }).toInternal(async (value) => {
1018
- * if (value.r === 255 && value.g === 0 && value.b === 0) return 'red';
1019
- * if (value.r === 0 && value.g === 255 && value.b === 0) return 'green';
1020
- * if (value.r === 0 && value.g === 0 && value.b === 255) return 'blue';
1021
- * return `rgb(${value.r}, ${value.g}, ${value.b})`;
1022
- * });
1023
- * ```
1024
- *
1025
- * @param toInternalCallback - The callback that will be called to transform the value to the internal representation.
1026
- *
1027
- * @returns The schema with a new return type.
1028
- */ key: "toInternal",
1029
- value: function toInternal(toInternalCallback) {
1030
- return _get(_get_prototype_of(UnionSchema.prototype), "toInternal", this).call(this, toInternalCallback);
1031
- }
1032
- },
1033
- {
1034
- /**
1035
- * Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
1036
- * you can convert that string to a date here BEFORE the validation. This pretty much transforms the value to a type
1037
- * that the schema adapter can understand.
1038
- *
1039
- * @example
1040
- * ```
1041
- * import * as p from '@palmares/schemas';
1042
- * import * as z from 'zod';
1043
- *
1044
- * const customRecordToMapSchema = p.schema().appendSchema(z.map()).toValidate(async (value) => {
1045
- * return new Map(value); // Before validating we transform the value to a map.
1046
- * });
1047
- *
1048
- * const { errors, parsed } = await customRecordToMapSchema.parse({ key: 'value' });
1049
- * ```
1050
- *
1051
- * @param toValidateCallback - The callback that will be called to validate the value.
1052
- *
1053
- * @returns The schema with a new return type.
1054
- */ key: "toValidate",
1055
- value: function toValidate(toValidateCallback) {
1056
- return _get(_get_prototype_of(UnionSchema.prototype), "toValidate", this).call(this, toValidateCallback);
1057
- }
1058
- }
1059
- ], [
1060
- {
1061
- key: "new",
1062
- value: function _new(schemas) {
1063
- var returnValue = new UnionSchema(schemas);
1064
- return returnValue;
1065
- }
1066
- }
1067
- ]);
1068
- return UnionSchema;
1069
- }(_schema.Schema);
1070
- var union = UnionSchema.new;