@palmares/schemas 0.1.25 → 0.2.0

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 +26 -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 +48 -0
  45. package/dist/{cjs/types → src}/exceptions.d.ts +0 -3
  46. package/dist/src/exceptions.d.ts.map +1 -0
  47. package/dist/src/exceptions.js +24 -0
  48. package/dist/src/index.cjs +6509 -0
  49. package/dist/src/index.d.ts.map +1 -0
  50. package/dist/src/index.js +6445 -0
  51. package/dist/src/middleware.cjs +58 -0
  52. package/dist/src/middleware.d.ts.map +1 -0
  53. package/dist/src/middleware.js +35 -0
  54. package/dist/src/model.cjs +5992 -0
  55. package/dist/src/model.d.ts.map +1 -0
  56. package/dist/src/model.js +5967 -0
  57. package/dist/src/parsers/convert-from-number.cjs +34 -0
  58. package/dist/src/parsers/convert-from-number.d.ts.map +1 -0
  59. package/dist/src/parsers/convert-from-number.js +11 -0
  60. package/dist/src/parsers/convert-from-string.cjs +40 -0
  61. package/dist/src/parsers/convert-from-string.d.ts.map +1 -0
  62. package/dist/src/parsers/convert-from-string.js +17 -0
  63. package/dist/src/parsers/index.cjs +50 -0
  64. package/dist/src/parsers/index.d.ts.map +1 -0
  65. package/dist/src/parsers/index.js +24 -0
  66. package/dist/src/schema/array.cjs +1863 -0
  67. package/dist/src/schema/array.d.ts.map +1 -0
  68. package/dist/src/schema/array.js +1837 -0
  69. package/dist/src/schema/boolean.cjs +1800 -0
  70. package/dist/src/schema/boolean.d.ts.map +1 -0
  71. package/dist/src/schema/boolean.js +1774 -0
  72. package/dist/src/schema/datetime.cjs +1789 -0
  73. package/dist/src/schema/datetime.d.ts.map +1 -0
  74. package/dist/src/schema/datetime.js +1763 -0
  75. package/dist/src/schema/index.cjs +2026 -0
  76. package/dist/src/schema/index.d.ts.map +1 -0
  77. package/dist/src/schema/index.js +2001 -0
  78. package/dist/src/schema/number.cjs +2026 -0
  79. package/dist/src/schema/number.d.ts.map +1 -0
  80. package/dist/src/schema/number.js +2000 -0
  81. package/dist/src/schema/object.cjs +1795 -0
  82. package/dist/src/schema/object.d.ts.map +1 -0
  83. package/dist/src/schema/object.js +1769 -0
  84. package/dist/src/schema/schema.cjs +844 -0
  85. package/dist/src/schema/schema.d.ts.map +1 -0
  86. package/dist/src/schema/schema.js +818 -0
  87. package/dist/src/schema/string.cjs +2126 -0
  88. package/dist/src/schema/string.d.ts.map +1 -0
  89. package/dist/src/schema/string.js +2100 -0
  90. package/dist/src/schema/types.cjs +18 -0
  91. package/dist/src/schema/types.d.ts.map +1 -0
  92. package/dist/src/schema/types.js +0 -0
  93. package/dist/src/schema/union.cjs +1737 -0
  94. package/dist/src/schema/union.d.ts.map +1 -0
  95. package/dist/src/schema/union.js +1711 -0
  96. package/dist/src/types.cjs +18 -0
  97. package/dist/src/types.d.ts.map +1 -0
  98. package/dist/src/types.js +0 -0
  99. package/dist/src/utils.cjs +477 -0
  100. package/dist/src/utils.d.ts.map +1 -0
  101. package/dist/src/utils.js +445 -0
  102. package/dist/src/validators/array.cjs +176 -0
  103. package/dist/src/validators/array.d.ts.map +1 -0
  104. package/dist/src/validators/array.js +150 -0
  105. package/dist/src/validators/boolean.cjs +71 -0
  106. package/dist/src/validators/boolean.d.ts.map +1 -0
  107. package/dist/src/validators/boolean.js +47 -0
  108. package/dist/src/validators/datetime.cjs +129 -0
  109. package/dist/src/validators/datetime.d.ts.map +1 -0
  110. package/dist/src/validators/datetime.js +103 -0
  111. package/dist/src/validators/number.cjs +194 -0
  112. package/dist/src/validators/number.d.ts.map +1 -0
  113. package/dist/src/validators/number.js +166 -0
  114. package/dist/src/validators/object.cjs +69 -0
  115. package/dist/src/validators/object.d.ts.map +1 -0
  116. package/dist/src/validators/object.js +46 -0
  117. package/dist/src/validators/schema.cjs +158 -0
  118. package/dist/src/validators/schema.d.ts.map +1 -0
  119. package/dist/src/validators/schema.js +132 -0
  120. package/dist/src/validators/string.cjs +255 -0
  121. package/dist/src/validators/string.d.ts.map +1 -0
  122. package/dist/src/validators/string.js +224 -0
  123. package/dist/src/validators/types.cjs +18 -0
  124. package/dist/src/validators/types.d.ts.map +1 -0
  125. package/dist/src/validators/types.js +0 -0
  126. package/dist/src/validators/union.cjs +69 -0
  127. package/dist/src/validators/union.d.ts.map +1 -0
  128. package/dist/src/validators/union.js +46 -0
  129. package/dist/src/validators/utils.cjs +165 -0
  130. package/dist/src/validators/utils.d.ts.map +1 -0
  131. package/dist/src/validators/utils.js +142 -0
  132. package/dist/tsconfig.types.tsbuildinfo +1 -0
  133. package/package.json +15 -23
  134. package/dist/cjs/src/adapter/fields/array.js +0 -152
  135. package/dist/cjs/src/adapter/fields/boolean.js +0 -162
  136. package/dist/cjs/src/adapter/fields/datetime.js +0 -162
  137. package/dist/cjs/src/adapter/fields/index.js +0 -311
  138. package/dist/cjs/src/adapter/fields/number.js +0 -162
  139. package/dist/cjs/src/adapter/fields/object.js +0 -171
  140. package/dist/cjs/src/adapter/fields/string.js +0 -162
  141. package/dist/cjs/src/adapter/fields/union.js +0 -162
  142. package/dist/cjs/src/adapter/index.js +0 -201
  143. package/dist/cjs/src/adapter/types.js +0 -4
  144. package/dist/cjs/src/compile.js +0 -262
  145. package/dist/cjs/src/conf.js +0 -26
  146. package/dist/cjs/src/constants.js +0 -36
  147. package/dist/cjs/src/domain.js +0 -156
  148. package/dist/cjs/src/exceptions.js +0 -168
  149. package/dist/cjs/src/index.js +0 -317
  150. package/dist/cjs/src/middleware.js +0 -213
  151. package/dist/cjs/src/model.js +0 -628
  152. package/dist/cjs/src/parsers/convert-from-number.js +0 -15
  153. package/dist/cjs/src/parsers/convert-from-string.js +0 -19
  154. package/dist/cjs/src/parsers/index.js +0 -20
  155. package/dist/cjs/src/schema/array.js +0 -1014
  156. package/dist/cjs/src/schema/boolean.js +0 -883
  157. package/dist/cjs/src/schema/datetime.js +0 -840
  158. package/dist/cjs/src/schema/index.js +0 -12
  159. package/dist/cjs/src/schema/number.js +0 -976
  160. package/dist/cjs/src/schema/object.js +0 -1052
  161. package/dist/cjs/src/schema/schema.js +0 -1840
  162. package/dist/cjs/src/schema/string.js +0 -1038
  163. package/dist/cjs/src/schema/types.js +0 -4
  164. package/dist/cjs/src/schema/union.js +0 -1070
  165. package/dist/cjs/src/types.js +0 -4
  166. package/dist/cjs/src/utils.js +0 -647
  167. package/dist/cjs/src/validators/array.js +0 -462
  168. package/dist/cjs/src/validators/boolean.js +0 -201
  169. package/dist/cjs/src/validators/datetime.js +0 -291
  170. package/dist/cjs/src/validators/number.js +0 -375
  171. package/dist/cjs/src/validators/object.js +0 -291
  172. package/dist/cjs/src/validators/schema.js +0 -322
  173. package/dist/cjs/src/validators/string.js +0 -467
  174. package/dist/cjs/src/validators/types.js +0 -4
  175. package/dist/cjs/src/validators/union.js +0 -233
  176. package/dist/cjs/src/validators/utils.js +0 -499
  177. package/dist/cjs/tsconfig.types.tsbuildinfo +0 -1
  178. package/dist/cjs/types/adapter/fields/array.d.ts.map +0 -1
  179. package/dist/cjs/types/adapter/fields/boolean.d.ts.map +0 -1
  180. package/dist/cjs/types/adapter/fields/datetime.d.ts.map +0 -1
  181. package/dist/cjs/types/adapter/fields/index.d.ts.map +0 -1
  182. package/dist/cjs/types/adapter/fields/number.d.ts.map +0 -1
  183. package/dist/cjs/types/adapter/fields/object.d.ts.map +0 -1
  184. package/dist/cjs/types/adapter/fields/string.d.ts.map +0 -1
  185. package/dist/cjs/types/adapter/fields/union.d.ts.map +0 -1
  186. package/dist/cjs/types/adapter/index.d.ts.map +0 -1
  187. package/dist/cjs/types/adapter/types.d.ts.map +0 -1
  188. package/dist/cjs/types/compile.d.ts.map +0 -1
  189. package/dist/cjs/types/conf.d.ts.map +0 -1
  190. package/dist/cjs/types/constants.d.ts.map +0 -1
  191. package/dist/cjs/types/domain.d.ts.map +0 -1
  192. package/dist/cjs/types/exceptions.d.ts.map +0 -1
  193. package/dist/cjs/types/index.d.ts.map +0 -1
  194. package/dist/cjs/types/middleware.d.ts.map +0 -1
  195. package/dist/cjs/types/model.d.ts.map +0 -1
  196. package/dist/cjs/types/parsers/convert-from-number.d.ts.map +0 -1
  197. package/dist/cjs/types/parsers/convert-from-string.d.ts.map +0 -1
  198. package/dist/cjs/types/parsers/index.d.ts.map +0 -1
  199. package/dist/cjs/types/schema/array.d.ts.map +0 -1
  200. package/dist/cjs/types/schema/boolean.d.ts.map +0 -1
  201. package/dist/cjs/types/schema/datetime.d.ts.map +0 -1
  202. package/dist/cjs/types/schema/index.d.ts.map +0 -1
  203. package/dist/cjs/types/schema/number.d.ts.map +0 -1
  204. package/dist/cjs/types/schema/object.d.ts.map +0 -1
  205. package/dist/cjs/types/schema/schema.d.ts.map +0 -1
  206. package/dist/cjs/types/schema/string.d.ts.map +0 -1
  207. package/dist/cjs/types/schema/types.d.ts.map +0 -1
  208. package/dist/cjs/types/schema/union.d.ts.map +0 -1
  209. package/dist/cjs/types/types.d.ts.map +0 -1
  210. package/dist/cjs/types/utils.d.ts.map +0 -1
  211. package/dist/cjs/types/validators/array.d.ts.map +0 -1
  212. package/dist/cjs/types/validators/boolean.d.ts.map +0 -1
  213. package/dist/cjs/types/validators/datetime.d.ts.map +0 -1
  214. package/dist/cjs/types/validators/number.d.ts.map +0 -1
  215. package/dist/cjs/types/validators/object.d.ts.map +0 -1
  216. package/dist/cjs/types/validators/schema.d.ts.map +0 -1
  217. package/dist/cjs/types/validators/string.d.ts.map +0 -1
  218. package/dist/cjs/types/validators/types.d.ts.map +0 -1
  219. package/dist/cjs/types/validators/union.d.ts.map +0 -1
  220. package/dist/cjs/types/validators/utils.d.ts.map +0 -1
  221. package/dist/esm/src/adapter/fields/array.js +0 -13
  222. package/dist/esm/src/adapter/fields/boolean.js +0 -20
  223. package/dist/esm/src/adapter/fields/datetime.js +0 -20
  224. package/dist/esm/src/adapter/fields/index.js +0 -37
  225. package/dist/esm/src/adapter/fields/number.js +0 -20
  226. package/dist/esm/src/adapter/fields/object.js +0 -26
  227. package/dist/esm/src/adapter/fields/string.js +0 -20
  228. package/dist/esm/src/adapter/fields/union.js +0 -20
  229. package/dist/esm/src/adapter/index.js +0 -21
  230. package/dist/esm/src/adapter/types.js +0 -1
  231. package/dist/esm/src/compile.js +0 -10
  232. package/dist/esm/src/conf.js +0 -18
  233. package/dist/esm/src/constants.js +0 -4
  234. package/dist/esm/src/domain.js +0 -11
  235. package/dist/esm/src/exceptions.js +0 -15
  236. package/dist/esm/src/index.js +0 -126
  237. package/dist/esm/src/middleware.js +0 -29
  238. package/dist/esm/src/model.js +0 -264
  239. package/dist/esm/src/parsers/convert-from-number.js +0 -8
  240. package/dist/esm/src/parsers/convert-from-string.js +0 -14
  241. package/dist/esm/src/parsers/index.js +0 -2
  242. package/dist/esm/src/schema/array.js +0 -487
  243. package/dist/esm/src/schema/boolean.js +0 -520
  244. package/dist/esm/src/schema/datetime.js +0 -477
  245. package/dist/esm/src/schema/index.js +0 -3
  246. package/dist/esm/src/schema/number.js +0 -610
  247. package/dist/esm/src/schema/object.js +0 -521
  248. package/dist/esm/src/schema/schema.js +0 -767
  249. package/dist/esm/src/schema/string.js +0 -668
  250. package/dist/esm/src/schema/types.js +0 -1
  251. package/dist/esm/src/schema/union.js +0 -468
  252. package/dist/esm/src/types.js +0 -14
  253. package/dist/esm/src/utils.js +0 -207
  254. package/dist/esm/src/validators/array.js +0 -140
  255. package/dist/esm/src/validators/boolean.js +0 -37
  256. package/dist/esm/src/validators/datetime.js +0 -89
  257. package/dist/esm/src/validators/number.js +0 -148
  258. package/dist/esm/src/validators/object.js +0 -39
  259. package/dist/esm/src/validators/schema.js +0 -118
  260. package/dist/esm/src/validators/string.js +0 -202
  261. package/dist/esm/src/validators/types.js +0 -1
  262. package/dist/esm/src/validators/union.js +0 -39
  263. package/dist/esm/src/validators/utils.js +0 -147
  264. /package/dist/{cjs/types → src}/adapter/fields/array.d.ts +0 -0
  265. /package/dist/{cjs/types → src}/adapter/fields/boolean.d.ts +0 -0
  266. /package/dist/{cjs/types → src}/adapter/fields/datetime.d.ts +0 -0
  267. /package/dist/{cjs/types → src}/adapter/fields/index.d.ts +0 -0
  268. /package/dist/{cjs/types → src}/adapter/fields/number.d.ts +0 -0
  269. /package/dist/{cjs/types → src}/adapter/fields/object.d.ts +0 -0
  270. /package/dist/{cjs/types → src}/adapter/fields/string.d.ts +0 -0
  271. /package/dist/{cjs/types → src}/adapter/fields/union.d.ts +0 -0
  272. /package/dist/{cjs/types → src}/adapter/index.d.ts +0 -0
  273. /package/dist/{cjs/types → src}/adapter/types.d.ts +0 -0
  274. /package/dist/{cjs/types → src}/compile.d.ts +0 -0
  275. /package/dist/{cjs/types → src}/conf.d.ts +0 -0
  276. /package/dist/{cjs/types → src}/constants.d.ts +0 -0
  277. /package/dist/{cjs/types → src}/domain.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
@@ -0,0 +1,844 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/schema/schema.ts
22
+ var schema_exports = {};
23
+ __export(schema_exports, {
24
+ Schema: () => Schema,
25
+ schema: () => schema
26
+ });
27
+ module.exports = __toCommonJS(schema_exports);
28
+
29
+ // src/exceptions.ts
30
+ var NoAdapterFoundError = class extends Error {
31
+ static {
32
+ __name(this, "NoAdapterFoundError");
33
+ }
34
+ constructor() {
35
+ super("No adapter found, please define an adapter using setDefaultAdapter() before using any schema.");
36
+ }
37
+ };
38
+
39
+ // src/conf.ts
40
+ function getDefaultAdapter() {
41
+ if (!globalThis.$PSchemasAdapter) throw new NoAdapterFoundError();
42
+ return globalThis.$PSchemasAdapter;
43
+ }
44
+ __name(getDefaultAdapter, "getDefaultAdapter");
45
+
46
+ // src/validators/utils.ts
47
+ var priorityByType = {
48
+ low: 0,
49
+ medium: 1,
50
+ high: 2
51
+ };
52
+ var typeByPriority = Object.entries(priorityByType).reduce((acc, [key, value]) => {
53
+ acc[value] = key;
54
+ return acc;
55
+ }, {});
56
+
57
+ // src/utils.ts
58
+ async function formatErrorFromParseMethod(adapter, fieldAdapter, error, received, schema2, path, errorsAsHashedSet) {
59
+ const formattedError = await fieldAdapter.formatError(adapter, adapter.field, schema2, error);
60
+ formattedError.path = Array.isArray(formattedError.path) ? [
61
+ ...path,
62
+ ...formattedError.path
63
+ ] : path;
64
+ const formattedErrorAsParseResultError = formattedError;
65
+ formattedErrorAsParseResultError.isValid = false;
66
+ const sortedError = Object.fromEntries(Object.entries(formattedErrorAsParseResultError).sort(([a], [b]) => a.localeCompare(b)));
67
+ const hashedError = JSON.stringify(sortedError);
68
+ errorsAsHashedSet.add(JSON.stringify(sortedError));
69
+ formattedErrorAsParseResultError.received = received;
70
+ return formattedErrorAsParseResultError;
71
+ }
72
+ __name(formatErrorFromParseMethod, "formatErrorFromParseMethod");
73
+
74
+ // src/schema/schema.ts
75
+ var Schema = class _Schema {
76
+ static {
77
+ __name(this, "Schema");
78
+ }
79
+ $$type = "$PSchema";
80
+ fieldType = "schema";
81
+ // Those functions will assume control of the validation process on adapters, instead of the schema.
82
+ // Why this is used? The idea is that the Schema has NO idea
83
+ // that one of it's children might be an UnionSchema for example. The adapter might not support unions,
84
+ // so then we give control to the union. The parent schema will already have an array of translated
85
+ // adapter schemas. This means for a union with Number and String it'll generate two schemas, one for number
86
+ // and one for the value as String. Of course this gets multiplied. So if we have a union with Number and String.
87
+ // We should take those two schemas from the array and validate them individually. This logic is
88
+ // handled by the union schema. If we have an intersection type for example, instead of validating
89
+ // One schema OR the other, we validate one schema AND the other. This will be handled
90
+ // by the schema that contains that intersection logic.
91
+ __beforeValidationCallbacks = /* @__PURE__ */ new Map();
92
+ __cachedGetParent;
93
+ set __getParent(value) {
94
+ this.__cachedGetParent = value;
95
+ }
96
+ get __getParent() {
97
+ return this.__cachedGetParent;
98
+ }
99
+ __alreadyAppliedModel;
100
+ __runBeforeParseAndData;
101
+ __rootFallbacksValidator;
102
+ __saveCallback;
103
+ __modelOmitCallback;
104
+ __parsers = {
105
+ high: /* @__PURE__ */ new Map(),
106
+ medium: /* @__PURE__ */ new Map(),
107
+ low: /* @__PURE__ */ new Map(),
108
+ _fallbacks: /* @__PURE__ */ new Set()
109
+ };
110
+ __refinements = [];
111
+ __nullable = {
112
+ message: "Cannot be null",
113
+ allow: false
114
+ };
115
+ __optional = {
116
+ message: "Required",
117
+ allow: false
118
+ };
119
+ __extends = void 0;
120
+ __transformedSchemas = {};
121
+ __defaultFunction = void 0;
122
+ __toRepresentation = void 0;
123
+ __toValidate = void 0;
124
+ __toInternal = void 0;
125
+ __type = {
126
+ message: "Invalid type",
127
+ check: /* @__PURE__ */ __name(() => true, "check")
128
+ };
129
+ __getDefaultTransformedSchemas() {
130
+ const adapterInstance = getDefaultAdapter();
131
+ if (this.__transformedSchemas[adapterInstance.constructor.name] === void 0) this.__transformedSchemas[adapterInstance.constructor.name] = {
132
+ transformed: false,
133
+ adapter: adapterInstance,
134
+ schemas: []
135
+ };
136
+ }
137
+ /**
138
+ * This will validate the data with the fallbacks, so internally, without relaying on the schema adapter.
139
+ * This is nice because we can support things that the schema adapter is not able to support by default.
140
+ *
141
+ * @param errorsAsHashedSet - The errors as a hashed set. This is used to prevent duplicate errors.
142
+ * @param path - The path of the error.
143
+ * @param parseResult - The result of the parse method.
144
+ */
145
+ async __validateByFallbacks(path, parseResult, options) {
146
+ if (this.__rootFallbacksValidator) return this.__rootFallbacksValidator.validate(options.errorsAsHashedSet, path, parseResult, options);
147
+ return parseResult;
148
+ }
149
+ /**
150
+ * This will validate by the adapter. In other words, we send the data to the schema adapter and then we validate
151
+ * that data.
152
+ * So understand that, first we send the data to the adapter, the adapter validates it, then, after we validate
153
+ * from the adapter we validate with the fallbacks so we can do all of the extra validations not handled by
154
+ * the adapter.
155
+ *
156
+ * @param value - The value to be validated.
157
+ * @param errorsAsHashedSet - The errors as a hashed set. This is used to prevent duplicate errors on the validator.
158
+ * @param path - The path of the error so we can construct an object with the nested paths of the error.
159
+ * @param parseResult - The result of the parse method.
160
+ *
161
+ * @returns The result and the errors of the parse method.
162
+ */
163
+ async __validateByAdapter(adapter, fieldAdapter, schema2, value, path, options) {
164
+ const parseResult = {
165
+ errors: [],
166
+ parsed: value
167
+ };
168
+ parseResult.errors = [];
169
+ parseResult.parsed = value;
170
+ if (fieldAdapter === void 0 || typeof fieldAdapter.parse !== "function") return parseResult;
171
+ const adapterParseResult = await fieldAdapter.parse(adapter, adapter.field, schema2.transformed, value, options.args);
172
+ parseResult.parsed = adapterParseResult.parsed;
173
+ if (adapterParseResult.errors) {
174
+ if (Array.isArray(adapterParseResult.errors)) parseResult.errors = await Promise.all(adapterParseResult.errors.map(async (error) => formatErrorFromParseMethod(adapter, fieldAdapter, error, value, schema2.transformed, path, options.errorsAsHashedSet || /* @__PURE__ */ new Set())));
175
+ else parseResult.errors = [
176
+ await formatErrorFromParseMethod(adapter, fieldAdapter, parseResult.errors, value, schema2.transformed, path, options.errorsAsHashedSet || /* @__PURE__ */ new Set())
177
+ ];
178
+ }
179
+ parseResult.errors = parseResult.errors.filter((error) => typeof error !== "undefined");
180
+ return parseResult;
181
+ }
182
+ // eslint-disable-next-line ts/require-await
183
+ async __transformToAdapter(_options) {
184
+ throw new Error("Not implemented");
185
+ }
186
+ /** */
187
+ async __parsersToTransformValue(value, parsersToUse) {
188
+ let shouldStop = false;
189
+ for (const [parserName, parser] of this.__parsers.high.entries()) {
190
+ if (parsersToUse instanceof Set === false || parsersToUse.has(parserName)) {
191
+ const result = await Promise.resolve(parser(value));
192
+ if (result.preventNextParsers) shouldStop = true;
193
+ value = result.value;
194
+ } else continue;
195
+ }
196
+ if (shouldStop === false) {
197
+ for (const [parserName, parser] of this.__parsers.medium.entries()) {
198
+ if (parsersToUse instanceof Set === false || parsersToUse.has(parserName)) {
199
+ const result = await Promise.resolve(parser(value));
200
+ if (result.preventNextParsers) shouldStop = true;
201
+ value = result.value;
202
+ } else continue;
203
+ }
204
+ }
205
+ if (shouldStop === false) {
206
+ for (const [parserName, parser] of this.__parsers.low.entries()) {
207
+ if (parsersToUse instanceof Set === false || parsersToUse.has(parserName)) {
208
+ const result = await Promise.resolve(parser(value));
209
+ if (result.preventNextParsers) shouldStop = true;
210
+ value = result.value;
211
+ } else continue;
212
+ }
213
+ }
214
+ return value;
215
+ }
216
+ async __parse(value, path = [], options) {
217
+ this.__getDefaultTransformedSchemas();
218
+ if (typeof this.__runBeforeParseAndData === "function") await this.__runBeforeParseAndData(this);
219
+ const shouldRunToInternalToBubbleUp = options.toInternalToBubbleUp === void 0;
220
+ if (shouldRunToInternalToBubbleUp) options.toInternalToBubbleUp = [];
221
+ if (options.errorsAsHashedSet instanceof Set === false) options.errorsAsHashedSet = /* @__PURE__ */ new Set();
222
+ const shouldCallDefaultFunction = value === void 0 && typeof this.__defaultFunction === "function";
223
+ const shouldCallToValidateCallback = typeof this.__toValidate === "function";
224
+ const schemaAdapterFieldType = this.fieldType;
225
+ if (shouldCallDefaultFunction) value = await this.__defaultFunction();
226
+ if (shouldCallToValidateCallback) value = await Promise.resolve(this.__toValidate(value));
227
+ const parseResult = {
228
+ errors: [],
229
+ parsed: value
230
+ };
231
+ value = await this.__parsersToTransformValue(value, this.__parsers._fallbacks);
232
+ if (options.appendFallbacksBeforeAdapterValidation === void 0) options.appendFallbacksBeforeAdapterValidation = (schema2, name, callback) => {
233
+ if (this !== schema2) this.__beforeValidationCallbacks.set(name, callback);
234
+ };
235
+ if (this.__transformedSchemas[options.schemaAdapter?.constructor.name || getDefaultAdapter().constructor.name].transformed === false) await this.__transformToAdapter(options);
236
+ const adapterToUse = options.schemaAdapter ? options.schemaAdapter : Object.values(this.__transformedSchemas)[0].adapter;
237
+ const parsedResultsAfterFallbacks = await this.__validateByFallbacks(path, {
238
+ errors: parseResult.errors,
239
+ parsed: value
240
+ }, options);
241
+ parseResult.parsed = parsedResultsAfterFallbacks.parsed;
242
+ parseResult.errors = (parseResult.errors || []).concat(parsedResultsAfterFallbacks.errors || []);
243
+ if (this.__beforeValidationCallbacks.size > 0) {
244
+ for (const callback of this.__beforeValidationCallbacks.values()) {
245
+ const parsedValuesAfterValidationCallbacks = await callback(adapterToUse, adapterToUse[schemaAdapterFieldType], this, this.__transformedSchemas[adapterToUse.constructor.name].schemas, value, path, options);
246
+ parseResult.parsed = parsedValuesAfterValidationCallbacks.parsed;
247
+ parseResult.errors = Array.isArray(parseResult.errors) && Array.isArray(parsedValuesAfterValidationCallbacks.errors) ? [
248
+ ...parseResult.errors,
249
+ ...parsedValuesAfterValidationCallbacks.errors
250
+ ] : Array.isArray(parseResult.errors) ? parseResult.errors : parsedValuesAfterValidationCallbacks.errors;
251
+ }
252
+ } else {
253
+ const parsedValuesAfterValidatingByAdapter = await this.__validateByAdapter(adapterToUse, adapterToUse[schemaAdapterFieldType], this.__transformedSchemas[adapterToUse.constructor.name].schemas[0], value, path, options);
254
+ parseResult.parsed = parsedValuesAfterValidatingByAdapter.parsed;
255
+ parseResult.errors = (parseResult.errors || []).concat(parsedValuesAfterValidatingByAdapter.errors);
256
+ }
257
+ const hasToInternalCallback = typeof this.__toInternal === "function";
258
+ const shouldCallToInternalDuringParse = hasToInternalCallback && (options.toInternalToBubbleUp?.length === 0 || Array.isArray(options.toInternalToBubbleUp) === false);
259
+ const hasNoErrors = parseResult.errors === void 0 || (parseResult.errors || []).length === 0;
260
+ await Promise.all(this.__refinements.map(async (refinement) => {
261
+ const errorOrNothing = await Promise.resolve(refinement(parseResult.parsed));
262
+ if (typeof errorOrNothing === "undefined") return;
263
+ parseResult.errors.push({
264
+ isValid: false,
265
+ code: errorOrNothing.code,
266
+ message: errorOrNothing.message,
267
+ received: parseResult.parsed,
268
+ path
269
+ });
270
+ }));
271
+ if (shouldCallToInternalDuringParse && hasNoErrors) parseResult.parsed = await this.__toInternal(value);
272
+ if (shouldRunToInternalToBubbleUp && hasNoErrors) for (const functionToModifyResult of options.toInternalToBubbleUp || []) await functionToModifyResult();
273
+ return parseResult;
274
+ }
275
+ /**
276
+ * This let's you refine the schema with custom validations. This is useful when you want to validate something
277
+ * that is not supported by default by the schema adapter.
278
+ *
279
+ * @example
280
+ * ```typescript
281
+ * import * as p from '@palmares/schemas';
282
+ *
283
+ * const numberSchema = p.number().refine((value) => {
284
+ * if (value < 0) return { code: 'invalid_number', message: 'The number should be greater than 0' };
285
+ * });
286
+ *
287
+ * const { errors, parsed } = await numberSchema.parse(-1);
288
+ *
289
+ * console.log(errors);
290
+ * // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
291
+ * ```
292
+ *
293
+ * @param refinementCallback - The callback that will be called to validate the value.
294
+ * @param options - Options for the refinement.
295
+ * @param options.isAsync - Whether the callback is async or not. Defaults to true.
296
+ */
297
+ refine(refinementCallback) {
298
+ this.__refinements.push(refinementCallback);
299
+ return this;
300
+ }
301
+ /**
302
+ * Allows the value to be either undefined or null.
303
+ *
304
+ * @example
305
+ * ```typescript
306
+ * import * as p from '@palmares/schemas';
307
+ *
308
+ * const numberSchema = p.number().optional();
309
+ *
310
+ * const { errors, parsed } = await numberSchema.parse(undefined);
311
+ *
312
+ * console.log(parsed); // undefined
313
+ *
314
+ * const { errors, parsed } = await numberSchema.parse(null);
315
+ *
316
+ * console.log(parsed); // null
317
+ *
318
+ * const { errors, parsed } = await numberSchema.parse(1);
319
+ *
320
+ * console.log(parsed); // 1
321
+ * ```
322
+ *
323
+ * @returns - The schema we are working with.
324
+ */
325
+ optional(options) {
326
+ this.__optional = {
327
+ message: typeof options?.message === "string" ? options.message : "Required",
328
+ allow: typeof options?.allow === "boolean" ? options.allow : true
329
+ };
330
+ return this;
331
+ }
332
+ /**
333
+ * Allows the value to be null and ONLY null. You can also use this function to set a custom message when
334
+ * the value is NULL by setting the { message: 'Your custom message', allow: false } on the options.
335
+ *
336
+ * @example
337
+ * ```typescript
338
+ * import * as p from '@palmares/schemas';
339
+ *
340
+ * const numberSchema = p.number().nullable();
341
+ *
342
+ * const { errors, parsed } = await numberSchema.parse(null);
343
+ *
344
+ * console.log(parsed); // null
345
+ *
346
+ * const { errors, parsed } = await numberSchema.parse(undefined);
347
+ *
348
+ * console.log(errors); // [{ isValid: false, code: 'invalid_type', message: 'Invalid type', path: [] }]
349
+ * ```
350
+ *
351
+ * @param options - The options for the nullable function.
352
+ * @param options.message - The message to be shown when the value is not null. Defaults to 'Cannot be null'.
353
+ * @param options.allow - Whether the value can be null or not. Defaults to true.
354
+ *
355
+ * @returns The schema.
356
+ */
357
+ nullable(options) {
358
+ this.__nullable = {
359
+ message: typeof options?.message === "string" ? options.message : "Cannot be null",
360
+ allow: typeof options?.allow === "boolean" ? options.allow : true
361
+ };
362
+ return this;
363
+ }
364
+ /**
365
+ * Appends a custom schema to the schema, this way it will bypass the creation of the schema in runtime.
366
+ *
367
+ * By default when validating, on the first validation we create the schema. Just during the first validation.
368
+ * With this function, you bypass that, so you can speed up the validation process.
369
+ *
370
+ * @example
371
+ * ```typescript
372
+ * import * as p from '@palmares/schemas';
373
+ * import * as z from 'zod';
374
+ *
375
+ * const numberSchema = p.number().appendSchema(z.number());
376
+ *
377
+ * const { errors, parsed } = await numberSchema.parse(1);
378
+ * ```
379
+ *
380
+ * @param schema - The schema to be appended.
381
+ * @param args - The arguments for the schema.
382
+ * @param args.adapter - The adapter to be used. If not provided, the default adapter will be used.
383
+ *
384
+ * @returns The same schema again.
385
+ */
386
+ appendSchema(schema2, args) {
387
+ const adapter = args?.adapter || getDefaultAdapter();
388
+ this.__transformedSchemas[adapter.constructor.name] = {
389
+ transformed: true,
390
+ adapter,
391
+ schemas: [
392
+ schema2
393
+ ]
394
+ };
395
+ return this;
396
+ }
397
+ /**
398
+ * This method will remove the value from the representation of the schema. If the value is undefined it will keep
399
+ * that way otherwise it will set the value to undefined after it's validated.
400
+ * This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
401
+ * function. This will remove the value from the representation of the schema.
402
+ *
403
+ * By default, the value will be removed just from the representation, in other words, when you call the {@link data}
404
+ * function. But if you want to remove the value from the internal representation, you can pass the argument
405
+ * `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
406
+ * the argument `toRepresentation` as true as well.
407
+ *
408
+ * @example
409
+ * ```typescript
410
+ * import * as p from '@palmares/schemas';
411
+ *
412
+ * const userSchema = p.object({
413
+ * id: p.number().optional(),
414
+ * name: p.string(),
415
+ * password: p.string().omit()
416
+ * });
417
+ *
418
+ * const user = await userSchema.data({
419
+ * id: 1,
420
+ * name: 'John Doe',
421
+ * password: '123456'
422
+ * });
423
+ *
424
+ * console.log(user); // { id: 1, name: 'John Doe' }
425
+ * ```
426
+ *
427
+ *
428
+ * @param args - By default, the value will be removed just from the representation, in other words, when you call
429
+ * the {@link data} function.
430
+ * But if you want to remove the value from the internal representation, you can pass the argument `toInternal`
431
+ * as true. Then if you still want to remove the value from the representation, you will need to pass the
432
+ * argument `toRepresentation` as true as well.
433
+ *
434
+ * @returns The schema.
435
+ */
436
+ omit(args) {
437
+ const toRepresentation = typeof args?.toRepresentation === "boolean" ? args.toRepresentation : typeof args?.toInternal !== "boolean";
438
+ const toInternal = typeof args?.toInternal === "boolean" ? args.toInternal : false;
439
+ if (toInternal) {
440
+ if (this.__toInternal) {
441
+ const toInternal2 = this.__toInternal;
442
+ this.__toInternal = async (value) => {
443
+ await toInternal2(value);
444
+ return void 0;
445
+ };
446
+ } else this.__toInternal = async () => void 0;
447
+ } else if (toRepresentation) {
448
+ if (this.__toRepresentation) {
449
+ const toRepresentation2 = this.__toRepresentation;
450
+ this.__toRepresentation = async (value) => {
451
+ await toRepresentation2(value);
452
+ return void 0;
453
+ };
454
+ } else this.__toRepresentation = async () => void 0;
455
+ }
456
+ return this;
457
+ }
458
+ /**
459
+ * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
460
+ * source like a database. You should always return the schema after you save the value, that way we will always have
461
+ * the correct type of the schema after the save operation.
462
+ *
463
+ * You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
464
+ *
465
+ * @example
466
+ * ```typescript
467
+ * import * as p from '@palmares/schemas';
468
+ *
469
+ * import { User } from './models';
470
+ *
471
+ * const userSchema = p.object({
472
+ * id: p.number().optional(),
473
+ * name: p.string(),
474
+ * email: p.string().email(),
475
+ * }).onSave(async (value) => {
476
+ * // Create or update the user on the database using palmares models or any other library of your choice.
477
+ * if (value.id)
478
+ * await User.default.set(value, { search: { id: value.id } });
479
+ * else
480
+ * await User.default.set(value);
481
+ *
482
+ * return value;
483
+ * });
484
+ *
485
+ *
486
+ * // Then, on your controller, do something like this:
487
+ * const { isValid, save, errors } = await userSchema.validate(req.body);
488
+ * if (isValid) {
489
+ * const savedValue = await save();
490
+ * return Response.json(savedValue, { status: 201 });
491
+ * }
492
+ *
493
+ * return Response.json({ errors }, { status: 400 });
494
+ * ```
495
+ *
496
+ * @param callback - The callback that will be called to save the value on an external source.
497
+ *
498
+ * @returns The schema.
499
+ */
500
+ onSave(callback) {
501
+ this.__saveCallback = callback;
502
+ return this;
503
+ }
504
+ /**
505
+ * This function is used to validate the schema and save the value to the database. It is used in
506
+ * conjunction with the {@link onSave} function.
507
+ *
508
+ * Different from other validation libraries, palmares schemas is aware that you want to save. On your
509
+ * routes/functions we recommend to ALWAYS use this function instead of {@link parse} directly. This is because
510
+ * this function by default will return an object with the property `save` or the `errors`. If the errors are present,
511
+ * you can return the errors to the user. If the save property is present, you can use to save the value to an
512
+ * external source. e.g. a database.
513
+ *
514
+ * @example
515
+ * ```typescript
516
+ * import * as p from '@palmares/schemas';
517
+ *
518
+ * import { User } from './models';
519
+ *
520
+ * const userSchema = p.object({
521
+ * id: p.number().optional(),
522
+ * name: p.string(),
523
+ * email: p.string().email(),
524
+ * }).onSave(async (value) => {
525
+ * // Create or update the user on the database using palmares models or any other library of your choice.
526
+ * if (value.id)
527
+ * await User.default.set(value, { search: { id: value.id } });
528
+ * else
529
+ * await User.default.set(value);
530
+ *
531
+ * return value;
532
+ * });
533
+ *
534
+ *
535
+ * // Then, on your controller, do something like this:
536
+ * const { isValid, save, errors } = await userSchema.validate(req.body);
537
+ * if (isValid) {
538
+ * const savedValue = await save();
539
+ * return Response.json(savedValue, { status: 201 });
540
+ * }
541
+ *
542
+ * return Response.json({ errors }, { status: 400 });
543
+ * ```
544
+ *
545
+ * @param value - The value to be validated.
546
+ *
547
+ * @returns An object with the property isValid, if the value is valid, the function `save` will be present.
548
+ * If the value is invalid, the property errors will be present.
549
+ */
550
+ async validate(value, context) {
551
+ const { errors, parsed } = await this.__parse(value, [], {
552
+ context
553
+ });
554
+ if ((errors || []).length > 0) return {
555
+ isValid: false,
556
+ errors
557
+ };
558
+ return {
559
+ isValid: true,
560
+ save: /* @__PURE__ */ __name(async () => this._save.bind(this)(parsed, context), "save")
561
+ };
562
+ }
563
+ /**
564
+ * Internal function, when we call the {@link validate} function it's this function that gets called
565
+ * when the user uses the `save` function returned by the {@link validate} function if the value is valid.
566
+ *
567
+ * @param value - The value to be saved.
568
+ *
569
+ * @returns The value to representation.
570
+ */
571
+ async _save(value, context) {
572
+ if (this.__saveCallback) {
573
+ const result = await this.__saveCallback(value, context);
574
+ return this.data(result);
575
+ }
576
+ return this.data(value);
577
+ }
578
+ /**
579
+ * This function is used to validate and parse the value to the internal representation of the schema.
580
+ *
581
+ * @example
582
+ * ```typescript
583
+ * import * as p from '@palmares/schemas';
584
+ *
585
+ * const numberSchema = p.number().allowString();
586
+ *
587
+ * const { errors, parsed } = await numberSchema.parse('123');
588
+ *
589
+ * console.log(parsed); // 123
590
+ * ```
591
+ *
592
+ * @param value - The value to be parsed.
593
+ *
594
+ * @returns The parsed value.
595
+ */
596
+ async parse(value) {
597
+ return this.__parse(value, [], {});
598
+ }
599
+ /**
600
+ * This function is used to transform the value to the representation without validating it.
601
+ * This is useful when you want to return a data from a query directly to the user. But for example
602
+ * you are returning the data of a user, you can clean the password or any other sensitive data.
603
+ *
604
+ * @example
605
+ * ```typescript
606
+ * import * as p from '@palmares/schemas';
607
+ *
608
+ * const userSchema = p.object({
609
+ * id: p.number().optional(),
610
+ * name: p.string(),
611
+ * email: p.string().email(),
612
+ * password: p.string().optional()
613
+ * }).toRepresentation(async (value) => {
614
+ * return {
615
+ * id: value.id,
616
+ * name: value.name,
617
+ * email: value.email
618
+ * }
619
+ * });
620
+ *
621
+ * const user = await userSchema.data({
622
+ * id: 1,
623
+ * name: 'John Doe',
624
+ * email: 'john@gmail.com',
625
+ * password: '123456'
626
+ * });
627
+ * ```
628
+ */
629
+ async data(value) {
630
+ this.__getDefaultTransformedSchemas();
631
+ if (typeof this.__runBeforeParseAndData === "function") await this.__runBeforeParseAndData(this);
632
+ value = await this.__parsersToTransformValue(value);
633
+ if (this.__toRepresentation) value = await Promise.resolve(this.__toRepresentation(value));
634
+ if (this.__defaultFunction && value === void 0) value = await Promise.resolve(this.__defaultFunction());
635
+ return value;
636
+ }
637
+ instanceOf(args) {
638
+ this.__type.check = typeof args.check === "function" ? args.check : this.__type.check;
639
+ this.__type.message = typeof args.message === "string" ? args.message : this.__type.message;
640
+ return this;
641
+ }
642
+ /**
643
+ * This function is used to add a default value to the schema. If the value is either undefined or null,
644
+ * the default value will be used.
645
+ *
646
+ * @example
647
+ * ```typescript
648
+ * import * as p from '@palmares/schemas';
649
+ *
650
+ * const numberSchema = p.number().default(0);
651
+ *
652
+ * const { errors, parsed } = await numberSchema.parse(undefined);
653
+ *
654
+ * console.log(parsed); // 0
655
+ * ```
656
+ */
657
+ default(defaultValueOrFunction) {
658
+ const isFunction = typeof defaultValueOrFunction === "function";
659
+ if (isFunction) this.__defaultFunction = defaultValueOrFunction;
660
+ else this.__defaultFunction = async () => defaultValueOrFunction;
661
+ return this;
662
+ }
663
+ /**
664
+ * This function let's you customize the schema your own way. After we translate the schema on the adapter we call
665
+ * this function to let you customize the custom schema your own way. Our API does not support passthrough?
666
+ * No problem, you can use this function to customize the zod schema.
667
+ *
668
+ * @example
669
+ * ```typescript
670
+ * import * as p from '@palmares/schemas';
671
+ *
672
+ * const numberSchema = p.number().extends((schema) => {
673
+ * return schema.nonnegative();
674
+ * });
675
+ *
676
+ * const { errors, parsed } = await numberSchema.parse(-1);
677
+ *
678
+ * console.log(errors);
679
+ * // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
680
+ * ```
681
+ *
682
+ * @param callback - The callback that will be called to customize the schema.
683
+ * @param toStringCallback - The callback that will be called to transform the schema to a string when you want
684
+ * to compile the underlying schema to a string so you can save it for future runs.
685
+ *
686
+ * @returns The schema.
687
+ */
688
+ extends(callback, toStringCallback) {
689
+ this.__extends = {
690
+ callback,
691
+ toStringCallback
692
+ };
693
+ return this;
694
+ }
695
+ /**
696
+ * This function is used to transform the value to the representation of the schema. When using the {@link data}
697
+ * function. With this function you have full control to add data cleaning for example, transforming the data
698
+ * and whatever. Another use case is when you want to return deeply nested recursive data.
699
+ * The schema maps to itself.
700
+ *
701
+ * @example
702
+ * ```typescript
703
+ * import * as p from '@palmares/schemas';
704
+ *
705
+ * const recursiveSchema = p.object({
706
+ * id: p.number().optional(),
707
+ * name: p.string(),
708
+ * }).toRepresentation(async (value) => {
709
+ * return {
710
+ * id: value.id,
711
+ * name: value.name,
712
+ * children: await Promise.all(value.children.map(async (child) => await recursiveSchema.data(child)))
713
+ * }
714
+ * });
715
+ *
716
+ * const data = await recursiveSchema.data({
717
+ * id: 1,
718
+ * name: 'John Doe',
719
+ * });
720
+ * ```
721
+ *
722
+ * @example
723
+ * ```
724
+ * import * as p from '@palmares/schemas';
725
+ *
726
+ * const colorToRGBSchema = p.string().toRepresentation(async (value) => {
727
+ * switch (value) {
728
+ * case 'red': return { r: 255, g: 0, b: 0 };
729
+ * case 'green': return { r: 0, g: 255, b: 0 };
730
+ * case 'blue': return { r: 0, g: 0, b: 255 };
731
+ * default: return { r: 0, g: 0, b: 0 };
732
+ * }
733
+ * });
734
+ * ```
735
+ * @param toRepresentationCallback - The callback that will be called to transform the value to the representation.
736
+ * @param options - Options for the toRepresentation function.
737
+ * @param options.after - Whether the toRepresentationCallback should be called after the existing
738
+ * toRepresentationCallback. Defaults to true.
739
+ * @param options.before - Whether the toRepresentationCallback should be called before the existing
740
+ * toRepresentationCallback. Defaults to true.
741
+ *
742
+ * @returns The schema with a new return type
743
+ */
744
+ toRepresentation(toRepresentationCallback, options) {
745
+ if (this.__toRepresentation) {
746
+ const before = typeof options?.before === "boolean" ? options.before : typeof options?.after === "boolean" ? !options.after : true;
747
+ const existingToRepresentation = this.__toRepresentation;
748
+ this.__toRepresentation = async (value) => {
749
+ if (before) return toRepresentationCallback(await existingToRepresentation(value));
750
+ else return existingToRepresentation(await toRepresentationCallback(value));
751
+ };
752
+ } else this.__toRepresentation = toRepresentationCallback;
753
+ return this;
754
+ }
755
+ /**
756
+ * This function is used to transform the value to the internal representation of the schema. This is useful
757
+ * when you want to transform the value to a type that the schema adapter can understand. For example, you
758
+ * might want to transform a string to a date. This is the function you use.
759
+ *
760
+ * @example
761
+ * ```typescript
762
+ * import * as p from '@palmares/schemas';
763
+ *
764
+ * const dateSchema = p.string().toInternal((value) => {
765
+ * return new Date(value);
766
+ * });
767
+ *
768
+ * const date = await dateSchema.parse('2021-01-01');
769
+ *
770
+ * console.log(date); // Date object
771
+ *
772
+ * const rgbToColorSchema = p.object({
773
+ * r: p.number().min(0).max(255),
774
+ * g: p.number().min(0).max(255),
775
+ * b: p.number().min(0).max(255),
776
+ * }).toInternal(async (value) => {
777
+ * if (value.r === 255 && value.g === 0 && value.b === 0) return 'red';
778
+ * if (value.r === 0 && value.g === 255 && value.b === 0) return 'green';
779
+ * if (value.r === 0 && value.g === 0 && value.b === 255) return 'blue';
780
+ * return `rgb(${value.r}, ${value.g}, ${value.b})`;
781
+ * });
782
+ * ```
783
+ *
784
+ * @param toInternalCallback - The callback that will be called to transform the value to the internal representation.
785
+ *
786
+ * @returns The schema with a new return type.
787
+ */
788
+ toInternal(toInternalCallback) {
789
+ if (this.__toInternal) {
790
+ const toInternal = this.__toInternal;
791
+ this.__toInternal = async (value) => {
792
+ const newValue = await toInternal(value);
793
+ return toInternalCallback(newValue);
794
+ };
795
+ } else this.__toInternal = toInternalCallback;
796
+ return this;
797
+ }
798
+ /**
799
+ * Called before the validation of the schema. Let's say that you want to validate a date that might receive a
800
+ * string, you can convert that string to a date here BEFORE the validation. This pretty much transforms the value
801
+ * to a type that the schema adapter can understand.
802
+ *
803
+ * @example
804
+ * ```
805
+ * import * as p from '@palmares/schemas';
806
+ * import * as z from 'zod';
807
+ *
808
+ * const customRecordToMapSchema = p.schema().appendSchema(z.map()).toValidate(async (value) => {
809
+ * return new Map(value); // Before validating we transform the value to a map.
810
+ * });
811
+ *
812
+ * const { errors, parsed } = await customRecordToMapSchema.parse({ key: 'value' });
813
+ * ```
814
+ *
815
+ * @param toValidateCallback - The callback that will be called to validate the value.
816
+ *
817
+ * @returns The schema with a new return type.
818
+ */
819
+ toValidate(toValidateCallback) {
820
+ this.__toValidate = toValidateCallback;
821
+ return this;
822
+ }
823
+ /**
824
+ * Used to transform the given schema on a stringfied version of the adapter.
825
+ */
826
+ async compile(adapter) {
827
+ const data = await this.__transformToAdapter({
828
+ shouldAddStringVersion: true,
829
+ force: true
830
+ });
831
+ const stringVersions = data.map((value) => value.asString);
832
+ return stringVersions;
833
+ }
834
+ static new(..._args) {
835
+ const result = new _Schema();
836
+ return result;
837
+ }
838
+ };
839
+ var schema = Schema.new;
840
+ // Annotate the CommonJS export names for ESM import in node:
841
+ 0 && (module.exports = {
842
+ Schema,
843
+ schema
844
+ });