@palmares/schemas 0.0.1

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