@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,1840 +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
- Schema: function() {
13
- return Schema;
14
- },
15
- schema: function() {
16
- return schema;
17
- }
18
- });
19
- var _conf = require("../conf");
20
- var _utils = require("../utils");
21
- function _array_like_to_array(arr, len) {
22
- if (len == null || len > arr.length) len = arr.length;
23
- for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
24
- return arr2;
25
- }
26
- function _array_with_holes(arr) {
27
- if (Array.isArray(arr)) return arr;
28
- }
29
- function _array_without_holes(arr) {
30
- if (Array.isArray(arr)) return _array_like_to_array(arr);
31
- }
32
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
33
- try {
34
- var info = gen[key](arg);
35
- var value = info.value;
36
- } catch (error) {
37
- reject(error);
38
- return;
39
- }
40
- if (info.done) {
41
- resolve(value);
42
- } else {
43
- Promise.resolve(value).then(_next, _throw);
44
- }
45
- }
46
- function _async_to_generator(fn) {
47
- return function() {
48
- var self = this, args = arguments;
49
- return new Promise(function(resolve, reject) {
50
- var gen = fn.apply(self, args);
51
- function _next(value) {
52
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
53
- }
54
- function _throw(err) {
55
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
56
- }
57
- _next(undefined);
58
- });
59
- };
60
- }
61
- function _class_call_check(instance, Constructor) {
62
- if (!(instance instanceof Constructor)) {
63
- throw new TypeError("Cannot call a class as a function");
64
- }
65
- }
66
- function _defineProperties(target, props) {
67
- for(var i = 0; i < props.length; i++){
68
- var descriptor = props[i];
69
- descriptor.enumerable = descriptor.enumerable || false;
70
- descriptor.configurable = true;
71
- if ("value" in descriptor) descriptor.writable = true;
72
- Object.defineProperty(target, descriptor.key, descriptor);
73
- }
74
- }
75
- function _create_class(Constructor, protoProps, staticProps) {
76
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
77
- if (staticProps) _defineProperties(Constructor, staticProps);
78
- return Constructor;
79
- }
80
- function _define_property(obj, key, value) {
81
- if (key in obj) {
82
- Object.defineProperty(obj, key, {
83
- value: value,
84
- enumerable: true,
85
- configurable: true,
86
- writable: true
87
- });
88
- } else {
89
- obj[key] = value;
90
- }
91
- return obj;
92
- }
93
- function _instanceof(left, right) {
94
- if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
95
- return !!right[Symbol.hasInstance](left);
96
- } else {
97
- return left instanceof right;
98
- }
99
- }
100
- function _iterable_to_array(iter) {
101
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
102
- }
103
- function _iterable_to_array_limit(arr, i) {
104
- var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
105
- if (_i == null) return;
106
- var _arr = [];
107
- var _n = true;
108
- var _d = false;
109
- var _s, _e;
110
- try {
111
- for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
112
- _arr.push(_s.value);
113
- if (i && _arr.length === i) break;
114
- }
115
- } catch (err) {
116
- _d = true;
117
- _e = err;
118
- } finally{
119
- try {
120
- if (!_n && _i["return"] != null) _i["return"]();
121
- } finally{
122
- if (_d) throw _e;
123
- }
124
- }
125
- return _arr;
126
- }
127
- function _non_iterable_rest() {
128
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
129
- }
130
- function _non_iterable_spread() {
131
- throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
132
- }
133
- function _sliced_to_array(arr, i) {
134
- return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
135
- }
136
- function _to_consumable_array(arr) {
137
- return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
138
- }
139
- function _unsupported_iterable_to_array(o, minLen) {
140
- if (!o) return;
141
- if (typeof o === "string") return _array_like_to_array(o, minLen);
142
- var n = Object.prototype.toString.call(o).slice(8, -1);
143
- if (n === "Object" && o.constructor) n = o.constructor.name;
144
- if (n === "Map" || n === "Set") return Array.from(n);
145
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
146
- }
147
- function _ts_generator(thisArg, body) {
148
- var f, y, t, g, _ = {
149
- label: 0,
150
- sent: function() {
151
- if (t[0] & 1) throw t[1];
152
- return t[1];
153
- },
154
- trys: [],
155
- ops: []
156
- };
157
- return g = {
158
- next: verb(0),
159
- "throw": verb(1),
160
- "return": verb(2)
161
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
162
- return this;
163
- }), g;
164
- function verb(n) {
165
- return function(v) {
166
- return step([
167
- n,
168
- v
169
- ]);
170
- };
171
- }
172
- function step(op) {
173
- if (f) throw new TypeError("Generator is already executing.");
174
- while(_)try {
175
- 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;
176
- if (y = 0, t) op = [
177
- op[0] & 2,
178
- t.value
179
- ];
180
- switch(op[0]){
181
- case 0:
182
- case 1:
183
- t = op;
184
- break;
185
- case 4:
186
- _.label++;
187
- return {
188
- value: op[1],
189
- done: false
190
- };
191
- case 5:
192
- _.label++;
193
- y = op[1];
194
- op = [
195
- 0
196
- ];
197
- continue;
198
- case 7:
199
- op = _.ops.pop();
200
- _.trys.pop();
201
- continue;
202
- default:
203
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
204
- _ = 0;
205
- continue;
206
- }
207
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
208
- _.label = op[1];
209
- break;
210
- }
211
- if (op[0] === 6 && _.label < t[1]) {
212
- _.label = t[1];
213
- t = op;
214
- break;
215
- }
216
- if (t && _.label < t[2]) {
217
- _.label = t[2];
218
- _.ops.push(op);
219
- break;
220
- }
221
- if (t[2]) _.ops.pop();
222
- _.trys.pop();
223
- continue;
224
- }
225
- op = body.call(thisArg, _);
226
- } catch (e) {
227
- op = [
228
- 6,
229
- e
230
- ];
231
- y = 0;
232
- } finally{
233
- f = t = 0;
234
- }
235
- if (op[0] & 5) throw op[1];
236
- return {
237
- value: op[0] ? op[1] : void 0,
238
- done: true
239
- };
240
- }
241
- }
242
- var Schema = /*#__PURE__*/ function() {
243
- "use strict";
244
- function Schema() {
245
- _class_call_check(this, Schema);
246
- _define_property(this, "$$type", '$PSchema');
247
- _define_property(this, "fieldType", 'schema');
248
- // Those functions will assume control of the validation process on adapters, instead of the schema.
249
- // Why this is used? The idea is that the Schema has NO idea
250
- // that one of it's children might be an UnionSchema for example. The adapter might not support unions,
251
- // so then we give control to the union. The parent schema will already have an array of translated
252
- // adapter schemas. This means for a union with Number and String it'll generate two schemas, one for number
253
- // and one for the value as String. Of course this gets multiplied. So if we have a union with Number and String.
254
- // We should take those two schemas from the array and validate them individually. This logic is
255
- // handled by the union schema. If we have an intersection type for example, instead of validating
256
- // One schema OR the other, we validate one schema AND the other. This will be handled
257
- // by the schema that contains that intersection logic.
258
- _define_property(this, "__beforeValidationCallbacks", new Map());
259
- _define_property(this, "__cachedGetParent", void 0);
260
- _define_property(this, "__alreadyAppliedModel", void 0);
261
- _define_property(this, "__runBeforeParseAndData", void 0);
262
- _define_property(this, "__rootFallbacksValidator", void 0);
263
- _define_property(this, "__saveCallback", void 0);
264
- _define_property(this, "__modelOmitCallback", void 0);
265
- _define_property(this, "__parsers", {
266
- high: new Map(),
267
- medium: new Map(),
268
- low: new Map(),
269
- _fallbacks: new Set()
270
- });
271
- _define_property(this, "__refinements", []);
272
- _define_property(this, "__nullable", {
273
- message: 'Cannot be null',
274
- allow: false
275
- });
276
- _define_property(this, "__optional", {
277
- message: 'Required',
278
- allow: false
279
- });
280
- _define_property(this, "__extends", undefined);
281
- _define_property(this, "__transformedSchemas", {});
282
- _define_property(this, "__defaultFunction", undefined);
283
- _define_property(this, "__toRepresentation", undefined);
284
- _define_property(this, "__toValidate", undefined);
285
- _define_property(this, "__toInternal", undefined);
286
- _define_property(this, "__type", {
287
- message: 'Invalid type',
288
- check: function() {
289
- return true;
290
- }
291
- });
292
- }
293
- _create_class(Schema, [
294
- {
295
- key: "__getParent",
296
- get: function get() {
297
- return this.__cachedGetParent;
298
- },
299
- set: function set(value) {
300
- this.__cachedGetParent = value;
301
- }
302
- },
303
- {
304
- key: "__getDefaultTransformedSchemas",
305
- value: function __getDefaultTransformedSchemas() {
306
- var adapterInstance = (0, _conf.getDefaultAdapter)();
307
- // eslint-disable-next-line ts/no-unnecessary-condition
308
- if (this.__transformedSchemas[adapterInstance.constructor.name] === undefined) this.__transformedSchemas[adapterInstance.constructor.name] = {
309
- transformed: false,
310
- adapter: adapterInstance,
311
- schemas: []
312
- };
313
- }
314
- },
315
- {
316
- key: "__validateByFallbacks",
317
- value: /**
318
- * This will validate the data with the fallbacks, so internally, without relaying on the schema adapter.
319
- * This is nice because we can support things that the schema adapter is not able to support by default.
320
- *
321
- * @param errorsAsHashedSet - The errors as a hashed set. This is used to prevent duplicate errors.
322
- * @param path - The path of the error.
323
- * @param parseResult - The result of the parse method.
324
- */ function __validateByFallbacks(path, parseResult, options) {
325
- var _this = this;
326
- return _async_to_generator(function() {
327
- return _ts_generator(this, function(_state) {
328
- // eslint-disable-next-line ts/no-unnecessary-condition
329
- if (_this.__rootFallbacksValidator) return [
330
- 2,
331
- _this.__rootFallbacksValidator.validate(options.errorsAsHashedSet, path, parseResult, options)
332
- ];
333
- return [
334
- 2,
335
- parseResult
336
- ];
337
- });
338
- })();
339
- }
340
- },
341
- {
342
- key: "__validateByAdapter",
343
- value: /**
344
- * This will validate by the adapter. In other words, we send the data to the schema adapter and then we validate
345
- * that data.
346
- * So understand that, first we send the data to the adapter, the adapter validates it, then, after we validate
347
- * from the adapter we validate with the fallbacks so we can do all of the extra validations not handled by
348
- * the adapter.
349
- *
350
- * @param value - The value to be validated.
351
- * @param errorsAsHashedSet - The errors as a hashed set. This is used to prevent duplicate errors on the validator.
352
- * @param path - The path of the error so we can construct an object with the nested paths of the error.
353
- * @param parseResult - The result of the parse method.
354
- *
355
- * @returns The result and the errors of the parse method.
356
- */ function __validateByAdapter(adapter, fieldAdapter, schema, value, path, options) {
357
- return _async_to_generator(function() {
358
- var parseResult, adapterParseResult;
359
- return _ts_generator(this, function(_state) {
360
- switch(_state.label){
361
- case 0:
362
- parseResult = {
363
- errors: [],
364
- parsed: value
365
- };
366
- // On the next iteration we will reset the errors and the parsed value
367
- parseResult.errors = [];
368
- parseResult.parsed = value;
369
- // eslint-disable-next-line ts/no-unnecessary-condition
370
- if (fieldAdapter === undefined || typeof fieldAdapter.parse !== 'function') return [
371
- 2,
372
- parseResult
373
- ];
374
- return [
375
- 4,
376
- fieldAdapter.parse(adapter, adapter.field, schema.transformed, value, options.args)
377
- ];
378
- case 1:
379
- adapterParseResult = _state.sent();
380
- parseResult.parsed = adapterParseResult.parsed;
381
- if (!adapterParseResult.errors) return [
382
- 3,
383
- 5
384
- ];
385
- if (!Array.isArray(adapterParseResult.errors)) return [
386
- 3,
387
- 3
388
- ];
389
- return [
390
- 4,
391
- Promise.all(adapterParseResult.errors.map(function() {
392
- var _ref = _async_to_generator(function(error) {
393
- return _ts_generator(this, function(_state) {
394
- return [
395
- 2,
396
- (0, _utils.formatErrorFromParseMethod)(adapter, fieldAdapter, error, value, schema.transformed, path, options.errorsAsHashedSet || new Set())
397
- ];
398
- });
399
- });
400
- return function(error) {
401
- return _ref.apply(this, arguments);
402
- };
403
- }()))
404
- ];
405
- case 2:
406
- parseResult.errors = _state.sent();
407
- return [
408
- 3,
409
- 5
410
- ];
411
- case 3:
412
- return [
413
- 4,
414
- (0, _utils.formatErrorFromParseMethod)(adapter, fieldAdapter, parseResult.errors, value, schema.transformed, path, options.errorsAsHashedSet || new Set())
415
- ];
416
- case 4:
417
- parseResult.errors = [
418
- _state.sent()
419
- ];
420
- _state.label = 5;
421
- case 5:
422
- parseResult.errors = parseResult.errors.filter(function(error) {
423
- return typeof error !== 'undefined';
424
- });
425
- return [
426
- 2,
427
- parseResult
428
- ];
429
- }
430
- });
431
- })();
432
- }
433
- },
434
- {
435
- key: "__transformToAdapter",
436
- value: // eslint-disable-next-line ts/require-await
437
- function __transformToAdapter(_options) {
438
- return _async_to_generator(function() {
439
- return _ts_generator(this, function(_state) {
440
- throw new Error('Not implemented');
441
- });
442
- })();
443
- }
444
- },
445
- {
446
- key: "__parsersToTransformValue",
447
- value: /** */ function __parsersToTransformValue(value, parsersToUse) {
448
- var _this = this;
449
- return _async_to_generator(function() {
450
- var shouldStop, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _step_value, parserName, parser, result, err, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, _step_value1, parserName1, parser1, result1, err, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, _step_value2, parserName2, parser2, result2, err;
451
- return _ts_generator(this, function(_state) {
452
- switch(_state.label){
453
- case 0:
454
- shouldStop = false;
455
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
456
- _state.label = 1;
457
- case 1:
458
- _state.trys.push([
459
- 1,
460
- 7,
461
- 8,
462
- 9
463
- ]);
464
- _iterator = _this.__parsers.high.entries()[Symbol.iterator]();
465
- _state.label = 2;
466
- case 2:
467
- if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
468
- 3,
469
- 6
470
- ];
471
- _step_value = _sliced_to_array(_step.value, 2), parserName = _step_value[0], parser = _step_value[1];
472
- if (!(_instanceof(parsersToUse, Set) === false || parsersToUse.has(parserName))) return [
473
- 3,
474
- 4
475
- ];
476
- return [
477
- 4,
478
- Promise.resolve(parser(value))
479
- ];
480
- case 3:
481
- result = _state.sent();
482
- if (result.preventNextParsers) shouldStop = true;
483
- value = result.value;
484
- return [
485
- 3,
486
- 5
487
- ];
488
- case 4:
489
- return [
490
- 3,
491
- 5
492
- ];
493
- case 5:
494
- _iteratorNormalCompletion = true;
495
- return [
496
- 3,
497
- 2
498
- ];
499
- case 6:
500
- return [
501
- 3,
502
- 9
503
- ];
504
- case 7:
505
- err = _state.sent();
506
- _didIteratorError = true;
507
- _iteratorError = err;
508
- return [
509
- 3,
510
- 9
511
- ];
512
- case 8:
513
- try {
514
- if (!_iteratorNormalCompletion && _iterator.return != null) {
515
- _iterator.return();
516
- }
517
- } finally{
518
- if (_didIteratorError) {
519
- throw _iteratorError;
520
- }
521
- }
522
- return [
523
- 7
524
- ];
525
- case 9:
526
- if (!(shouldStop === false)) return [
527
- 3,
528
- 18
529
- ];
530
- _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
531
- _state.label = 10;
532
- case 10:
533
- _state.trys.push([
534
- 10,
535
- 16,
536
- 17,
537
- 18
538
- ]);
539
- _iterator1 = _this.__parsers.medium.entries()[Symbol.iterator]();
540
- _state.label = 11;
541
- case 11:
542
- if (!!(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done)) return [
543
- 3,
544
- 15
545
- ];
546
- _step_value1 = _sliced_to_array(_step1.value, 2), parserName1 = _step_value1[0], parser1 = _step_value1[1];
547
- if (!(_instanceof(parsersToUse, Set) === false || parsersToUse.has(parserName1))) return [
548
- 3,
549
- 13
550
- ];
551
- return [
552
- 4,
553
- Promise.resolve(parser1(value))
554
- ];
555
- case 12:
556
- result1 = _state.sent();
557
- if (result1.preventNextParsers) shouldStop = true;
558
- value = result1.value;
559
- return [
560
- 3,
561
- 14
562
- ];
563
- case 13:
564
- return [
565
- 3,
566
- 14
567
- ];
568
- case 14:
569
- _iteratorNormalCompletion1 = true;
570
- return [
571
- 3,
572
- 11
573
- ];
574
- case 15:
575
- return [
576
- 3,
577
- 18
578
- ];
579
- case 16:
580
- err = _state.sent();
581
- _didIteratorError1 = true;
582
- _iteratorError1 = err;
583
- return [
584
- 3,
585
- 18
586
- ];
587
- case 17:
588
- try {
589
- if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
590
- _iterator1.return();
591
- }
592
- } finally{
593
- if (_didIteratorError1) {
594
- throw _iteratorError1;
595
- }
596
- }
597
- return [
598
- 7
599
- ];
600
- case 18:
601
- if (!(shouldStop === false)) return [
602
- 3,
603
- 27
604
- ];
605
- _iteratorNormalCompletion2 = true, _didIteratorError2 = false, _iteratorError2 = undefined;
606
- _state.label = 19;
607
- case 19:
608
- _state.trys.push([
609
- 19,
610
- 25,
611
- 26,
612
- 27
613
- ]);
614
- _iterator2 = _this.__parsers.low.entries()[Symbol.iterator]();
615
- _state.label = 20;
616
- case 20:
617
- if (!!(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done)) return [
618
- 3,
619
- 24
620
- ];
621
- _step_value2 = _sliced_to_array(_step2.value, 2), parserName2 = _step_value2[0], parser2 = _step_value2[1];
622
- if (!(_instanceof(parsersToUse, Set) === false || parsersToUse.has(parserName2))) return [
623
- 3,
624
- 22
625
- ];
626
- return [
627
- 4,
628
- Promise.resolve(parser2(value))
629
- ];
630
- case 21:
631
- result2 = _state.sent();
632
- if (result2.preventNextParsers) shouldStop = true;
633
- value = result2.value;
634
- return [
635
- 3,
636
- 23
637
- ];
638
- case 22:
639
- return [
640
- 3,
641
- 23
642
- ];
643
- case 23:
644
- _iteratorNormalCompletion2 = true;
645
- return [
646
- 3,
647
- 20
648
- ];
649
- case 24:
650
- return [
651
- 3,
652
- 27
653
- ];
654
- case 25:
655
- err = _state.sent();
656
- _didIteratorError2 = true;
657
- _iteratorError2 = err;
658
- return [
659
- 3,
660
- 27
661
- ];
662
- case 26:
663
- try {
664
- if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
665
- _iterator2.return();
666
- }
667
- } finally{
668
- if (_didIteratorError2) {
669
- throw _iteratorError2;
670
- }
671
- }
672
- return [
673
- 7
674
- ];
675
- case 27:
676
- return [
677
- 2,
678
- value
679
- ];
680
- }
681
- });
682
- })();
683
- }
684
- },
685
- {
686
- key: "__parse",
687
- value: function __parse(value) {
688
- var path = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [], options = arguments.length > 2 ? arguments[2] : void 0;
689
- var _this = this;
690
- return _async_to_generator(function() {
691
- var _options_schemaAdapter, _options_toInternalToBubbleUp, shouldRunToInternalToBubbleUp, shouldCallDefaultFunction, shouldCallToValidateCallback, schemaAdapterFieldType, parseResult, adapterToUse, parsedResultsAfterFallbacks, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, callback, parsedValuesAfterValidationCallbacks, err, parsedValuesAfterValidatingByAdapter, hasToInternalCallback, shouldCallToInternalDuringParse, hasNoErrors, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, functionToModifyResult, err;
692
- return _ts_generator(this, function(_state) {
693
- switch(_state.label){
694
- case 0:
695
- _this.__getDefaultTransformedSchemas();
696
- if (!(typeof _this.__runBeforeParseAndData === 'function')) return [
697
- 3,
698
- 2
699
- ];
700
- return [
701
- 4,
702
- _this.__runBeforeParseAndData(_this)
703
- ];
704
- case 1:
705
- _state.sent();
706
- _state.label = 2;
707
- case 2:
708
- // This is used to run the toInternal command. If we didn't do this, we would need to parse through all of
709
- // the schemas to run the toInternal command, from the leafs (ObjectSchemas) to the root schema. This is not
710
- // a good idea, so what we do is that during validation the leafs attach a function to the
711
- // options.toInternalToBubbleUp like
712
- // `options.toInternalToBubbleUp.push(async () => (value[key] = await (schema as any).__toInternal(parsed)));``
713
- // This way, when the root schema finishes the validation, it will run all of the functions in the
714
- // toInternalToBubbleUp array, modifying the parsed value.
715
- shouldRunToInternalToBubbleUp = options.toInternalToBubbleUp === undefined;
716
- if (shouldRunToInternalToBubbleUp) options.toInternalToBubbleUp = [];
717
- if (_instanceof(options.errorsAsHashedSet, Set) === false) options.errorsAsHashedSet = new Set();
718
- shouldCallDefaultFunction = value === undefined && typeof _this.__defaultFunction === 'function';
719
- shouldCallToValidateCallback = typeof _this.__toValidate === 'function';
720
- schemaAdapterFieldType = _this.fieldType;
721
- if (!shouldCallDefaultFunction) return [
722
- 3,
723
- 4
724
- ];
725
- return [
726
- 4,
727
- _this.__defaultFunction()
728
- ];
729
- case 3:
730
- value = _state.sent();
731
- _state.label = 4;
732
- case 4:
733
- if (!shouldCallToValidateCallback) return [
734
- 3,
735
- 6
736
- ];
737
- return [
738
- 4,
739
- Promise.resolve(_this.__toValidate(value))
740
- ];
741
- case 5:
742
- value = _state.sent();
743
- _state.label = 6;
744
- case 6:
745
- parseResult = {
746
- errors: [],
747
- parsed: value
748
- };
749
- return [
750
- 4,
751
- _this.__parsersToTransformValue(value, _this.__parsers._fallbacks)
752
- ];
753
- case 7:
754
- value = _state.sent();
755
- if (options.appendFallbacksBeforeAdapterValidation === undefined) options.appendFallbacksBeforeAdapterValidation = function(schema, name, callback) {
756
- // We just need this if the union adapter is net defined but the parent is not of the same type.
757
- // For example, it's a union child of o object schema
758
- if (_this !== schema) _this.__beforeValidationCallbacks.set(name, callback);
759
- };
760
- if (!(_this.__transformedSchemas[((_options_schemaAdapter = options.schemaAdapter) === null || _options_schemaAdapter === void 0 ? void 0 : _options_schemaAdapter.constructor.name) || (0, _conf.getDefaultAdapter)().constructor.name].transformed === false)) return [
761
- 3,
762
- 9
763
- ];
764
- return [
765
- 4,
766
- _this.__transformToAdapter(options)
767
- ];
768
- case 8:
769
- _state.sent();
770
- _state.label = 9;
771
- case 9:
772
- adapterToUse = options.schemaAdapter ? options.schemaAdapter : Object.values(_this.__transformedSchemas)[0].adapter;
773
- return [
774
- 4,
775
- _this.__validateByFallbacks(path, {
776
- errors: parseResult.errors,
777
- parsed: value
778
- }, options)
779
- ];
780
- case 10:
781
- parsedResultsAfterFallbacks = _state.sent();
782
- parseResult.parsed = parsedResultsAfterFallbacks.parsed;
783
- // eslint-disable-next-line ts/no-unnecessary-condition
784
- parseResult.errors = (parseResult.errors || []).concat(parsedResultsAfterFallbacks.errors || []);
785
- if (!(_this.__beforeValidationCallbacks.size > 0)) return [
786
- 3,
787
- 19
788
- ];
789
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
790
- _state.label = 11;
791
- case 11:
792
- _state.trys.push([
793
- 11,
794
- 16,
795
- 17,
796
- 18
797
- ]);
798
- _iterator = _this.__beforeValidationCallbacks.values()[Symbol.iterator]();
799
- _state.label = 12;
800
- case 12:
801
- if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
802
- 3,
803
- 15
804
- ];
805
- callback = _step.value;
806
- return [
807
- 4,
808
- callback(adapterToUse, adapterToUse[schemaAdapterFieldType], _this, _this.__transformedSchemas[adapterToUse.constructor.name].schemas, value, path, options)
809
- ];
810
- case 13:
811
- parsedValuesAfterValidationCallbacks = _state.sent();
812
- parseResult.parsed = parsedValuesAfterValidationCallbacks.parsed;
813
- parseResult.errors = Array.isArray(parseResult.errors) && Array.isArray(parsedValuesAfterValidationCallbacks.errors) ? _to_consumable_array(parseResult.errors).concat(_to_consumable_array(parsedValuesAfterValidationCallbacks.errors)) : Array.isArray(parseResult.errors) ? parseResult.errors : parsedValuesAfterValidationCallbacks.errors;
814
- _state.label = 14;
815
- case 14:
816
- _iteratorNormalCompletion = true;
817
- return [
818
- 3,
819
- 12
820
- ];
821
- case 15:
822
- return [
823
- 3,
824
- 18
825
- ];
826
- case 16:
827
- err = _state.sent();
828
- _didIteratorError = true;
829
- _iteratorError = err;
830
- return [
831
- 3,
832
- 18
833
- ];
834
- case 17:
835
- try {
836
- if (!_iteratorNormalCompletion && _iterator.return != null) {
837
- _iterator.return();
838
- }
839
- } finally{
840
- if (_didIteratorError) {
841
- throw _iteratorError;
842
- }
843
- }
844
- return [
845
- 7
846
- ];
847
- case 18:
848
- return [
849
- 3,
850
- 21
851
- ];
852
- case 19:
853
- return [
854
- 4,
855
- _this.__validateByAdapter(adapterToUse, adapterToUse[schemaAdapterFieldType], _this.__transformedSchemas[adapterToUse.constructor.name].schemas[0], value, path, options)
856
- ];
857
- case 20:
858
- parsedValuesAfterValidatingByAdapter = _state.sent();
859
- parseResult.parsed = parsedValuesAfterValidatingByAdapter.parsed;
860
- // eslint-disable-next-line ts/no-unnecessary-condition
861
- parseResult.errors = (parseResult.errors || []).concat(parsedValuesAfterValidatingByAdapter.errors);
862
- _state.label = 21;
863
- case 21:
864
- hasToInternalCallback = typeof _this.__toInternal === 'function';
865
- shouldCallToInternalDuringParse = hasToInternalCallback && (((_options_toInternalToBubbleUp = options.toInternalToBubbleUp) === null || _options_toInternalToBubbleUp === void 0 ? void 0 : _options_toInternalToBubbleUp.length) === 0 || Array.isArray(options.toInternalToBubbleUp) === false);
866
- // eslint-disable-next-line ts/no-unnecessary-condition
867
- hasNoErrors = parseResult.errors === undefined || (parseResult.errors || []).length === 0;
868
- return [
869
- 4,
870
- Promise.all(_this.__refinements.map(function() {
871
- var _ref = _async_to_generator(function(refinement) {
872
- var errorOrNothing;
873
- return _ts_generator(this, function(_state) {
874
- switch(_state.label){
875
- case 0:
876
- return [
877
- 4,
878
- Promise.resolve(refinement(parseResult.parsed))
879
- ];
880
- case 1:
881
- errorOrNothing = _state.sent();
882
- if (typeof errorOrNothing === 'undefined') return [
883
- 2
884
- ];
885
- parseResult.errors.push({
886
- isValid: false,
887
- code: errorOrNothing.code,
888
- message: errorOrNothing.message,
889
- received: parseResult.parsed,
890
- path: path
891
- });
892
- return [
893
- 2
894
- ];
895
- }
896
- });
897
- });
898
- return function(refinement) {
899
- return _ref.apply(this, arguments);
900
- };
901
- }()))
902
- ];
903
- case 22:
904
- _state.sent();
905
- if (!(shouldCallToInternalDuringParse && hasNoErrors)) return [
906
- 3,
907
- 24
908
- ];
909
- return [
910
- 4,
911
- _this.__toInternal(value)
912
- ];
913
- case 23:
914
- parseResult.parsed = _state.sent();
915
- _state.label = 24;
916
- case 24:
917
- _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
918
- if (!(shouldRunToInternalToBubbleUp && hasNoErrors)) return [
919
- 3,
920
- 32
921
- ];
922
- _state.label = 25;
923
- case 25:
924
- _state.trys.push([
925
- 25,
926
- 30,
927
- 31,
928
- 32
929
- ]);
930
- _iterator1 = (options.toInternalToBubbleUp || [])[Symbol.iterator]();
931
- _state.label = 26;
932
- case 26:
933
- if (!!(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done)) return [
934
- 3,
935
- 29
936
- ];
937
- functionToModifyResult = _step1.value;
938
- return [
939
- 4,
940
- functionToModifyResult()
941
- ];
942
- case 27:
943
- _state.sent();
944
- _state.label = 28;
945
- case 28:
946
- _iteratorNormalCompletion1 = true;
947
- return [
948
- 3,
949
- 26
950
- ];
951
- case 29:
952
- return [
953
- 3,
954
- 32
955
- ];
956
- case 30:
957
- err = _state.sent();
958
- _didIteratorError1 = true;
959
- _iteratorError1 = err;
960
- return [
961
- 3,
962
- 32
963
- ];
964
- case 31:
965
- try {
966
- if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
967
- _iterator1.return();
968
- }
969
- } finally{
970
- if (_didIteratorError1) {
971
- throw _iteratorError1;
972
- }
973
- }
974
- return [
975
- 7
976
- ];
977
- case 32:
978
- return [
979
- 2,
980
- parseResult
981
- ];
982
- }
983
- });
984
- })();
985
- }
986
- },
987
- {
988
- /**
989
- * This let's you refine the schema with custom validations. This is useful when you want to validate something
990
- * that is not supported by default by the schema adapter.
991
- *
992
- * @example
993
- * ```typescript
994
- * import * as p from '@palmares/schemas';
995
- *
996
- * const numberSchema = p.number().refine((value) => {
997
- * if (value < 0) return { code: 'invalid_number', message: 'The number should be greater than 0' };
998
- * });
999
- *
1000
- * const { errors, parsed } = await numberSchema.parse(-1);
1001
- *
1002
- * console.log(errors);
1003
- * // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
1004
- * ```
1005
- *
1006
- * @param refinementCallback - The callback that will be called to validate the value.
1007
- * @param options - Options for the refinement.
1008
- * @param options.isAsync - Whether the callback is async or not. Defaults to true.
1009
- */ key: "refine",
1010
- value: function refine(refinementCallback) {
1011
- this.__refinements.push(refinementCallback);
1012
- return this;
1013
- }
1014
- },
1015
- {
1016
- /**
1017
- * Allows the value to be either undefined or null.
1018
- *
1019
- * @example
1020
- * ```typescript
1021
- * import * as p from '@palmares/schemas';
1022
- *
1023
- * const numberSchema = p.number().optional();
1024
- *
1025
- * const { errors, parsed } = await numberSchema.parse(undefined);
1026
- *
1027
- * console.log(parsed); // undefined
1028
- *
1029
- * const { errors, parsed } = await numberSchema.parse(null);
1030
- *
1031
- * console.log(parsed); // null
1032
- *
1033
- * const { errors, parsed } = await numberSchema.parse(1);
1034
- *
1035
- * console.log(parsed); // 1
1036
- * ```
1037
- *
1038
- * @returns - The schema we are working with.
1039
- */ key: "optional",
1040
- value: function optional(options) {
1041
- this.__optional = {
1042
- message: typeof (options === null || options === void 0 ? void 0 : options.message) === 'string' ? options.message : 'Required',
1043
- allow: typeof (options === null || options === void 0 ? void 0 : options.allow) === 'boolean' ? options.allow : true
1044
- };
1045
- return this;
1046
- }
1047
- },
1048
- {
1049
- /**
1050
- * Allows the value to be null and ONLY null. You can also use this function to set a custom message when
1051
- * the value is NULL by setting the { message: 'Your custom message', allow: false } on the options.
1052
- *
1053
- * @example
1054
- * ```typescript
1055
- * import * as p from '@palmares/schemas';
1056
- *
1057
- * const numberSchema = p.number().nullable();
1058
- *
1059
- * const { errors, parsed } = await numberSchema.parse(null);
1060
- *
1061
- * console.log(parsed); // null
1062
- *
1063
- * const { errors, parsed } = await numberSchema.parse(undefined);
1064
- *
1065
- * console.log(errors); // [{ isValid: false, code: 'invalid_type', message: 'Invalid type', path: [] }]
1066
- * ```
1067
- *
1068
- * @param options - The options for the nullable function.
1069
- * @param options.message - The message to be shown when the value is not null. Defaults to 'Cannot be null'.
1070
- * @param options.allow - Whether the value can be null or not. Defaults to true.
1071
- *
1072
- * @returns The schema.
1073
- */ key: "nullable",
1074
- value: function nullable(options) {
1075
- this.__nullable = {
1076
- message: typeof (options === null || options === void 0 ? void 0 : options.message) === 'string' ? options.message : 'Cannot be null',
1077
- allow: typeof (options === null || options === void 0 ? void 0 : options.allow) === 'boolean' ? options.allow : true
1078
- };
1079
- return this;
1080
- }
1081
- },
1082
- {
1083
- /**
1084
- * Appends a custom schema to the schema, this way it will bypass the creation of the schema in runtime.
1085
- *
1086
- * By default when validating, on the first validation we create the schema. Just during the first validation.
1087
- * With this function, you bypass that, so you can speed up the validation process.
1088
- *
1089
- * @example
1090
- * ```typescript
1091
- * import * as p from '@palmares/schemas';
1092
- * import * as z from 'zod';
1093
- *
1094
- * const numberSchema = p.number().appendSchema(z.number());
1095
- *
1096
- * const { errors, parsed } = await numberSchema.parse(1);
1097
- * ```
1098
- *
1099
- * @param schema - The schema to be appended.
1100
- * @param args - The arguments for the schema.
1101
- * @param args.adapter - The adapter to be used. If not provided, the default adapter will be used.
1102
- *
1103
- * @returns The same schema again.
1104
- */ key: "appendSchema",
1105
- value: function appendSchema(schema, args) {
1106
- var adapter = (args === null || args === void 0 ? void 0 : args.adapter) || (0, _conf.getDefaultAdapter)();
1107
- this.__transformedSchemas[adapter.constructor.name] = {
1108
- transformed: true,
1109
- adapter: adapter,
1110
- schemas: [
1111
- schema
1112
- ]
1113
- };
1114
- return this;
1115
- }
1116
- },
1117
- {
1118
- /**
1119
- * This method will remove the value from the representation of the schema. If the value is undefined it will keep
1120
- * that way otherwise it will set the value to undefined after it's validated.
1121
- * This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
1122
- * function. This will remove the value from the representation of the schema.
1123
- *
1124
- * By default, the value will be removed just from the representation, in other words, when you call the {@link data}
1125
- * function. But if you want to remove the value from the internal representation, you can pass the argument
1126
- * `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
1127
- * the argument `toRepresentation` as true as well.
1128
- *
1129
- * @example
1130
- * ```typescript
1131
- * import * as p from '@palmares/schemas';
1132
- *
1133
- * const userSchema = p.object({
1134
- * id: p.number().optional(),
1135
- * name: p.string(),
1136
- * password: p.string().omit()
1137
- * });
1138
- *
1139
- * const user = await userSchema.data({
1140
- * id: 1,
1141
- * name: 'John Doe',
1142
- * password: '123456'
1143
- * });
1144
- *
1145
- * console.log(user); // { id: 1, name: 'John Doe' }
1146
- * ```
1147
- *
1148
- *
1149
- * @param args - By default, the value will be removed just from the representation, in other words, when you call
1150
- * the {@link data} function.
1151
- * But if you want to remove the value from the internal representation, you can pass the argument `toInternal`
1152
- * as true. Then if you still want to remove the value from the representation, you will need to pass the
1153
- * argument `toRepresentation` as true as well.
1154
- *
1155
- * @returns The schema.
1156
- */ key: "omit",
1157
- value: function omit(args) {
1158
- // To representation is true by default, unless to internal is true.
1159
- var toRepresentation = typeof (args === null || args === void 0 ? void 0 : args.toRepresentation) === 'boolean' ? args.toRepresentation : typeof (args === null || args === void 0 ? void 0 : args.toInternal) !== 'boolean';
1160
- var toInternal = typeof (args === null || args === void 0 ? void 0 : args.toInternal) === 'boolean' ? args.toInternal : false;
1161
- if (toInternal) {
1162
- if (this.__toInternal) {
1163
- var toInternal1 = this.__toInternal;
1164
- this.__toInternal = function() {
1165
- var _ref = _async_to_generator(function(value) {
1166
- return _ts_generator(this, function(_state) {
1167
- switch(_state.label){
1168
- case 0:
1169
- return [
1170
- 4,
1171
- toInternal1(value)
1172
- ];
1173
- case 1:
1174
- _state.sent();
1175
- return [
1176
- 2,
1177
- undefined
1178
- ];
1179
- }
1180
- });
1181
- });
1182
- return function(value) {
1183
- return _ref.apply(this, arguments);
1184
- };
1185
- }();
1186
- // eslint-disable-next-line ts/require-await
1187
- } else this.__toInternal = /*#__PURE__*/ _async_to_generator(function() {
1188
- return _ts_generator(this, function(_state) {
1189
- return [
1190
- 2,
1191
- undefined
1192
- ];
1193
- });
1194
- });
1195
- } else if (toRepresentation) {
1196
- if (this.__toRepresentation) {
1197
- var toRepresentation1 = this.__toRepresentation;
1198
- this.__toRepresentation = function() {
1199
- var _ref = _async_to_generator(function(value) {
1200
- return _ts_generator(this, function(_state) {
1201
- switch(_state.label){
1202
- case 0:
1203
- return [
1204
- 4,
1205
- toRepresentation1(value)
1206
- ];
1207
- case 1:
1208
- _state.sent();
1209
- return [
1210
- 2,
1211
- undefined
1212
- ];
1213
- }
1214
- });
1215
- });
1216
- return function(value) {
1217
- return _ref.apply(this, arguments);
1218
- };
1219
- }();
1220
- // eslint-disable-next-line ts/require-await
1221
- } else this.__toRepresentation = /*#__PURE__*/ _async_to_generator(function() {
1222
- return _ts_generator(this, function(_state) {
1223
- return [
1224
- 2,
1225
- undefined
1226
- ];
1227
- });
1228
- });
1229
- }
1230
- return this;
1231
- }
1232
- },
1233
- {
1234
- /**
1235
- * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
1236
- * source like a database. You should always return the schema after you save the value, that way we will always have
1237
- * the correct type of the schema after the save operation.
1238
- *
1239
- * You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
1240
- *
1241
- * @example
1242
- * ```typescript
1243
- * import * as p from '@palmares/schemas';
1244
- *
1245
- * import { User } from './models';
1246
- *
1247
- * const userSchema = p.object({
1248
- * id: p.number().optional(),
1249
- * name: p.string(),
1250
- * email: p.string().email(),
1251
- * }).onSave(async (value) => {
1252
- * // Create or update the user on the database using palmares models or any other library of your choice.
1253
- * if (value.id)
1254
- * await User.default.set(value, { search: { id: value.id } });
1255
- * else
1256
- * await User.default.set(value);
1257
- *
1258
- * return value;
1259
- * });
1260
- *
1261
- *
1262
- * // Then, on your controller, do something like this:
1263
- * const { isValid, save, errors } = await userSchema.validate(req.body);
1264
- * if (isValid) {
1265
- * const savedValue = await save();
1266
- * return Response.json(savedValue, { status: 201 });
1267
- * }
1268
- *
1269
- * return Response.json({ errors }, { status: 400 });
1270
- * ```
1271
- *
1272
- * @param callback - The callback that will be called to save the value on an external source.
1273
- *
1274
- * @returns The schema.
1275
- */ key: "onSave",
1276
- value: function onSave(callback) {
1277
- this.__saveCallback = callback;
1278
- return this;
1279
- }
1280
- },
1281
- {
1282
- key: "validate",
1283
- value: /**
1284
- * This function is used to validate the schema and save the value to the database. It is used in
1285
- * conjunction with the {@link onSave} function.
1286
- *
1287
- * Different from other validation libraries, palmares schemas is aware that you want to save. On your
1288
- * routes/functions we recommend to ALWAYS use this function instead of {@link parse} directly. This is because
1289
- * this function by default will return an object with the property `save` or the `errors`. If the errors are present,
1290
- * you can return the errors to the user. If the save property is present, you can use to save the value to an
1291
- * external source. e.g. a database.
1292
- *
1293
- * @example
1294
- * ```typescript
1295
- * import * as p from '@palmares/schemas';
1296
- *
1297
- * import { User } from './models';
1298
- *
1299
- * const userSchema = p.object({
1300
- * id: p.number().optional(),
1301
- * name: p.string(),
1302
- * email: p.string().email(),
1303
- * }).onSave(async (value) => {
1304
- * // Create or update the user on the database using palmares models or any other library of your choice.
1305
- * if (value.id)
1306
- * await User.default.set(value, { search: { id: value.id } });
1307
- * else
1308
- * await User.default.set(value);
1309
- *
1310
- * return value;
1311
- * });
1312
- *
1313
- *
1314
- * // Then, on your controller, do something like this:
1315
- * const { isValid, save, errors } = await userSchema.validate(req.body);
1316
- * if (isValid) {
1317
- * const savedValue = await save();
1318
- * return Response.json(savedValue, { status: 201 });
1319
- * }
1320
- *
1321
- * return Response.json({ errors }, { status: 400 });
1322
- * ```
1323
- *
1324
- * @param value - The value to be validated.
1325
- *
1326
- * @returns An object with the property isValid, if the value is valid, the function `save` will be present.
1327
- * If the value is invalid, the property errors will be present.
1328
- */ function validate(value, context) {
1329
- var _this = this;
1330
- return _async_to_generator(function() {
1331
- var _ref, errors, parsed;
1332
- return _ts_generator(this, function(_state) {
1333
- switch(_state.label){
1334
- case 0:
1335
- return [
1336
- 4,
1337
- _this.__parse(value, [], {
1338
- context: context
1339
- })
1340
- ];
1341
- case 1:
1342
- _ref = _state.sent(), errors = _ref.errors, parsed = _ref.parsed;
1343
- // eslint-disable-next-line ts/no-unnecessary-condition
1344
- if ((errors || []).length > 0) return [
1345
- 2,
1346
- {
1347
- isValid: false,
1348
- errors: errors
1349
- }
1350
- ];
1351
- return [
1352
- 2,
1353
- {
1354
- isValid: true,
1355
- save: /*#__PURE__*/ _async_to_generator(function() {
1356
- return _ts_generator(this, function(_state) {
1357
- return [
1358
- 2,
1359
- _this._save.bind(_this)(parsed, context)
1360
- ];
1361
- });
1362
- })
1363
- }
1364
- ];
1365
- }
1366
- });
1367
- })();
1368
- }
1369
- },
1370
- {
1371
- key: "_save",
1372
- value: /**
1373
- * Internal function, when we call the {@link validate} function it's this function that gets called
1374
- * when the user uses the `save` function returned by the {@link validate} function if the value is valid.
1375
- *
1376
- * @param value - The value to be saved.
1377
- *
1378
- * @returns The value to representation.
1379
- */ function _save(value, context) {
1380
- var _this = this;
1381
- return _async_to_generator(function() {
1382
- var result;
1383
- return _ts_generator(this, function(_state) {
1384
- switch(_state.label){
1385
- case 0:
1386
- if (!_this.__saveCallback) return [
1387
- 3,
1388
- 2
1389
- ];
1390
- return [
1391
- 4,
1392
- _this.__saveCallback(value, context)
1393
- ];
1394
- case 1:
1395
- result = _state.sent();
1396
- return [
1397
- 2,
1398
- _this.data(result)
1399
- ];
1400
- case 2:
1401
- return [
1402
- 2,
1403
- _this.data(value)
1404
- ];
1405
- }
1406
- });
1407
- })();
1408
- }
1409
- },
1410
- {
1411
- key: "parse",
1412
- value: /**
1413
- * This function is used to validate and parse the value to the internal representation of the schema.
1414
- *
1415
- * @example
1416
- * ```typescript
1417
- * import * as p from '@palmares/schemas';
1418
- *
1419
- * const numberSchema = p.number().allowString();
1420
- *
1421
- * const { errors, parsed } = await numberSchema.parse('123');
1422
- *
1423
- * console.log(parsed); // 123
1424
- * ```
1425
- *
1426
- * @param value - The value to be parsed.
1427
- *
1428
- * @returns The parsed value.
1429
- */ function parse(value) {
1430
- var _this = this;
1431
- return _async_to_generator(function() {
1432
- return _ts_generator(this, function(_state) {
1433
- return [
1434
- 2,
1435
- _this.__parse(value, [], {})
1436
- ];
1437
- });
1438
- })();
1439
- }
1440
- },
1441
- {
1442
- key: "data",
1443
- value: /**
1444
- * This function is used to transform the value to the representation without validating it.
1445
- * This is useful when you want to return a data from a query directly to the user. But for example
1446
- * you are returning the data of a user, you can clean the password or any other sensitive data.
1447
- *
1448
- * @example
1449
- * ```typescript
1450
- * import * as p from '@palmares/schemas';
1451
- *
1452
- * const userSchema = p.object({
1453
- * id: p.number().optional(),
1454
- * name: p.string(),
1455
- * email: p.string().email(),
1456
- * password: p.string().optional()
1457
- * }).toRepresentation(async (value) => {
1458
- * return {
1459
- * id: value.id,
1460
- * name: value.name,
1461
- * email: value.email
1462
- * }
1463
- * });
1464
- *
1465
- * const user = await userSchema.data({
1466
- * id: 1,
1467
- * name: 'John Doe',
1468
- * email: 'john@gmail.com',
1469
- * password: '123456'
1470
- * });
1471
- * ```
1472
- */ function data(value) {
1473
- var _this = this;
1474
- return _async_to_generator(function() {
1475
- return _ts_generator(this, function(_state) {
1476
- switch(_state.label){
1477
- case 0:
1478
- _this.__getDefaultTransformedSchemas();
1479
- if (!(typeof _this.__runBeforeParseAndData === 'function')) return [
1480
- 3,
1481
- 2
1482
- ];
1483
- return [
1484
- 4,
1485
- _this.__runBeforeParseAndData(_this)
1486
- ];
1487
- case 1:
1488
- _state.sent();
1489
- _state.label = 2;
1490
- case 2:
1491
- return [
1492
- 4,
1493
- _this.__parsersToTransformValue(value)
1494
- ];
1495
- case 3:
1496
- value = _state.sent();
1497
- if (!_this.__toRepresentation) return [
1498
- 3,
1499
- 5
1500
- ];
1501
- return [
1502
- 4,
1503
- Promise.resolve(_this.__toRepresentation(value))
1504
- ];
1505
- case 4:
1506
- value = _state.sent();
1507
- _state.label = 5;
1508
- case 5:
1509
- if (!(_this.__defaultFunction && value === undefined)) return [
1510
- 3,
1511
- 7
1512
- ];
1513
- return [
1514
- 4,
1515
- Promise.resolve(_this.__defaultFunction())
1516
- ];
1517
- case 6:
1518
- value = _state.sent();
1519
- _state.label = 7;
1520
- case 7:
1521
- return [
1522
- 2,
1523
- value
1524
- ];
1525
- }
1526
- });
1527
- })();
1528
- }
1529
- },
1530
- {
1531
- key: "instanceOf",
1532
- value: function instanceOf(args) {
1533
- this.__type.check = typeof args.check === 'function' ? args.check : this.__type.check;
1534
- this.__type.message = typeof args.message === 'string' ? args.message : this.__type.message;
1535
- return this;
1536
- }
1537
- },
1538
- {
1539
- /**
1540
- * This function is used to add a default value to the schema. If the value is either undefined or null,
1541
- * the default value will be used.
1542
- *
1543
- * @example
1544
- * ```typescript
1545
- * import * as p from '@palmares/schemas';
1546
- *
1547
- * const numberSchema = p.number().default(0);
1548
- *
1549
- * const { errors, parsed } = await numberSchema.parse(undefined);
1550
- *
1551
- * console.log(parsed); // 0
1552
- * ```
1553
- */ key: "default",
1554
- value: function _default(defaultValueOrFunction) {
1555
- var isFunction = typeof defaultValueOrFunction === 'function';
1556
- if (isFunction) this.__defaultFunction = defaultValueOrFunction;
1557
- else this.__defaultFunction = /*#__PURE__*/ _async_to_generator(function() {
1558
- return _ts_generator(this, function(_state) {
1559
- return [
1560
- 2,
1561
- defaultValueOrFunction
1562
- ];
1563
- });
1564
- });
1565
- return this;
1566
- }
1567
- },
1568
- {
1569
- /**
1570
- * This function let's you customize the schema your own way. After we translate the schema on the adapter we call
1571
- * this function to let you customize the custom schema your own way. Our API does not support passthrough?
1572
- * No problem, you can use this function to customize the zod schema.
1573
- *
1574
- * @example
1575
- * ```typescript
1576
- * import * as p from '@palmares/schemas';
1577
- *
1578
- * const numberSchema = p.number().extends((schema) => {
1579
- * return schema.nonnegative();
1580
- * });
1581
- *
1582
- * const { errors, parsed } = await numberSchema.parse(-1);
1583
- *
1584
- * console.log(errors);
1585
- * // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
1586
- * ```
1587
- *
1588
- * @param callback - The callback that will be called to customize the schema.
1589
- * @param toStringCallback - The callback that will be called to transform the schema to a string when you want
1590
- * to compile the underlying schema to a string so you can save it for future runs.
1591
- *
1592
- * @returns The schema.
1593
- */ key: "extends",
1594
- value: function _extends(callback, toStringCallback) {
1595
- this.__extends = {
1596
- callback: callback,
1597
- toStringCallback: toStringCallback
1598
- };
1599
- return this;
1600
- }
1601
- },
1602
- {
1603
- /**
1604
- * This function is used to transform the value to the representation of the schema. When using the {@link data}
1605
- * function. With this function you have full control to add data cleaning for example, transforming the data
1606
- * and whatever. Another use case is when you want to return deeply nested recursive data.
1607
- * The schema maps to itself.
1608
- *
1609
- * @example
1610
- * ```typescript
1611
- * import * as p from '@palmares/schemas';
1612
- *
1613
- * const recursiveSchema = p.object({
1614
- * id: p.number().optional(),
1615
- * name: p.string(),
1616
- * }).toRepresentation(async (value) => {
1617
- * return {
1618
- * id: value.id,
1619
- * name: value.name,
1620
- * children: await Promise.all(value.children.map(async (child) => await recursiveSchema.data(child)))
1621
- * }
1622
- * });
1623
- *
1624
- * const data = await recursiveSchema.data({
1625
- * id: 1,
1626
- * name: 'John Doe',
1627
- * });
1628
- * ```
1629
- *
1630
- * @example
1631
- * ```
1632
- * import * as p from '@palmares/schemas';
1633
- *
1634
- * const colorToRGBSchema = p.string().toRepresentation(async (value) => {
1635
- * switch (value) {
1636
- * case 'red': return { r: 255, g: 0, b: 0 };
1637
- * case 'green': return { r: 0, g: 255, b: 0 };
1638
- * case 'blue': return { r: 0, g: 0, b: 255 };
1639
- * default: return { r: 0, g: 0, b: 0 };
1640
- * }
1641
- * });
1642
- * ```
1643
- * @param toRepresentationCallback - The callback that will be called to transform the value to the representation.
1644
- * @param options - Options for the toRepresentation function.
1645
- * @param options.after - Whether the toRepresentationCallback should be called after the existing
1646
- * toRepresentationCallback. Defaults to true.
1647
- * @param options.before - Whether the toRepresentationCallback should be called before the existing
1648
- * toRepresentationCallback. Defaults to true.
1649
- *
1650
- * @returns The schema with a new return type
1651
- */ key: "toRepresentation",
1652
- value: function toRepresentation(toRepresentationCallback, options) {
1653
- if (this.__toRepresentation) {
1654
- var before = typeof (options === null || options === void 0 ? void 0 : options.before) === 'boolean' ? options.before : typeof (options === null || options === void 0 ? void 0 : options.after) === 'boolean' ? !options.after : true;
1655
- var existingToRepresentation = this.__toRepresentation;
1656
- this.__toRepresentation = function() {
1657
- var _ref = _async_to_generator(function(value) {
1658
- return _ts_generator(this, function(_state) {
1659
- switch(_state.label){
1660
- case 0:
1661
- if (!before) return [
1662
- 3,
1663
- 2
1664
- ];
1665
- return [
1666
- 4,
1667
- existingToRepresentation(value)
1668
- ];
1669
- case 1:
1670
- return [
1671
- 2,
1672
- toRepresentationCallback.apply(void 0, [
1673
- _state.sent()
1674
- ])
1675
- ];
1676
- case 2:
1677
- return [
1678
- 4,
1679
- toRepresentationCallback(value)
1680
- ];
1681
- case 3:
1682
- return [
1683
- 2,
1684
- existingToRepresentation.apply(void 0, [
1685
- _state.sent()
1686
- ])
1687
- ];
1688
- case 4:
1689
- return [
1690
- 2
1691
- ];
1692
- }
1693
- });
1694
- });
1695
- return function(value) {
1696
- return _ref.apply(this, arguments);
1697
- };
1698
- }();
1699
- } else this.__toRepresentation = toRepresentationCallback;
1700
- return this;
1701
- }
1702
- },
1703
- {
1704
- /**
1705
- * This function is used to transform the value to the internal representation of the schema. This is useful
1706
- * when you want to transform the value to a type that the schema adapter can understand. For example, you
1707
- * might want to transform a string to a date. This is the function you use.
1708
- *
1709
- * @example
1710
- * ```typescript
1711
- * import * as p from '@palmares/schemas';
1712
- *
1713
- * const dateSchema = p.string().toInternal((value) => {
1714
- * return new Date(value);
1715
- * });
1716
- *
1717
- * const date = await dateSchema.parse('2021-01-01');
1718
- *
1719
- * console.log(date); // Date object
1720
- *
1721
- * const rgbToColorSchema = p.object({
1722
- * r: p.number().min(0).max(255),
1723
- * g: p.number().min(0).max(255),
1724
- * b: p.number().min(0).max(255),
1725
- * }).toInternal(async (value) => {
1726
- * if (value.r === 255 && value.g === 0 && value.b === 0) return 'red';
1727
- * if (value.r === 0 && value.g === 255 && value.b === 0) return 'green';
1728
- * if (value.r === 0 && value.g === 0 && value.b === 255) return 'blue';
1729
- * return `rgb(${value.r}, ${value.g}, ${value.b})`;
1730
- * });
1731
- * ```
1732
- *
1733
- * @param toInternalCallback - The callback that will be called to transform the value to the internal representation.
1734
- *
1735
- * @returns The schema with a new return type.
1736
- */ key: "toInternal",
1737
- value: function toInternal(toInternalCallback) {
1738
- if (this.__toInternal) {
1739
- var toInternal = this.__toInternal;
1740
- this.__toInternal = function() {
1741
- var _ref = _async_to_generator(function(value) {
1742
- var newValue;
1743
- return _ts_generator(this, function(_state) {
1744
- switch(_state.label){
1745
- case 0:
1746
- return [
1747
- 4,
1748
- toInternal(value)
1749
- ];
1750
- case 1:
1751
- newValue = _state.sent();
1752
- return [
1753
- 2,
1754
- toInternalCallback(newValue)
1755
- ];
1756
- }
1757
- });
1758
- });
1759
- return function(value) {
1760
- return _ref.apply(this, arguments);
1761
- };
1762
- }();
1763
- } else this.__toInternal = toInternalCallback;
1764
- return this;
1765
- }
1766
- },
1767
- {
1768
- /**
1769
- * Called before the validation of the schema. Let's say that you want to validate a date that might receive a
1770
- * string, you can convert that string to a date here BEFORE the validation. This pretty much transforms the value
1771
- * to a type that the schema adapter can understand.
1772
- *
1773
- * @example
1774
- * ```
1775
- * import * as p from '@palmares/schemas';
1776
- * import * as z from 'zod';
1777
- *
1778
- * const customRecordToMapSchema = p.schema().appendSchema(z.map()).toValidate(async (value) => {
1779
- * return new Map(value); // Before validating we transform the value to a map.
1780
- * });
1781
- *
1782
- * const { errors, parsed } = await customRecordToMapSchema.parse({ key: 'value' });
1783
- * ```
1784
- *
1785
- * @param toValidateCallback - The callback that will be called to validate the value.
1786
- *
1787
- * @returns The schema with a new return type.
1788
- */ key: "toValidate",
1789
- value: function toValidate(toValidateCallback) {
1790
- this.__toValidate = toValidateCallback;
1791
- return this;
1792
- }
1793
- },
1794
- {
1795
- key: "compile",
1796
- value: /**
1797
- * Used to transform the given schema on a stringfied version of the adapter.
1798
- */ function compile(adapter) {
1799
- var _this = this;
1800
- return _async_to_generator(function() {
1801
- var data, stringVersions;
1802
- return _ts_generator(this, function(_state) {
1803
- switch(_state.label){
1804
- case 0:
1805
- return [
1806
- 4,
1807
- _this.__transformToAdapter({
1808
- shouldAddStringVersion: true,
1809
- force: true
1810
- })
1811
- ];
1812
- case 1:
1813
- data = _state.sent();
1814
- stringVersions = data.map(function(value) {
1815
- return value.asString;
1816
- });
1817
- return [
1818
- 2,
1819
- stringVersions
1820
- ];
1821
- }
1822
- });
1823
- })();
1824
- }
1825
- }
1826
- ], [
1827
- {
1828
- key: "new",
1829
- value: function _new() {
1830
- for(var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++){
1831
- _args[_key] = arguments[_key];
1832
- }
1833
- var result = new Schema();
1834
- return result;
1835
- }
1836
- }
1837
- ]);
1838
- return Schema;
1839
- }();
1840
- var schema = Schema.new;