@palmares/schemas 0.1.24 → 0.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (305) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/src/adapter/fields/array.cjs +96 -0
  3. package/dist/src/adapter/fields/array.d.ts.map +1 -0
  4. package/dist/src/adapter/fields/array.js +70 -0
  5. package/dist/src/adapter/fields/boolean.cjs +102 -0
  6. package/dist/src/adapter/fields/boolean.d.ts.map +1 -0
  7. package/dist/src/adapter/fields/boolean.js +76 -0
  8. package/dist/src/adapter/fields/datetime.cjs +102 -0
  9. package/dist/src/adapter/fields/datetime.d.ts.map +1 -0
  10. package/dist/src/adapter/fields/datetime.js +76 -0
  11. package/dist/src/adapter/fields/index.cjs +87 -0
  12. package/dist/src/adapter/fields/index.d.ts.map +1 -0
  13. package/dist/src/adapter/fields/index.js +61 -0
  14. package/dist/src/adapter/fields/number.cjs +102 -0
  15. package/dist/src/adapter/fields/number.d.ts.map +1 -0
  16. package/dist/src/adapter/fields/number.js +76 -0
  17. package/dist/src/adapter/fields/object.cjs +108 -0
  18. package/dist/src/adapter/fields/object.d.ts.map +1 -0
  19. package/dist/src/adapter/fields/object.js +82 -0
  20. package/dist/src/adapter/fields/string.cjs +102 -0
  21. package/dist/src/adapter/fields/string.d.ts.map +1 -0
  22. package/dist/src/adapter/fields/string.js +76 -0
  23. package/dist/src/adapter/fields/union.cjs +102 -0
  24. package/dist/src/adapter/fields/union.d.ts.map +1 -0
  25. package/dist/src/adapter/fields/union.js +76 -0
  26. package/dist/src/adapter/index.cjs +65 -0
  27. package/dist/src/adapter/index.d.ts.map +1 -0
  28. package/dist/src/adapter/index.js +40 -0
  29. package/dist/src/adapter/types.cjs +18 -0
  30. package/dist/src/adapter/types.d.ts.map +1 -0
  31. package/dist/src/adapter/types.js +0 -0
  32. package/dist/src/compile.cjs +59 -0
  33. package/dist/src/compile.d.ts.map +1 -0
  34. package/dist/src/compile.js +36 -0
  35. package/dist/src/conf.cjs +53 -0
  36. package/dist/src/conf.d.ts.map +1 -0
  37. package/dist/src/conf.js +27 -0
  38. package/dist/src/constants.cjs +40 -0
  39. package/dist/src/constants.d.ts.map +1 -0
  40. package/dist/src/constants.js +14 -0
  41. package/dist/src/domain.cjs +63 -0
  42. package/dist/src/domain.d.ts.map +1 -0
  43. package/dist/src/domain.js +40 -0
  44. package/dist/src/exceptions.cjs +58 -0
  45. package/dist/src/exceptions.d.ts.map +1 -0
  46. package/dist/src/exceptions.js +33 -0
  47. package/dist/src/index.cjs +6520 -0
  48. package/dist/src/index.d.ts.map +1 -0
  49. package/dist/src/index.js +6456 -0
  50. package/dist/src/middleware.cjs +58 -0
  51. package/dist/src/middleware.d.ts.map +1 -0
  52. package/dist/src/middleware.js +35 -0
  53. package/dist/src/model.cjs +6003 -0
  54. package/dist/src/model.d.ts.map +1 -0
  55. package/dist/src/model.js +5978 -0
  56. package/dist/src/parsers/convert-from-number.cjs +34 -0
  57. package/dist/src/parsers/convert-from-number.d.ts.map +1 -0
  58. package/dist/src/parsers/convert-from-number.js +11 -0
  59. package/dist/src/parsers/convert-from-string.cjs +40 -0
  60. package/dist/src/parsers/convert-from-string.d.ts.map +1 -0
  61. package/dist/src/parsers/convert-from-string.js +17 -0
  62. package/dist/src/parsers/index.cjs +50 -0
  63. package/dist/src/parsers/index.d.ts.map +1 -0
  64. package/dist/src/parsers/index.js +24 -0
  65. package/dist/src/schema/array.cjs +1863 -0
  66. package/dist/src/schema/array.d.ts.map +1 -0
  67. package/dist/src/schema/array.js +1837 -0
  68. package/dist/src/schema/boolean.cjs +1800 -0
  69. package/dist/src/schema/boolean.d.ts.map +1 -0
  70. package/dist/src/schema/boolean.js +1774 -0
  71. package/dist/src/schema/datetime.cjs +1789 -0
  72. package/dist/src/schema/datetime.d.ts.map +1 -0
  73. package/dist/src/schema/datetime.js +1763 -0
  74. package/dist/src/schema/index.cjs +2026 -0
  75. package/dist/src/schema/index.d.ts.map +1 -0
  76. package/dist/src/schema/index.js +2001 -0
  77. package/dist/src/schema/number.cjs +2026 -0
  78. package/dist/src/schema/number.d.ts.map +1 -0
  79. package/dist/src/schema/number.js +2000 -0
  80. package/dist/src/schema/object.cjs +1795 -0
  81. package/dist/src/schema/object.d.ts.map +1 -0
  82. package/dist/src/schema/object.js +1769 -0
  83. package/dist/src/schema/schema.cjs +844 -0
  84. package/dist/src/schema/schema.d.ts.map +1 -0
  85. package/dist/src/schema/schema.js +818 -0
  86. package/dist/src/schema/string.cjs +2126 -0
  87. package/dist/src/schema/string.d.ts.map +1 -0
  88. package/dist/src/schema/string.js +2100 -0
  89. package/dist/src/schema/types.cjs +18 -0
  90. package/dist/src/schema/types.d.ts.map +1 -0
  91. package/dist/src/schema/types.js +0 -0
  92. package/dist/src/schema/union.cjs +1737 -0
  93. package/dist/src/schema/union.d.ts.map +1 -0
  94. package/dist/src/schema/union.js +1711 -0
  95. package/dist/src/types.cjs +18 -0
  96. package/dist/src/types.d.ts.map +1 -0
  97. package/dist/src/types.js +0 -0
  98. package/dist/src/utils.cjs +477 -0
  99. package/dist/src/utils.d.ts.map +1 -0
  100. package/dist/src/utils.js +445 -0
  101. package/dist/src/validators/array.cjs +176 -0
  102. package/dist/src/validators/array.d.ts.map +1 -0
  103. package/dist/src/validators/array.js +150 -0
  104. package/dist/src/validators/boolean.cjs +71 -0
  105. package/dist/src/validators/boolean.d.ts.map +1 -0
  106. package/dist/src/validators/boolean.js +47 -0
  107. package/dist/src/validators/datetime.cjs +129 -0
  108. package/dist/src/validators/datetime.d.ts.map +1 -0
  109. package/dist/src/validators/datetime.js +103 -0
  110. package/dist/src/validators/number.cjs +194 -0
  111. package/dist/src/validators/number.d.ts.map +1 -0
  112. package/dist/src/validators/number.js +166 -0
  113. package/dist/src/validators/object.cjs +69 -0
  114. package/dist/src/validators/object.d.ts.map +1 -0
  115. package/dist/src/validators/object.js +46 -0
  116. package/dist/src/validators/schema.cjs +158 -0
  117. package/dist/src/validators/schema.d.ts.map +1 -0
  118. package/dist/src/validators/schema.js +132 -0
  119. package/dist/src/validators/string.cjs +255 -0
  120. package/dist/src/validators/string.d.ts.map +1 -0
  121. package/dist/src/validators/string.js +224 -0
  122. package/dist/src/validators/types.cjs +18 -0
  123. package/dist/src/validators/types.d.ts.map +1 -0
  124. package/dist/src/validators/types.js +0 -0
  125. package/dist/src/validators/union.cjs +69 -0
  126. package/dist/src/validators/union.d.ts.map +1 -0
  127. package/dist/src/validators/union.js +46 -0
  128. package/dist/src/validators/utils.cjs +165 -0
  129. package/dist/src/validators/utils.d.ts.map +1 -0
  130. package/dist/src/validators/utils.js +142 -0
  131. package/dist/tsconfig.types.tsbuildinfo +1 -0
  132. package/package.json +15 -23
  133. package/dist/cjs/src/adapter/fields/array.js +0 -152
  134. package/dist/cjs/src/adapter/fields/boolean.js +0 -162
  135. package/dist/cjs/src/adapter/fields/datetime.js +0 -162
  136. package/dist/cjs/src/adapter/fields/index.js +0 -311
  137. package/dist/cjs/src/adapter/fields/number.js +0 -162
  138. package/dist/cjs/src/adapter/fields/object.js +0 -171
  139. package/dist/cjs/src/adapter/fields/string.js +0 -162
  140. package/dist/cjs/src/adapter/fields/union.js +0 -162
  141. package/dist/cjs/src/adapter/index.js +0 -201
  142. package/dist/cjs/src/adapter/types.js +0 -4
  143. package/dist/cjs/src/compile.js +0 -262
  144. package/dist/cjs/src/conf.js +0 -26
  145. package/dist/cjs/src/constants.js +0 -36
  146. package/dist/cjs/src/domain.js +0 -156
  147. package/dist/cjs/src/exceptions.js +0 -168
  148. package/dist/cjs/src/index.js +0 -317
  149. package/dist/cjs/src/middleware.js +0 -213
  150. package/dist/cjs/src/model.js +0 -628
  151. package/dist/cjs/src/parsers/convert-from-number.js +0 -15
  152. package/dist/cjs/src/parsers/convert-from-string.js +0 -19
  153. package/dist/cjs/src/parsers/index.js +0 -20
  154. package/dist/cjs/src/schema/array.js +0 -1014
  155. package/dist/cjs/src/schema/boolean.js +0 -883
  156. package/dist/cjs/src/schema/datetime.js +0 -840
  157. package/dist/cjs/src/schema/index.js +0 -12
  158. package/dist/cjs/src/schema/number.js +0 -976
  159. package/dist/cjs/src/schema/object.js +0 -1052
  160. package/dist/cjs/src/schema/schema.js +0 -1840
  161. package/dist/cjs/src/schema/string.js +0 -1038
  162. package/dist/cjs/src/schema/types.js +0 -4
  163. package/dist/cjs/src/schema/union.js +0 -1070
  164. package/dist/cjs/src/types.js +0 -4
  165. package/dist/cjs/src/utils.js +0 -647
  166. package/dist/cjs/src/validators/array.js +0 -462
  167. package/dist/cjs/src/validators/boolean.js +0 -201
  168. package/dist/cjs/src/validators/datetime.js +0 -291
  169. package/dist/cjs/src/validators/number.js +0 -375
  170. package/dist/cjs/src/validators/object.js +0 -291
  171. package/dist/cjs/src/validators/schema.js +0 -322
  172. package/dist/cjs/src/validators/string.js +0 -467
  173. package/dist/cjs/src/validators/types.js +0 -4
  174. package/dist/cjs/src/validators/union.js +0 -233
  175. package/dist/cjs/src/validators/utils.js +0 -499
  176. package/dist/cjs/tsconfig.types.tsbuildinfo +0 -1
  177. package/dist/cjs/types/adapter/fields/array.d.ts.map +0 -1
  178. package/dist/cjs/types/adapter/fields/boolean.d.ts.map +0 -1
  179. package/dist/cjs/types/adapter/fields/datetime.d.ts.map +0 -1
  180. package/dist/cjs/types/adapter/fields/index.d.ts.map +0 -1
  181. package/dist/cjs/types/adapter/fields/number.d.ts.map +0 -1
  182. package/dist/cjs/types/adapter/fields/object.d.ts.map +0 -1
  183. package/dist/cjs/types/adapter/fields/string.d.ts.map +0 -1
  184. package/dist/cjs/types/adapter/fields/union.d.ts.map +0 -1
  185. package/dist/cjs/types/adapter/index.d.ts.map +0 -1
  186. package/dist/cjs/types/adapter/types.d.ts.map +0 -1
  187. package/dist/cjs/types/compile.d.ts.map +0 -1
  188. package/dist/cjs/types/conf.d.ts.map +0 -1
  189. package/dist/cjs/types/constants.d.ts.map +0 -1
  190. package/dist/cjs/types/domain.d.ts.map +0 -1
  191. package/dist/cjs/types/exceptions.d.ts.map +0 -1
  192. package/dist/cjs/types/index.d.ts.map +0 -1
  193. package/dist/cjs/types/middleware.d.ts.map +0 -1
  194. package/dist/cjs/types/model.d.ts.map +0 -1
  195. package/dist/cjs/types/parsers/convert-from-number.d.ts.map +0 -1
  196. package/dist/cjs/types/parsers/convert-from-string.d.ts.map +0 -1
  197. package/dist/cjs/types/parsers/index.d.ts.map +0 -1
  198. package/dist/cjs/types/schema/array.d.ts.map +0 -1
  199. package/dist/cjs/types/schema/boolean.d.ts.map +0 -1
  200. package/dist/cjs/types/schema/datetime.d.ts.map +0 -1
  201. package/dist/cjs/types/schema/index.d.ts.map +0 -1
  202. package/dist/cjs/types/schema/number.d.ts.map +0 -1
  203. package/dist/cjs/types/schema/object.d.ts.map +0 -1
  204. package/dist/cjs/types/schema/schema.d.ts.map +0 -1
  205. package/dist/cjs/types/schema/string.d.ts.map +0 -1
  206. package/dist/cjs/types/schema/types.d.ts.map +0 -1
  207. package/dist/cjs/types/schema/union.d.ts.map +0 -1
  208. package/dist/cjs/types/types.d.ts.map +0 -1
  209. package/dist/cjs/types/utils.d.ts.map +0 -1
  210. package/dist/cjs/types/validators/array.d.ts.map +0 -1
  211. package/dist/cjs/types/validators/boolean.d.ts.map +0 -1
  212. package/dist/cjs/types/validators/datetime.d.ts.map +0 -1
  213. package/dist/cjs/types/validators/number.d.ts.map +0 -1
  214. package/dist/cjs/types/validators/object.d.ts.map +0 -1
  215. package/dist/cjs/types/validators/schema.d.ts.map +0 -1
  216. package/dist/cjs/types/validators/string.d.ts.map +0 -1
  217. package/dist/cjs/types/validators/types.d.ts.map +0 -1
  218. package/dist/cjs/types/validators/union.d.ts.map +0 -1
  219. package/dist/cjs/types/validators/utils.d.ts.map +0 -1
  220. package/dist/esm/src/adapter/fields/array.js +0 -13
  221. package/dist/esm/src/adapter/fields/boolean.js +0 -20
  222. package/dist/esm/src/adapter/fields/datetime.js +0 -20
  223. package/dist/esm/src/adapter/fields/index.js +0 -37
  224. package/dist/esm/src/adapter/fields/number.js +0 -20
  225. package/dist/esm/src/adapter/fields/object.js +0 -26
  226. package/dist/esm/src/adapter/fields/string.js +0 -20
  227. package/dist/esm/src/adapter/fields/union.js +0 -20
  228. package/dist/esm/src/adapter/index.js +0 -21
  229. package/dist/esm/src/adapter/types.js +0 -1
  230. package/dist/esm/src/compile.js +0 -10
  231. package/dist/esm/src/conf.js +0 -18
  232. package/dist/esm/src/constants.js +0 -4
  233. package/dist/esm/src/domain.js +0 -11
  234. package/dist/esm/src/exceptions.js +0 -15
  235. package/dist/esm/src/index.js +0 -126
  236. package/dist/esm/src/middleware.js +0 -29
  237. package/dist/esm/src/model.js +0 -264
  238. package/dist/esm/src/parsers/convert-from-number.js +0 -8
  239. package/dist/esm/src/parsers/convert-from-string.js +0 -14
  240. package/dist/esm/src/parsers/index.js +0 -2
  241. package/dist/esm/src/schema/array.js +0 -487
  242. package/dist/esm/src/schema/boolean.js +0 -520
  243. package/dist/esm/src/schema/datetime.js +0 -477
  244. package/dist/esm/src/schema/index.js +0 -3
  245. package/dist/esm/src/schema/number.js +0 -610
  246. package/dist/esm/src/schema/object.js +0 -521
  247. package/dist/esm/src/schema/schema.js +0 -767
  248. package/dist/esm/src/schema/string.js +0 -668
  249. package/dist/esm/src/schema/types.js +0 -1
  250. package/dist/esm/src/schema/union.js +0 -468
  251. package/dist/esm/src/types.js +0 -14
  252. package/dist/esm/src/utils.js +0 -207
  253. package/dist/esm/src/validators/array.js +0 -140
  254. package/dist/esm/src/validators/boolean.js +0 -37
  255. package/dist/esm/src/validators/datetime.js +0 -89
  256. package/dist/esm/src/validators/number.js +0 -148
  257. package/dist/esm/src/validators/object.js +0 -39
  258. package/dist/esm/src/validators/schema.js +0 -118
  259. package/dist/esm/src/validators/string.js +0 -202
  260. package/dist/esm/src/validators/types.js +0 -1
  261. package/dist/esm/src/validators/union.js +0 -39
  262. package/dist/esm/src/validators/utils.js +0 -147
  263. /package/dist/{cjs/types → src}/adapter/fields/array.d.ts +0 -0
  264. /package/dist/{cjs/types → src}/adapter/fields/boolean.d.ts +0 -0
  265. /package/dist/{cjs/types → src}/adapter/fields/datetime.d.ts +0 -0
  266. /package/dist/{cjs/types → src}/adapter/fields/index.d.ts +0 -0
  267. /package/dist/{cjs/types → src}/adapter/fields/number.d.ts +0 -0
  268. /package/dist/{cjs/types → src}/adapter/fields/object.d.ts +0 -0
  269. /package/dist/{cjs/types → src}/adapter/fields/string.d.ts +0 -0
  270. /package/dist/{cjs/types → src}/adapter/fields/union.d.ts +0 -0
  271. /package/dist/{cjs/types → src}/adapter/index.d.ts +0 -0
  272. /package/dist/{cjs/types → src}/adapter/types.d.ts +0 -0
  273. /package/dist/{cjs/types → src}/compile.d.ts +0 -0
  274. /package/dist/{cjs/types → src}/conf.d.ts +0 -0
  275. /package/dist/{cjs/types → src}/constants.d.ts +0 -0
  276. /package/dist/{cjs/types → src}/domain.d.ts +0 -0
  277. /package/dist/{cjs/types → src}/exceptions.d.ts +0 -0
  278. /package/dist/{cjs/types → src}/index.d.ts +0 -0
  279. /package/dist/{cjs/types → src}/middleware.d.ts +0 -0
  280. /package/dist/{cjs/types → src}/model.d.ts +0 -0
  281. /package/dist/{cjs/types → src}/parsers/convert-from-number.d.ts +0 -0
  282. /package/dist/{cjs/types → src}/parsers/convert-from-string.d.ts +0 -0
  283. /package/dist/{cjs/types → src}/parsers/index.d.ts +0 -0
  284. /package/dist/{cjs/types → src}/schema/array.d.ts +0 -0
  285. /package/dist/{cjs/types → src}/schema/boolean.d.ts +0 -0
  286. /package/dist/{cjs/types → src}/schema/datetime.d.ts +0 -0
  287. /package/dist/{cjs/types → src}/schema/index.d.ts +0 -0
  288. /package/dist/{cjs/types → src}/schema/number.d.ts +0 -0
  289. /package/dist/{cjs/types → src}/schema/object.d.ts +0 -0
  290. /package/dist/{cjs/types → src}/schema/schema.d.ts +0 -0
  291. /package/dist/{cjs/types → src}/schema/string.d.ts +0 -0
  292. /package/dist/{cjs/types → src}/schema/types.d.ts +0 -0
  293. /package/dist/{cjs/types → src}/schema/union.d.ts +0 -0
  294. /package/dist/{cjs/types → src}/types.d.ts +0 -0
  295. /package/dist/{cjs/types → src}/utils.d.ts +0 -0
  296. /package/dist/{cjs/types → src}/validators/array.d.ts +0 -0
  297. /package/dist/{cjs/types → src}/validators/boolean.d.ts +0 -0
  298. /package/dist/{cjs/types → src}/validators/datetime.d.ts +0 -0
  299. /package/dist/{cjs/types → src}/validators/number.d.ts +0 -0
  300. /package/dist/{cjs/types → src}/validators/object.d.ts +0 -0
  301. /package/dist/{cjs/types → src}/validators/schema.d.ts +0 -0
  302. /package/dist/{cjs/types → src}/validators/string.d.ts +0 -0
  303. /package/dist/{cjs/types → src}/validators/types.d.ts +0 -0
  304. /package/dist/{cjs/types → src}/validators/union.d.ts +0 -0
  305. /package/dist/{cjs/types → src}/validators/utils.d.ts +0 -0
@@ -0,0 +1,2100 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/exceptions.ts
5
+ var NoAdapterFoundError = class extends Error {
6
+ static {
7
+ __name(this, "NoAdapterFoundError");
8
+ }
9
+ constructor() {
10
+ super("No adapter found, please define an adapter using setDefaultAdapter() before using any schema.");
11
+ }
12
+ };
13
+
14
+ // src/conf.ts
15
+ function getDefaultAdapter() {
16
+ if (!globalThis.$PSchemasAdapter) throw new NoAdapterFoundError();
17
+ return globalThis.$PSchemasAdapter;
18
+ }
19
+ __name(getDefaultAdapter, "getDefaultAdapter");
20
+
21
+ // src/validators/schema.ts
22
+ function optional(args) {
23
+ return {
24
+ name: "optional",
25
+ type: "high",
26
+ // eslint-disable-next-line ts/require-await
27
+ callback: /* @__PURE__ */ __name(async (value, path) => {
28
+ if (value === void 0) {
29
+ if (args.allow === true) return {
30
+ parsed: value,
31
+ errors: [],
32
+ preventChildValidation: true
33
+ };
34
+ return {
35
+ parsed: value,
36
+ errors: [
37
+ {
38
+ isValid: false,
39
+ message: args.message,
40
+ code: "required",
41
+ // eslint-disable-next-line ts/no-unnecessary-condition
42
+ path: path || []
43
+ }
44
+ ],
45
+ preventChildValidation: true
46
+ };
47
+ }
48
+ return {
49
+ parsed: value,
50
+ errors: [],
51
+ preventChildValidation: false
52
+ };
53
+ }, "callback")
54
+ };
55
+ }
56
+ __name(optional, "optional");
57
+ function nullable(args) {
58
+ return {
59
+ name: "nullable",
60
+ type: "high",
61
+ // eslint-disable-next-line ts/require-await
62
+ callback: /* @__PURE__ */ __name(async (value, path) => {
63
+ if (value === null) {
64
+ if (args.allow === true) return {
65
+ parsed: value,
66
+ errors: [],
67
+ preventChildValidation: true
68
+ };
69
+ return {
70
+ parsed: value,
71
+ errors: [
72
+ {
73
+ isValid: false,
74
+ message: args.message,
75
+ code: "null",
76
+ // eslint-disable-next-line ts/no-unnecessary-condition
77
+ path: path || []
78
+ }
79
+ ],
80
+ preventChildValidation: true
81
+ };
82
+ }
83
+ return {
84
+ parsed: value,
85
+ errors: [],
86
+ preventChildValidation: false
87
+ };
88
+ }, "callback")
89
+ };
90
+ }
91
+ __name(nullable, "nullable");
92
+ function checkType(args) {
93
+ return {
94
+ name: "checkType",
95
+ type: "medium",
96
+ // eslint-disable-next-line ts/require-await
97
+ callback: /* @__PURE__ */ __name(async (value, path) => {
98
+ if (args.check(value)) return {
99
+ parsed: value,
100
+ errors: [],
101
+ preventChildValidation: false
102
+ };
103
+ return {
104
+ parsed: value,
105
+ errors: [
106
+ {
107
+ isValid: false,
108
+ message: args.message,
109
+ code: "invalid_type",
110
+ // eslint-disable-next-line ts/no-unnecessary-condition
111
+ path: path || []
112
+ }
113
+ ],
114
+ preventChildValidation: true
115
+ };
116
+ }, "callback")
117
+ };
118
+ }
119
+ __name(checkType, "checkType");
120
+ function is(args) {
121
+ return {
122
+ name: "is",
123
+ type: "medium",
124
+ // eslint-disable-next-line ts/require-await
125
+ callback: /* @__PURE__ */ __name(async (value, path, _options) => {
126
+ const isValid = Array.isArray(args.value) ? args.value.includes(value) : value === args.value;
127
+ return {
128
+ parsed: value,
129
+ errors: isValid ? [] : [
130
+ {
131
+ isValid: false,
132
+ code: "is",
133
+ // eslint-disable-next-line ts/no-unnecessary-condition
134
+ path: path || [],
135
+ message: "Value is not a boolean"
136
+ }
137
+ ],
138
+ preventChildValidation: true
139
+ };
140
+ }, "callback")
141
+ };
142
+ }
143
+ __name(is, "is");
144
+
145
+ // src/validators/utils.ts
146
+ var priorityByType = {
147
+ low: 0,
148
+ medium: 1,
149
+ high: 2
150
+ };
151
+ var typeByPriority = Object.entries(priorityByType).reduce((acc, [key, value]) => {
152
+ acc[value] = key;
153
+ return acc;
154
+ }, {});
155
+ var Validator = class _Validator {
156
+ static {
157
+ __name(this, "Validator");
158
+ }
159
+ $$type = "$PValidator";
160
+ child;
161
+ parent;
162
+ fallbackNamesAdded = /* @__PURE__ */ new Set();
163
+ priority;
164
+ fallbacks = [];
165
+ constructor(type) {
166
+ this.fallbackNamesAdded = /* @__PURE__ */ new Set();
167
+ this.fallbacks = [];
168
+ this.priority = priorityByType[type];
169
+ }
170
+ /**
171
+ * We create all of the validators on the schema in order, i actually didn't want to go on that route but i
172
+ * found it easier to do so.
173
+ *
174
+ * The logic here is simple, if it's not the same priority we will walk on the linked list until we find
175
+ * a validator that matches the priority we are expecting. If we can't walk anymore, we create the next
176
+ * priority validator and append it to the linked list. Be aware that it's a double linked list, so we
177
+ * can walk both ways, from the end to the start and from the start to the end.
178
+ * So you don't really need to start from the root, the linked list can start from anywhere and it will
179
+ * find it's way through.
180
+ *
181
+ * I know there are better ways to do this instead of walking through the linked list, but like i explained
182
+ * before, this is enough for now.
183
+ *
184
+ * If the priority is higher than the current priority saved on the schema, we should substitute the
185
+ * rootValidator on the schema with the new one.
186
+ *
187
+ * @param schema - The schema that we are working on right now, all fallbacks are tied to that specific schema.
188
+ * @param type - The type of the fallback that we are adding.
189
+ * @param fallback - The fallback function that we are adding.
190
+ * @param childOrParent - If we are adding a fallback to the child or to the parent.
191
+ * @param options - The options that we are passing to the fallback.
192
+ */
193
+ checkAppendOrCreate(schema2, type, fallbackName, fallback, childOrParent, options) {
194
+ const schemaWithProtected = schema2;
195
+ if (this[childOrParent]) this[childOrParent].addFallback(schemaWithProtected, type, fallbackName, fallback, options);
196
+ else {
197
+ const nextPriority = childOrParent === "child" ? this.priority - 1 : this.priority + 1;
198
+ if (Object.keys(typeByPriority).includes(String(nextPriority))) {
199
+ const nextType = typeByPriority[nextPriority];
200
+ const validatorInstance = new _Validator(nextType);
201
+ this[childOrParent] = validatorInstance;
202
+ this[childOrParent][childOrParent === "parent" ? "child" : "parent"] = this;
203
+ this[childOrParent].addFallback(schemaWithProtected, type, fallbackName, fallback, options);
204
+ if (nextPriority > schemaWithProtected.__rootFallbacksValidator.priority) schemaWithProtected.__rootFallbacksValidator = validatorInstance;
205
+ }
206
+ }
207
+ }
208
+ addFallback(schema2, type, fallbackName, fallback, options) {
209
+ if (this.fallbackNamesAdded.has(fallbackName) && options?.removeCurrent !== true) return;
210
+ this.fallbackNamesAdded.add(fallbackName);
211
+ const priority = priorityByType[type];
212
+ if (this.priority === priority) {
213
+ if (typeof options?.at === "number") this.fallbacks.splice(options.at, options.removeCurrent === true ? 1 : 0, fallback);
214
+ else this.fallbacks.push(fallback);
215
+ } else if (priority > this.priority) this.checkAppendOrCreate(schema2, type, fallbackName, fallback, "parent", options);
216
+ else if (priority < this.priority) this.checkAppendOrCreate(schema2, type, fallbackName, fallback, "child", options);
217
+ }
218
+ /**
219
+ * Validates the value against all of the fallbacks, the fallbacks are executed in order, from the highest
220
+ * priority to the lowest priority. A validator can stop the execution of the other validators if it feels
221
+ * like so. Like on the example of a value being null or undefined.
222
+ *
223
+ * @param errorsAsHashedSet - This is a set that contains all of the errors that we already found, this is
224
+ * used to avoid duplicated errors.
225
+ * @param path - The path that we are validating right now.
226
+ * @param parseResult - The result of the parsing, it contains the parsed value and the errors that we found.
227
+ * @param options - The options that we are passing to the fallback.
228
+ */
229
+ async validate(errorsAsHashedSet, path, parseResult, options) {
230
+ let doesItShouldPreventChildValidation = false;
231
+ for (const fallback of this.fallbacks) {
232
+ const { parsed, errors, preventChildValidation } = await fallback(parseResult.parsed, path, options);
233
+ parseResult.parsed = parsed;
234
+ for (const error of errors) {
235
+ if (error.isValid === false) {
236
+ const sortedError = Object.fromEntries(Object.entries(error).sort(([a], [b]) => a.localeCompare(b)));
237
+ const hashedError = JSON.stringify(sortedError);
238
+ if (errorsAsHashedSet.has(hashedError)) continue;
239
+ errorsAsHashedSet.add(hashedError);
240
+ if (!Array.isArray(parseResult.errors)) parseResult.errors = [];
241
+ parseResult.errors.push({
242
+ ...error,
243
+ received: parseResult.parsed
244
+ });
245
+ }
246
+ }
247
+ doesItShouldPreventChildValidation = doesItShouldPreventChildValidation || preventChildValidation || false;
248
+ }
249
+ if (this.child && doesItShouldPreventChildValidation === false) return await this.child.validate(errorsAsHashedSet, path, parseResult, options);
250
+ return parseResult;
251
+ }
252
+ /**
253
+ * This static method takes care of everything for you. This means that you should only call this method
254
+ * for appending new fallbacks, it takes care of creating the root validator and making sure that the
255
+ * rootValidator on the schema is the highest priority one.
256
+ *
257
+ * @param schema - The schema that we are working on right now, all fallbacks are tied to that specific
258
+ * schema. We automatically define the rootValidator on the schema so you don't need to worry about that.
259
+ * @param fallback - The fallback that we are adding. This is an object that contains the type of the
260
+ * fallback and the callback that we are adding.
261
+ * @param options - The options that we are passing to the fallback. Options like `at` and `removeCurrent`
262
+ * are passed to the `addFallback` method.
263
+ */
264
+ static createAndAppendFallback(schema2, fallback, options) {
265
+ const schemaWithProtected = schema2;
266
+ let validatorInstance = schemaWithProtected.__rootFallbacksValidator;
267
+ if (schemaWithProtected.__rootFallbacksValidator === void 0) {
268
+ validatorInstance = new _Validator(fallback.type);
269
+ schemaWithProtected.__rootFallbacksValidator = validatorInstance;
270
+ }
271
+ validatorInstance.addFallback(schema2, fallback.type, fallback.name, fallback.callback, options);
272
+ return validatorInstance;
273
+ }
274
+ toString(ident = 0) {
275
+ return `Priority: ${this.priority}
276
+ Fallbacks: ${this.fallbacks.length}
277
+ ${this.child ? `Children:
278
+ ${this.child.toString(ident + 2)}` : ""}`;
279
+ }
280
+ };
281
+
282
+ // src/utils.ts
283
+ var WithFallback = class {
284
+ static {
285
+ __name(this, "WithFallback");
286
+ }
287
+ $$type = "$PWithFallback";
288
+ fallbackFor;
289
+ transformedSchema;
290
+ adapterType;
291
+ constructor(adapterType, fallbackFor, transformedSchema) {
292
+ this.adapterType = adapterType;
293
+ this.fallbackFor = new Set(fallbackFor);
294
+ this.transformedSchema = transformedSchema;
295
+ }
296
+ };
297
+ function withFallbackFactory(adapterType) {
298
+ return (fallbackFor, transformedSchema) => new WithFallback(adapterType, fallbackFor, transformedSchema);
299
+ }
300
+ __name(withFallbackFactory, "withFallbackFactory");
301
+ async function defaultTransform(type, schema2, adapter, fieldAdapter, getValidationData, fallbackFunctions, options) {
302
+ const validationData = await Promise.resolve(getValidationData(false));
303
+ const validationDataForStringVersion = options.shouldAddStringVersion ? await Promise.resolve(getValidationData(true)) : void 0;
304
+ const schemaWithPrivateFields = schema2;
305
+ const checkIfShouldUseParserAndAppend = /* @__PURE__ */ __name((parser) => {
306
+ const isValidationDataAParser = validationData.parsers?.[parser] !== void 0;
307
+ if (isValidationDataAParser) schema2.__parsers._fallbacks.add(parser);
308
+ }, "checkIfShouldUseParserAndAppend");
309
+ const getExtendedOrNotSchemaAndString = /* @__PURE__ */ __name((schema3, toStringVersion) => {
310
+ const extendedOrNotSchema2 = typeof schemaWithPrivateFields.__extends?.callback === "function" ? schemaWithPrivateFields.__extends.callback(schema3) : schema3;
311
+ const extendedOrNotSchemaString2 = typeof schemaWithPrivateFields.__extends?.toStringCallback === "function" ? schemaWithPrivateFields.__extends.toStringCallback(toStringVersion) : toStringVersion;
312
+ return [
313
+ extendedOrNotSchema2,
314
+ extendedOrNotSchemaString2
315
+ ];
316
+ }, "getExtendedOrNotSchemaAndString");
317
+ const checkIfShouldAppendFallbackAndAppend = /* @__PURE__ */ __name((fallback) => {
318
+ const wereArgumentsForThatFallbackDefinedAndFallbackFunctionDefined = validationData[fallback] !== void 0 && fallbackFunctions[fallback] !== void 0;
319
+ if (wereArgumentsForThatFallbackDefinedAndFallbackFunctionDefined) {
320
+ const fallbackReturnType = fallbackFunctions[fallback](validationData[fallback]);
321
+ Validator.createAndAppendFallback(schema2, fallbackReturnType);
322
+ }
323
+ }, "checkIfShouldAppendFallbackAndAppend");
324
+ const appendRootFallback = /* @__PURE__ */ __name(() => {
325
+ if (options.validatorsIfFallbackOrNotSupported) {
326
+ const validatorsIfFallbackOrNotSupported = Array.isArray(options.validatorsIfFallbackOrNotSupported) ? options.validatorsIfFallbackOrNotSupported : [
327
+ options.validatorsIfFallbackOrNotSupported
328
+ ];
329
+ for (const fallback of validatorsIfFallbackOrNotSupported) Validator.createAndAppendFallback(schema2, fallback);
330
+ }
331
+ }, "appendRootFallback");
332
+ const appendRequiredFallbacks = /* @__PURE__ */ __name(() => {
333
+ const hasFallbacks = schemaWithPrivateFields.__rootFallbacksValidator?.["$$type"] === "$PValidator";
334
+ if (hasFallbacks) {
335
+ Validator.createAndAppendFallback(schema2, optional(schemaWithPrivateFields.__optional));
336
+ Validator.createAndAppendFallback(schema2, nullable(schemaWithPrivateFields.__nullable));
337
+ Validator.createAndAppendFallback(schema2, checkType(schemaWithPrivateFields.__type));
338
+ }
339
+ }, "appendRequiredFallbacks");
340
+ const isFieldAdapterNotSupportedForThatFieldType = fieldAdapter === void 0;
341
+ if (options.fallbackIfNotSupported !== void 0 && isFieldAdapterNotSupportedForThatFieldType) {
342
+ const existingFallbacks = Object.keys(fallbackFunctions);
343
+ const allParsers = Object.keys(validationData["parsers"]);
344
+ appendRootFallback();
345
+ for (const fallback of existingFallbacks) checkIfShouldAppendFallbackAndAppend(fallback);
346
+ for (const parser of allParsers) checkIfShouldUseParserAndAppend(parser);
347
+ appendRequiredFallbacks();
348
+ return options.fallbackIfNotSupported();
349
+ }
350
+ if (!fieldAdapter) throw new Error("The field adapter is not supported and no fallback was provided.");
351
+ const translatedSchemaOrWithFallback = await Promise.resolve(fieldAdapter.translate(adapter.field, {
352
+ withFallback: withFallbackFactory(type),
353
+ ...validationData
354
+ }));
355
+ let stringVersion = "";
356
+ if (options.shouldAddStringVersion) stringVersion = await fieldAdapter.toString(adapter, adapter.field, validationDataForStringVersion);
357
+ if (translatedSchemaOrWithFallback?.["$$type"] === "$PWithFallback") {
358
+ appendRootFallback();
359
+ for (const fallback of translatedSchemaOrWithFallback.fallbackFor) {
360
+ checkIfShouldAppendFallbackAndAppend(fallback);
361
+ checkIfShouldUseParserAndAppend(fallback);
362
+ }
363
+ const [extendedOrNotSchema2, extendedOrNotSchemaString2] = getExtendedOrNotSchemaAndString(translatedSchemaOrWithFallback.transformedSchema, stringVersion);
364
+ appendRequiredFallbacks();
365
+ return [
366
+ {
367
+ transformed: extendedOrNotSchema2,
368
+ asString: extendedOrNotSchemaString2
369
+ }
370
+ ];
371
+ }
372
+ const [extendedOrNotSchema, extendedOrNotSchemaString] = getExtendedOrNotSchemaAndString(translatedSchemaOrWithFallback, stringVersion);
373
+ return [
374
+ {
375
+ transformed: extendedOrNotSchema,
376
+ asString: extendedOrNotSchemaString
377
+ }
378
+ ];
379
+ }
380
+ __name(defaultTransform, "defaultTransform");
381
+ async function defaultTransformToAdapter(callback, schema2, transformedSchemas, options, type) {
382
+ const isTransformedSchemasEmpty = Object.keys(transformedSchemas).length <= 0;
383
+ if (isTransformedSchemasEmpty) {
384
+ const adapterInstanceToUse = (
385
+ // eslint-disable-next-line ts/no-unnecessary-condition
386
+ options.schemaAdapter?.["$$type"] === "$PSchemaAdapter" ? options.schemaAdapter : getDefaultAdapter()
387
+ );
388
+ schema2["__transformedSchemas"][adapterInstanceToUse.name] = {
389
+ transformed: false,
390
+ adapter: adapterInstanceToUse,
391
+ schemas: []
392
+ };
393
+ }
394
+ const schemaAdapterNameToUse = options.schemaAdapter?.name || Object.keys(transformedSchemas)[0];
395
+ const isACustomSchemaAdapterAndNotYetDefined = (
396
+ // eslint-disable-next-line ts/no-unnecessary-condition
397
+ transformedSchemas[schemaAdapterNameToUse] === void 0 && options.schemaAdapter !== void 0
398
+ );
399
+ if (isACustomSchemaAdapterAndNotYetDefined) transformedSchemas[schemaAdapterNameToUse] = {
400
+ transformed: false,
401
+ adapter: options.schemaAdapter,
402
+ schemas: []
403
+ };
404
+ const shouldTranslate = transformedSchemas[schemaAdapterNameToUse].transformed === false || options.force === true;
405
+ if (shouldTranslate) {
406
+ const translatedSchemas = await callback(transformedSchemas[schemaAdapterNameToUse].adapter);
407
+ transformedSchemas[schemaAdapterNameToUse].schemas = translatedSchemas;
408
+ transformedSchemas[schemaAdapterNameToUse].transformed = true;
409
+ }
410
+ transformedSchemas[schemaAdapterNameToUse].transformed = true;
411
+ return transformedSchemas[schemaAdapterNameToUse].schemas;
412
+ }
413
+ __name(defaultTransformToAdapter, "defaultTransformToAdapter");
414
+ async function formatErrorFromParseMethod(adapter, fieldAdapter, error, received, schema2, path, errorsAsHashedSet) {
415
+ const formattedError = await fieldAdapter.formatError(adapter, adapter.field, schema2, error);
416
+ formattedError.path = Array.isArray(formattedError.path) ? [
417
+ ...path,
418
+ ...formattedError.path
419
+ ] : path;
420
+ const formattedErrorAsParseResultError = formattedError;
421
+ formattedErrorAsParseResultError.isValid = false;
422
+ const sortedError = Object.fromEntries(Object.entries(formattedErrorAsParseResultError).sort(([a], [b]) => a.localeCompare(b)));
423
+ const hashedError = JSON.stringify(sortedError);
424
+ errorsAsHashedSet.add(JSON.stringify(sortedError));
425
+ formattedErrorAsParseResultError.received = received;
426
+ return formattedErrorAsParseResultError;
427
+ }
428
+ __name(formatErrorFromParseMethod, "formatErrorFromParseMethod");
429
+
430
+ // src/schema/schema.ts
431
+ var Schema = class _Schema {
432
+ static {
433
+ __name(this, "Schema");
434
+ }
435
+ $$type = "$PSchema";
436
+ fieldType = "schema";
437
+ // Those functions will assume control of the validation process on adapters, instead of the schema.
438
+ // Why this is used? The idea is that the Schema has NO idea
439
+ // that one of it's children might be an UnionSchema for example. The adapter might not support unions,
440
+ // so then we give control to the union. The parent schema will already have an array of translated
441
+ // adapter schemas. This means for a union with Number and String it'll generate two schemas, one for number
442
+ // and one for the value as String. Of course this gets multiplied. So if we have a union with Number and String.
443
+ // We should take those two schemas from the array and validate them individually. This logic is
444
+ // handled by the union schema. If we have an intersection type for example, instead of validating
445
+ // One schema OR the other, we validate one schema AND the other. This will be handled
446
+ // by the schema that contains that intersection logic.
447
+ __beforeValidationCallbacks = /* @__PURE__ */ new Map();
448
+ __cachedGetParent;
449
+ set __getParent(value) {
450
+ this.__cachedGetParent = value;
451
+ }
452
+ get __getParent() {
453
+ return this.__cachedGetParent;
454
+ }
455
+ __alreadyAppliedModel;
456
+ __runBeforeParseAndData;
457
+ __rootFallbacksValidator;
458
+ __saveCallback;
459
+ __modelOmitCallback;
460
+ __parsers = {
461
+ high: /* @__PURE__ */ new Map(),
462
+ medium: /* @__PURE__ */ new Map(),
463
+ low: /* @__PURE__ */ new Map(),
464
+ _fallbacks: /* @__PURE__ */ new Set()
465
+ };
466
+ __refinements = [];
467
+ __nullable = {
468
+ message: "Cannot be null",
469
+ allow: false
470
+ };
471
+ __optional = {
472
+ message: "Required",
473
+ allow: false
474
+ };
475
+ __extends = void 0;
476
+ __transformedSchemas = {};
477
+ __defaultFunction = void 0;
478
+ __toRepresentation = void 0;
479
+ __toValidate = void 0;
480
+ __toInternal = void 0;
481
+ __type = {
482
+ message: "Invalid type",
483
+ check: /* @__PURE__ */ __name(() => true, "check")
484
+ };
485
+ __getDefaultTransformedSchemas() {
486
+ const adapterInstance = getDefaultAdapter();
487
+ if (this.__transformedSchemas[adapterInstance.constructor.name] === void 0) this.__transformedSchemas[adapterInstance.constructor.name] = {
488
+ transformed: false,
489
+ adapter: adapterInstance,
490
+ schemas: []
491
+ };
492
+ }
493
+ /**
494
+ * This will validate the data with the fallbacks, so internally, without relaying on the schema adapter.
495
+ * This is nice because we can support things that the schema adapter is not able to support by default.
496
+ *
497
+ * @param errorsAsHashedSet - The errors as a hashed set. This is used to prevent duplicate errors.
498
+ * @param path - The path of the error.
499
+ * @param parseResult - The result of the parse method.
500
+ */
501
+ async __validateByFallbacks(path, parseResult, options) {
502
+ if (this.__rootFallbacksValidator) return this.__rootFallbacksValidator.validate(options.errorsAsHashedSet, path, parseResult, options);
503
+ return parseResult;
504
+ }
505
+ /**
506
+ * This will validate by the adapter. In other words, we send the data to the schema adapter and then we validate
507
+ * that data.
508
+ * So understand that, first we send the data to the adapter, the adapter validates it, then, after we validate
509
+ * from the adapter we validate with the fallbacks so we can do all of the extra validations not handled by
510
+ * the adapter.
511
+ *
512
+ * @param value - The value to be validated.
513
+ * @param errorsAsHashedSet - The errors as a hashed set. This is used to prevent duplicate errors on the validator.
514
+ * @param path - The path of the error so we can construct an object with the nested paths of the error.
515
+ * @param parseResult - The result of the parse method.
516
+ *
517
+ * @returns The result and the errors of the parse method.
518
+ */
519
+ async __validateByAdapter(adapter, fieldAdapter, schema2, value, path, options) {
520
+ const parseResult = {
521
+ errors: [],
522
+ parsed: value
523
+ };
524
+ parseResult.errors = [];
525
+ parseResult.parsed = value;
526
+ if (fieldAdapter === void 0 || typeof fieldAdapter.parse !== "function") return parseResult;
527
+ const adapterParseResult = await fieldAdapter.parse(adapter, adapter.field, schema2.transformed, value, options.args);
528
+ parseResult.parsed = adapterParseResult.parsed;
529
+ if (adapterParseResult.errors) {
530
+ 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())));
531
+ else parseResult.errors = [
532
+ await formatErrorFromParseMethod(adapter, fieldAdapter, parseResult.errors, value, schema2.transformed, path, options.errorsAsHashedSet || /* @__PURE__ */ new Set())
533
+ ];
534
+ }
535
+ parseResult.errors = parseResult.errors.filter((error) => typeof error !== "undefined");
536
+ return parseResult;
537
+ }
538
+ // eslint-disable-next-line ts/require-await
539
+ async __transformToAdapter(_options) {
540
+ throw new Error("Not implemented");
541
+ }
542
+ /** */
543
+ async __parsersToTransformValue(value, parsersToUse) {
544
+ let shouldStop = false;
545
+ for (const [parserName, parser] of this.__parsers.high.entries()) {
546
+ if (parsersToUse instanceof Set === false || parsersToUse.has(parserName)) {
547
+ const result = await Promise.resolve(parser(value));
548
+ if (result.preventNextParsers) shouldStop = true;
549
+ value = result.value;
550
+ } else continue;
551
+ }
552
+ if (shouldStop === false) {
553
+ for (const [parserName, parser] of this.__parsers.medium.entries()) {
554
+ if (parsersToUse instanceof Set === false || parsersToUse.has(parserName)) {
555
+ const result = await Promise.resolve(parser(value));
556
+ if (result.preventNextParsers) shouldStop = true;
557
+ value = result.value;
558
+ } else continue;
559
+ }
560
+ }
561
+ if (shouldStop === false) {
562
+ for (const [parserName, parser] of this.__parsers.low.entries()) {
563
+ if (parsersToUse instanceof Set === false || parsersToUse.has(parserName)) {
564
+ const result = await Promise.resolve(parser(value));
565
+ if (result.preventNextParsers) shouldStop = true;
566
+ value = result.value;
567
+ } else continue;
568
+ }
569
+ }
570
+ return value;
571
+ }
572
+ async __parse(value, path = [], options) {
573
+ this.__getDefaultTransformedSchemas();
574
+ if (typeof this.__runBeforeParseAndData === "function") await this.__runBeforeParseAndData(this);
575
+ const shouldRunToInternalToBubbleUp = options.toInternalToBubbleUp === void 0;
576
+ if (shouldRunToInternalToBubbleUp) options.toInternalToBubbleUp = [];
577
+ if (options.errorsAsHashedSet instanceof Set === false) options.errorsAsHashedSet = /* @__PURE__ */ new Set();
578
+ const shouldCallDefaultFunction = value === void 0 && typeof this.__defaultFunction === "function";
579
+ const shouldCallToValidateCallback = typeof this.__toValidate === "function";
580
+ const schemaAdapterFieldType = this.fieldType;
581
+ if (shouldCallDefaultFunction) value = await this.__defaultFunction();
582
+ if (shouldCallToValidateCallback) value = await Promise.resolve(this.__toValidate(value));
583
+ const parseResult = {
584
+ errors: [],
585
+ parsed: value
586
+ };
587
+ value = await this.__parsersToTransformValue(value, this.__parsers._fallbacks);
588
+ if (options.appendFallbacksBeforeAdapterValidation === void 0) options.appendFallbacksBeforeAdapterValidation = (schema2, name, callback) => {
589
+ if (this !== schema2) this.__beforeValidationCallbacks.set(name, callback);
590
+ };
591
+ if (this.__transformedSchemas[options.schemaAdapter?.constructor.name || getDefaultAdapter().constructor.name].transformed === false) await this.__transformToAdapter(options);
592
+ const adapterToUse = options.schemaAdapter ? options.schemaAdapter : Object.values(this.__transformedSchemas)[0].adapter;
593
+ const parsedResultsAfterFallbacks = await this.__validateByFallbacks(path, {
594
+ errors: parseResult.errors,
595
+ parsed: value
596
+ }, options);
597
+ parseResult.parsed = parsedResultsAfterFallbacks.parsed;
598
+ parseResult.errors = (parseResult.errors || []).concat(parsedResultsAfterFallbacks.errors || []);
599
+ if (this.__beforeValidationCallbacks.size > 0) {
600
+ for (const callback of this.__beforeValidationCallbacks.values()) {
601
+ const parsedValuesAfterValidationCallbacks = await callback(adapterToUse, adapterToUse[schemaAdapterFieldType], this, this.__transformedSchemas[adapterToUse.constructor.name].schemas, value, path, options);
602
+ parseResult.parsed = parsedValuesAfterValidationCallbacks.parsed;
603
+ parseResult.errors = Array.isArray(parseResult.errors) && Array.isArray(parsedValuesAfterValidationCallbacks.errors) ? [
604
+ ...parseResult.errors,
605
+ ...parsedValuesAfterValidationCallbacks.errors
606
+ ] : Array.isArray(parseResult.errors) ? parseResult.errors : parsedValuesAfterValidationCallbacks.errors;
607
+ }
608
+ } else {
609
+ const parsedValuesAfterValidatingByAdapter = await this.__validateByAdapter(adapterToUse, adapterToUse[schemaAdapterFieldType], this.__transformedSchemas[adapterToUse.constructor.name].schemas[0], value, path, options);
610
+ parseResult.parsed = parsedValuesAfterValidatingByAdapter.parsed;
611
+ parseResult.errors = (parseResult.errors || []).concat(parsedValuesAfterValidatingByAdapter.errors);
612
+ }
613
+ const hasToInternalCallback = typeof this.__toInternal === "function";
614
+ const shouldCallToInternalDuringParse = hasToInternalCallback && (options.toInternalToBubbleUp?.length === 0 || Array.isArray(options.toInternalToBubbleUp) === false);
615
+ const hasNoErrors = parseResult.errors === void 0 || (parseResult.errors || []).length === 0;
616
+ await Promise.all(this.__refinements.map(async (refinement) => {
617
+ const errorOrNothing = await Promise.resolve(refinement(parseResult.parsed));
618
+ if (typeof errorOrNothing === "undefined") return;
619
+ parseResult.errors.push({
620
+ isValid: false,
621
+ code: errorOrNothing.code,
622
+ message: errorOrNothing.message,
623
+ received: parseResult.parsed,
624
+ path
625
+ });
626
+ }));
627
+ if (shouldCallToInternalDuringParse && hasNoErrors) parseResult.parsed = await this.__toInternal(value);
628
+ if (shouldRunToInternalToBubbleUp && hasNoErrors) for (const functionToModifyResult of options.toInternalToBubbleUp || []) await functionToModifyResult();
629
+ return parseResult;
630
+ }
631
+ /**
632
+ * This let's you refine the schema with custom validations. This is useful when you want to validate something
633
+ * that is not supported by default by the schema adapter.
634
+ *
635
+ * @example
636
+ * ```typescript
637
+ * import * as p from '@palmares/schemas';
638
+ *
639
+ * const numberSchema = p.number().refine((value) => {
640
+ * if (value < 0) return { code: 'invalid_number', message: 'The number should be greater than 0' };
641
+ * });
642
+ *
643
+ * const { errors, parsed } = await numberSchema.parse(-1);
644
+ *
645
+ * console.log(errors);
646
+ * // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
647
+ * ```
648
+ *
649
+ * @param refinementCallback - The callback that will be called to validate the value.
650
+ * @param options - Options for the refinement.
651
+ * @param options.isAsync - Whether the callback is async or not. Defaults to true.
652
+ */
653
+ refine(refinementCallback) {
654
+ this.__refinements.push(refinementCallback);
655
+ return this;
656
+ }
657
+ /**
658
+ * Allows the value to be either undefined or null.
659
+ *
660
+ * @example
661
+ * ```typescript
662
+ * import * as p from '@palmares/schemas';
663
+ *
664
+ * const numberSchema = p.number().optional();
665
+ *
666
+ * const { errors, parsed } = await numberSchema.parse(undefined);
667
+ *
668
+ * console.log(parsed); // undefined
669
+ *
670
+ * const { errors, parsed } = await numberSchema.parse(null);
671
+ *
672
+ * console.log(parsed); // null
673
+ *
674
+ * const { errors, parsed } = await numberSchema.parse(1);
675
+ *
676
+ * console.log(parsed); // 1
677
+ * ```
678
+ *
679
+ * @returns - The schema we are working with.
680
+ */
681
+ optional(options) {
682
+ this.__optional = {
683
+ message: typeof options?.message === "string" ? options.message : "Required",
684
+ allow: typeof options?.allow === "boolean" ? options.allow : true
685
+ };
686
+ return this;
687
+ }
688
+ /**
689
+ * Allows the value to be null and ONLY null. You can also use this function to set a custom message when
690
+ * the value is NULL by setting the { message: 'Your custom message', allow: false } on the options.
691
+ *
692
+ * @example
693
+ * ```typescript
694
+ * import * as p from '@palmares/schemas';
695
+ *
696
+ * const numberSchema = p.number().nullable();
697
+ *
698
+ * const { errors, parsed } = await numberSchema.parse(null);
699
+ *
700
+ * console.log(parsed); // null
701
+ *
702
+ * const { errors, parsed } = await numberSchema.parse(undefined);
703
+ *
704
+ * console.log(errors); // [{ isValid: false, code: 'invalid_type', message: 'Invalid type', path: [] }]
705
+ * ```
706
+ *
707
+ * @param options - The options for the nullable function.
708
+ * @param options.message - The message to be shown when the value is not null. Defaults to 'Cannot be null'.
709
+ * @param options.allow - Whether the value can be null or not. Defaults to true.
710
+ *
711
+ * @returns The schema.
712
+ */
713
+ nullable(options) {
714
+ this.__nullable = {
715
+ message: typeof options?.message === "string" ? options.message : "Cannot be null",
716
+ allow: typeof options?.allow === "boolean" ? options.allow : true
717
+ };
718
+ return this;
719
+ }
720
+ /**
721
+ * Appends a custom schema to the schema, this way it will bypass the creation of the schema in runtime.
722
+ *
723
+ * By default when validating, on the first validation we create the schema. Just during the first validation.
724
+ * With this function, you bypass that, so you can speed up the validation process.
725
+ *
726
+ * @example
727
+ * ```typescript
728
+ * import * as p from '@palmares/schemas';
729
+ * import * as z from 'zod';
730
+ *
731
+ * const numberSchema = p.number().appendSchema(z.number());
732
+ *
733
+ * const { errors, parsed } = await numberSchema.parse(1);
734
+ * ```
735
+ *
736
+ * @param schema - The schema to be appended.
737
+ * @param args - The arguments for the schema.
738
+ * @param args.adapter - The adapter to be used. If not provided, the default adapter will be used.
739
+ *
740
+ * @returns The same schema again.
741
+ */
742
+ appendSchema(schema2, args) {
743
+ const adapter = args?.adapter || getDefaultAdapter();
744
+ this.__transformedSchemas[adapter.constructor.name] = {
745
+ transformed: true,
746
+ adapter,
747
+ schemas: [
748
+ schema2
749
+ ]
750
+ };
751
+ return this;
752
+ }
753
+ /**
754
+ * This method will remove the value from the representation of the schema. If the value is undefined it will keep
755
+ * that way otherwise it will set the value to undefined after it's validated.
756
+ * This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
757
+ * function. This will remove the value from the representation of the schema.
758
+ *
759
+ * By default, the value will be removed just from the representation, in other words, when you call the {@link data}
760
+ * function. But if you want to remove the value from the internal representation, you can pass the argument
761
+ * `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
762
+ * the argument `toRepresentation` as true as well.
763
+ *
764
+ * @example
765
+ * ```typescript
766
+ * import * as p from '@palmares/schemas';
767
+ *
768
+ * const userSchema = p.object({
769
+ * id: p.number().optional(),
770
+ * name: p.string(),
771
+ * password: p.string().omit()
772
+ * });
773
+ *
774
+ * const user = await userSchema.data({
775
+ * id: 1,
776
+ * name: 'John Doe',
777
+ * password: '123456'
778
+ * });
779
+ *
780
+ * console.log(user); // { id: 1, name: 'John Doe' }
781
+ * ```
782
+ *
783
+ *
784
+ * @param args - By default, the value will be removed just from the representation, in other words, when you call
785
+ * the {@link data} function.
786
+ * But if you want to remove the value from the internal representation, you can pass the argument `toInternal`
787
+ * as true. Then if you still want to remove the value from the representation, you will need to pass the
788
+ * argument `toRepresentation` as true as well.
789
+ *
790
+ * @returns The schema.
791
+ */
792
+ omit(args) {
793
+ const toRepresentation = typeof args?.toRepresentation === "boolean" ? args.toRepresentation : typeof args?.toInternal !== "boolean";
794
+ const toInternal = typeof args?.toInternal === "boolean" ? args.toInternal : false;
795
+ if (toInternal) {
796
+ if (this.__toInternal) {
797
+ const toInternal2 = this.__toInternal;
798
+ this.__toInternal = async (value) => {
799
+ await toInternal2(value);
800
+ return void 0;
801
+ };
802
+ } else this.__toInternal = async () => void 0;
803
+ } else if (toRepresentation) {
804
+ if (this.__toRepresentation) {
805
+ const toRepresentation2 = this.__toRepresentation;
806
+ this.__toRepresentation = async (value) => {
807
+ await toRepresentation2(value);
808
+ return void 0;
809
+ };
810
+ } else this.__toRepresentation = async () => void 0;
811
+ }
812
+ return this;
813
+ }
814
+ /**
815
+ * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
816
+ * source like a database. You should always return the schema after you save the value, that way we will always have
817
+ * the correct type of the schema after the save operation.
818
+ *
819
+ * You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
820
+ *
821
+ * @example
822
+ * ```typescript
823
+ * import * as p from '@palmares/schemas';
824
+ *
825
+ * import { User } from './models';
826
+ *
827
+ * const userSchema = p.object({
828
+ * id: p.number().optional(),
829
+ * name: p.string(),
830
+ * email: p.string().email(),
831
+ * }).onSave(async (value) => {
832
+ * // Create or update the user on the database using palmares models or any other library of your choice.
833
+ * if (value.id)
834
+ * await User.default.set(value, { search: { id: value.id } });
835
+ * else
836
+ * await User.default.set(value);
837
+ *
838
+ * return value;
839
+ * });
840
+ *
841
+ *
842
+ * // Then, on your controller, do something like this:
843
+ * const { isValid, save, errors } = await userSchema.validate(req.body);
844
+ * if (isValid) {
845
+ * const savedValue = await save();
846
+ * return Response.json(savedValue, { status: 201 });
847
+ * }
848
+ *
849
+ * return Response.json({ errors }, { status: 400 });
850
+ * ```
851
+ *
852
+ * @param callback - The callback that will be called to save the value on an external source.
853
+ *
854
+ * @returns The schema.
855
+ */
856
+ onSave(callback) {
857
+ this.__saveCallback = callback;
858
+ return this;
859
+ }
860
+ /**
861
+ * This function is used to validate the schema and save the value to the database. It is used in
862
+ * conjunction with the {@link onSave} function.
863
+ *
864
+ * Different from other validation libraries, palmares schemas is aware that you want to save. On your
865
+ * routes/functions we recommend to ALWAYS use this function instead of {@link parse} directly. This is because
866
+ * this function by default will return an object with the property `save` or the `errors`. If the errors are present,
867
+ * you can return the errors to the user. If the save property is present, you can use to save the value to an
868
+ * external source. e.g. a database.
869
+ *
870
+ * @example
871
+ * ```typescript
872
+ * import * as p from '@palmares/schemas';
873
+ *
874
+ * import { User } from './models';
875
+ *
876
+ * const userSchema = p.object({
877
+ * id: p.number().optional(),
878
+ * name: p.string(),
879
+ * email: p.string().email(),
880
+ * }).onSave(async (value) => {
881
+ * // Create or update the user on the database using palmares models or any other library of your choice.
882
+ * if (value.id)
883
+ * await User.default.set(value, { search: { id: value.id } });
884
+ * else
885
+ * await User.default.set(value);
886
+ *
887
+ * return value;
888
+ * });
889
+ *
890
+ *
891
+ * // Then, on your controller, do something like this:
892
+ * const { isValid, save, errors } = await userSchema.validate(req.body);
893
+ * if (isValid) {
894
+ * const savedValue = await save();
895
+ * return Response.json(savedValue, { status: 201 });
896
+ * }
897
+ *
898
+ * return Response.json({ errors }, { status: 400 });
899
+ * ```
900
+ *
901
+ * @param value - The value to be validated.
902
+ *
903
+ * @returns An object with the property isValid, if the value is valid, the function `save` will be present.
904
+ * If the value is invalid, the property errors will be present.
905
+ */
906
+ async validate(value, context) {
907
+ const { errors, parsed } = await this.__parse(value, [], {
908
+ context
909
+ });
910
+ if ((errors || []).length > 0) return {
911
+ isValid: false,
912
+ errors
913
+ };
914
+ return {
915
+ isValid: true,
916
+ save: /* @__PURE__ */ __name(async () => this._save.bind(this)(parsed, context), "save")
917
+ };
918
+ }
919
+ /**
920
+ * Internal function, when we call the {@link validate} function it's this function that gets called
921
+ * when the user uses the `save` function returned by the {@link validate} function if the value is valid.
922
+ *
923
+ * @param value - The value to be saved.
924
+ *
925
+ * @returns The value to representation.
926
+ */
927
+ async _save(value, context) {
928
+ if (this.__saveCallback) {
929
+ const result = await this.__saveCallback(value, context);
930
+ return this.data(result);
931
+ }
932
+ return this.data(value);
933
+ }
934
+ /**
935
+ * This function is used to validate and parse the value to the internal representation of the schema.
936
+ *
937
+ * @example
938
+ * ```typescript
939
+ * import * as p from '@palmares/schemas';
940
+ *
941
+ * const numberSchema = p.number().allowString();
942
+ *
943
+ * const { errors, parsed } = await numberSchema.parse('123');
944
+ *
945
+ * console.log(parsed); // 123
946
+ * ```
947
+ *
948
+ * @param value - The value to be parsed.
949
+ *
950
+ * @returns The parsed value.
951
+ */
952
+ async parse(value) {
953
+ return this.__parse(value, [], {});
954
+ }
955
+ /**
956
+ * This function is used to transform the value to the representation without validating it.
957
+ * This is useful when you want to return a data from a query directly to the user. But for example
958
+ * you are returning the data of a user, you can clean the password or any other sensitive data.
959
+ *
960
+ * @example
961
+ * ```typescript
962
+ * import * as p from '@palmares/schemas';
963
+ *
964
+ * const userSchema = p.object({
965
+ * id: p.number().optional(),
966
+ * name: p.string(),
967
+ * email: p.string().email(),
968
+ * password: p.string().optional()
969
+ * }).toRepresentation(async (value) => {
970
+ * return {
971
+ * id: value.id,
972
+ * name: value.name,
973
+ * email: value.email
974
+ * }
975
+ * });
976
+ *
977
+ * const user = await userSchema.data({
978
+ * id: 1,
979
+ * name: 'John Doe',
980
+ * email: 'john@gmail.com',
981
+ * password: '123456'
982
+ * });
983
+ * ```
984
+ */
985
+ async data(value) {
986
+ this.__getDefaultTransformedSchemas();
987
+ if (typeof this.__runBeforeParseAndData === "function") await this.__runBeforeParseAndData(this);
988
+ value = await this.__parsersToTransformValue(value);
989
+ if (this.__toRepresentation) value = await Promise.resolve(this.__toRepresentation(value));
990
+ if (this.__defaultFunction && value === void 0) value = await Promise.resolve(this.__defaultFunction());
991
+ return value;
992
+ }
993
+ instanceOf(args) {
994
+ this.__type.check = typeof args.check === "function" ? args.check : this.__type.check;
995
+ this.__type.message = typeof args.message === "string" ? args.message : this.__type.message;
996
+ return this;
997
+ }
998
+ /**
999
+ * This function is used to add a default value to the schema. If the value is either undefined or null,
1000
+ * the default value will be used.
1001
+ *
1002
+ * @example
1003
+ * ```typescript
1004
+ * import * as p from '@palmares/schemas';
1005
+ *
1006
+ * const numberSchema = p.number().default(0);
1007
+ *
1008
+ * const { errors, parsed } = await numberSchema.parse(undefined);
1009
+ *
1010
+ * console.log(parsed); // 0
1011
+ * ```
1012
+ */
1013
+ default(defaultValueOrFunction) {
1014
+ const isFunction = typeof defaultValueOrFunction === "function";
1015
+ if (isFunction) this.__defaultFunction = defaultValueOrFunction;
1016
+ else this.__defaultFunction = async () => defaultValueOrFunction;
1017
+ return this;
1018
+ }
1019
+ /**
1020
+ * This function let's you customize the schema your own way. After we translate the schema on the adapter we call
1021
+ * this function to let you customize the custom schema your own way. Our API does not support passthrough?
1022
+ * No problem, you can use this function to customize the zod schema.
1023
+ *
1024
+ * @example
1025
+ * ```typescript
1026
+ * import * as p from '@palmares/schemas';
1027
+ *
1028
+ * const numberSchema = p.number().extends((schema) => {
1029
+ * return schema.nonnegative();
1030
+ * });
1031
+ *
1032
+ * const { errors, parsed } = await numberSchema.parse(-1);
1033
+ *
1034
+ * console.log(errors);
1035
+ * // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
1036
+ * ```
1037
+ *
1038
+ * @param callback - The callback that will be called to customize the schema.
1039
+ * @param toStringCallback - The callback that will be called to transform the schema to a string when you want
1040
+ * to compile the underlying schema to a string so you can save it for future runs.
1041
+ *
1042
+ * @returns The schema.
1043
+ */
1044
+ extends(callback, toStringCallback) {
1045
+ this.__extends = {
1046
+ callback,
1047
+ toStringCallback
1048
+ };
1049
+ return this;
1050
+ }
1051
+ /**
1052
+ * This function is used to transform the value to the representation of the schema. When using the {@link data}
1053
+ * function. With this function you have full control to add data cleaning for example, transforming the data
1054
+ * and whatever. Another use case is when you want to return deeply nested recursive data.
1055
+ * The schema maps to itself.
1056
+ *
1057
+ * @example
1058
+ * ```typescript
1059
+ * import * as p from '@palmares/schemas';
1060
+ *
1061
+ * const recursiveSchema = p.object({
1062
+ * id: p.number().optional(),
1063
+ * name: p.string(),
1064
+ * }).toRepresentation(async (value) => {
1065
+ * return {
1066
+ * id: value.id,
1067
+ * name: value.name,
1068
+ * children: await Promise.all(value.children.map(async (child) => await recursiveSchema.data(child)))
1069
+ * }
1070
+ * });
1071
+ *
1072
+ * const data = await recursiveSchema.data({
1073
+ * id: 1,
1074
+ * name: 'John Doe',
1075
+ * });
1076
+ * ```
1077
+ *
1078
+ * @example
1079
+ * ```
1080
+ * import * as p from '@palmares/schemas';
1081
+ *
1082
+ * const colorToRGBSchema = p.string().toRepresentation(async (value) => {
1083
+ * switch (value) {
1084
+ * case 'red': return { r: 255, g: 0, b: 0 };
1085
+ * case 'green': return { r: 0, g: 255, b: 0 };
1086
+ * case 'blue': return { r: 0, g: 0, b: 255 };
1087
+ * default: return { r: 0, g: 0, b: 0 };
1088
+ * }
1089
+ * });
1090
+ * ```
1091
+ * @param toRepresentationCallback - The callback that will be called to transform the value to the representation.
1092
+ * @param options - Options for the toRepresentation function.
1093
+ * @param options.after - Whether the toRepresentationCallback should be called after the existing
1094
+ * toRepresentationCallback. Defaults to true.
1095
+ * @param options.before - Whether the toRepresentationCallback should be called before the existing
1096
+ * toRepresentationCallback. Defaults to true.
1097
+ *
1098
+ * @returns The schema with a new return type
1099
+ */
1100
+ toRepresentation(toRepresentationCallback, options) {
1101
+ if (this.__toRepresentation) {
1102
+ const before = typeof options?.before === "boolean" ? options.before : typeof options?.after === "boolean" ? !options.after : true;
1103
+ const existingToRepresentation = this.__toRepresentation;
1104
+ this.__toRepresentation = async (value) => {
1105
+ if (before) return toRepresentationCallback(await existingToRepresentation(value));
1106
+ else return existingToRepresentation(await toRepresentationCallback(value));
1107
+ };
1108
+ } else this.__toRepresentation = toRepresentationCallback;
1109
+ return this;
1110
+ }
1111
+ /**
1112
+ * This function is used to transform the value to the internal representation of the schema. This is useful
1113
+ * when you want to transform the value to a type that the schema adapter can understand. For example, you
1114
+ * might want to transform a string to a date. This is the function you use.
1115
+ *
1116
+ * @example
1117
+ * ```typescript
1118
+ * import * as p from '@palmares/schemas';
1119
+ *
1120
+ * const dateSchema = p.string().toInternal((value) => {
1121
+ * return new Date(value);
1122
+ * });
1123
+ *
1124
+ * const date = await dateSchema.parse('2021-01-01');
1125
+ *
1126
+ * console.log(date); // Date object
1127
+ *
1128
+ * const rgbToColorSchema = p.object({
1129
+ * r: p.number().min(0).max(255),
1130
+ * g: p.number().min(0).max(255),
1131
+ * b: p.number().min(0).max(255),
1132
+ * }).toInternal(async (value) => {
1133
+ * if (value.r === 255 && value.g === 0 && value.b === 0) return 'red';
1134
+ * if (value.r === 0 && value.g === 255 && value.b === 0) return 'green';
1135
+ * if (value.r === 0 && value.g === 0 && value.b === 255) return 'blue';
1136
+ * return `rgb(${value.r}, ${value.g}, ${value.b})`;
1137
+ * });
1138
+ * ```
1139
+ *
1140
+ * @param toInternalCallback - The callback that will be called to transform the value to the internal representation.
1141
+ *
1142
+ * @returns The schema with a new return type.
1143
+ */
1144
+ toInternal(toInternalCallback) {
1145
+ if (this.__toInternal) {
1146
+ const toInternal = this.__toInternal;
1147
+ this.__toInternal = async (value) => {
1148
+ const newValue = await toInternal(value);
1149
+ return toInternalCallback(newValue);
1150
+ };
1151
+ } else this.__toInternal = toInternalCallback;
1152
+ return this;
1153
+ }
1154
+ /**
1155
+ * Called before the validation of the schema. Let's say that you want to validate a date that might receive a
1156
+ * string, you can convert that string to a date here BEFORE the validation. This pretty much transforms the value
1157
+ * to a type that the schema adapter can understand.
1158
+ *
1159
+ * @example
1160
+ * ```
1161
+ * import * as p from '@palmares/schemas';
1162
+ * import * as z from 'zod';
1163
+ *
1164
+ * const customRecordToMapSchema = p.schema().appendSchema(z.map()).toValidate(async (value) => {
1165
+ * return new Map(value); // Before validating we transform the value to a map.
1166
+ * });
1167
+ *
1168
+ * const { errors, parsed } = await customRecordToMapSchema.parse({ key: 'value' });
1169
+ * ```
1170
+ *
1171
+ * @param toValidateCallback - The callback that will be called to validate the value.
1172
+ *
1173
+ * @returns The schema with a new return type.
1174
+ */
1175
+ toValidate(toValidateCallback) {
1176
+ this.__toValidate = toValidateCallback;
1177
+ return this;
1178
+ }
1179
+ /**
1180
+ * Used to transform the given schema on a stringfied version of the adapter.
1181
+ */
1182
+ async compile(adapter) {
1183
+ const data = await this.__transformToAdapter({
1184
+ shouldAddStringVersion: true,
1185
+ force: true
1186
+ });
1187
+ const stringVersions = data.map((value) => value.asString);
1188
+ return stringVersions;
1189
+ }
1190
+ static new(..._args) {
1191
+ const result = new _Schema();
1192
+ return result;
1193
+ }
1194
+ };
1195
+ var schema = Schema.new;
1196
+
1197
+ // src/validators/string.ts
1198
+ function stringValidation() {
1199
+ return {
1200
+ name: "string",
1201
+ type: "medium",
1202
+ // eslint-disable-next-line ts/require-await
1203
+ callback: /* @__PURE__ */ __name(async (value, path, _options) => {
1204
+ return {
1205
+ parsed: value,
1206
+ errors: typeof value === "string" ? [] : [
1207
+ {
1208
+ isValid: typeof value === "string",
1209
+ code: "string",
1210
+ // eslint-disable-next-line ts/no-unnecessary-condition
1211
+ path: path || [],
1212
+ message: "The value must be a string. Received: " + typeof value
1213
+ }
1214
+ ]
1215
+ };
1216
+ }, "callback")
1217
+ };
1218
+ }
1219
+ __name(stringValidation, "stringValidation");
1220
+ function maxLength(args) {
1221
+ return {
1222
+ name: "maxLength",
1223
+ type: "low",
1224
+ // eslint-disable-next-line ts/require-await
1225
+ callback: /* @__PURE__ */ __name(async (value, path, _options) => {
1226
+ const isValid = value.length <= args.value;
1227
+ return {
1228
+ parsed: value,
1229
+ errors: isValid ? [] : [
1230
+ {
1231
+ isValid: false,
1232
+ code: "maxLength",
1233
+ // eslint-disable-next-line ts/no-unnecessary-condition
1234
+ path: path || [],
1235
+ message: args.message
1236
+ }
1237
+ ]
1238
+ };
1239
+ }, "callback")
1240
+ };
1241
+ }
1242
+ __name(maxLength, "maxLength");
1243
+ function minLength(args) {
1244
+ return {
1245
+ name: "minLength",
1246
+ type: "low",
1247
+ // eslint-disable-next-line ts/require-await
1248
+ callback: /* @__PURE__ */ __name(async (value, path, _options) => {
1249
+ const isValid = value.length >= args.value;
1250
+ return {
1251
+ parsed: value,
1252
+ errors: isValid ? [] : [
1253
+ {
1254
+ isValid: false,
1255
+ code: "minLength",
1256
+ // eslint-disable-next-line ts/no-unnecessary-condition
1257
+ path: path || [],
1258
+ message: args.message
1259
+ }
1260
+ ]
1261
+ };
1262
+ }, "callback")
1263
+ };
1264
+ }
1265
+ __name(minLength, "minLength");
1266
+ function endsWith(args) {
1267
+ return {
1268
+ name: "endsWith",
1269
+ type: "low",
1270
+ // eslint-disable-next-line ts/require-await
1271
+ callback: /* @__PURE__ */ __name(async (value, path, _options) => {
1272
+ const isValid = value.endsWith(args.value);
1273
+ return {
1274
+ parsed: value,
1275
+ errors: isValid ? [] : [
1276
+ {
1277
+ isValid: false,
1278
+ code: "endsWith",
1279
+ // eslint-disable-next-line ts/no-unnecessary-condition
1280
+ path: path || [],
1281
+ message: args.message
1282
+ }
1283
+ ]
1284
+ };
1285
+ }, "callback")
1286
+ };
1287
+ }
1288
+ __name(endsWith, "endsWith");
1289
+ function startsWith(args) {
1290
+ return {
1291
+ name: "startsWith",
1292
+ type: "low",
1293
+ // eslint-disable-next-line ts/require-await
1294
+ callback: /* @__PURE__ */ __name(async (value, path, _options) => {
1295
+ const isValid = value.startsWith(args.value);
1296
+ return {
1297
+ parsed: value,
1298
+ errors: isValid ? [] : [
1299
+ {
1300
+ isValid: false,
1301
+ code: "startsWith",
1302
+ // eslint-disable-next-line ts/no-unnecessary-condition
1303
+ path: path || [],
1304
+ message: args.message
1305
+ }
1306
+ ]
1307
+ };
1308
+ }, "callback")
1309
+ };
1310
+ }
1311
+ __name(startsWith, "startsWith");
1312
+ function includes(args) {
1313
+ return {
1314
+ name: "includes",
1315
+ type: "low",
1316
+ // eslint-disable-next-line ts/require-await
1317
+ callback: /* @__PURE__ */ __name(async (value, path, _options) => {
1318
+ const isValid = value.includes(args.value);
1319
+ return {
1320
+ parsed: value,
1321
+ errors: isValid ? [] : [
1322
+ {
1323
+ isValid: false,
1324
+ code: "includes",
1325
+ // eslint-disable-next-line ts/no-unnecessary-condition
1326
+ path: path || [],
1327
+ message: args.message
1328
+ }
1329
+ ]
1330
+ };
1331
+ }, "callback")
1332
+ };
1333
+ }
1334
+ __name(includes, "includes");
1335
+ function regex(args) {
1336
+ return {
1337
+ name: "regex",
1338
+ type: "low",
1339
+ // eslint-disable-next-line ts/require-await
1340
+ callback: /* @__PURE__ */ __name(async (value, path, _options) => {
1341
+ const isValid = args.value.test(value);
1342
+ return {
1343
+ parsed: value,
1344
+ errors: isValid ? [] : [
1345
+ {
1346
+ isValid: false,
1347
+ code: "regex",
1348
+ // eslint-disable-next-line ts/no-unnecessary-condition
1349
+ path: path || [],
1350
+ message: args.message
1351
+ }
1352
+ ]
1353
+ };
1354
+ }, "callback")
1355
+ };
1356
+ }
1357
+ __name(regex, "regex");
1358
+ function uuid(args) {
1359
+ const uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
1360
+ return {
1361
+ name: "uuid",
1362
+ type: "low",
1363
+ // eslint-disable-next-line ts/require-await
1364
+ callback: /* @__PURE__ */ __name(async (value, path, _options) => {
1365
+ const isValid = uuidRegex.test(value);
1366
+ return {
1367
+ parsed: value,
1368
+ errors: isValid ? [] : [
1369
+ {
1370
+ isValid: false,
1371
+ code: "uuid",
1372
+ // eslint-disable-next-line ts/no-unnecessary-condition
1373
+ path: path || [],
1374
+ message: args.message
1375
+ }
1376
+ ]
1377
+ };
1378
+ }, "callback")
1379
+ };
1380
+ }
1381
+ __name(uuid, "uuid");
1382
+ function email(args) {
1383
+ const emailRegex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
1384
+ console.log("aquiiiiii");
1385
+ return {
1386
+ name: "email",
1387
+ type: "low",
1388
+ // eslint-disable-next-line ts/require-await
1389
+ callback: /* @__PURE__ */ __name(async (value, path, _options) => {
1390
+ const isValid = emailRegex.test(value);
1391
+ return {
1392
+ parsed: value,
1393
+ errors: isValid ? [] : [
1394
+ {
1395
+ isValid: false,
1396
+ code: "email",
1397
+ // eslint-disable-next-line ts/no-unnecessary-condition
1398
+ path: path || [],
1399
+ message: args.message
1400
+ }
1401
+ ]
1402
+ };
1403
+ }, "callback")
1404
+ };
1405
+ }
1406
+ __name(email, "email");
1407
+
1408
+ // src/schema/string.ts
1409
+ var StringSchema = class _StringSchema extends Schema {
1410
+ static {
1411
+ __name(this, "StringSchema");
1412
+ }
1413
+ fieldType = "string";
1414
+ __is;
1415
+ __email;
1416
+ __uuid;
1417
+ __minLength;
1418
+ __maxLength;
1419
+ __regex;
1420
+ __endsWith;
1421
+ __startsWith;
1422
+ __includes;
1423
+ __type = {
1424
+ message: "Invalid type",
1425
+ check: /* @__PURE__ */ __name((value) => {
1426
+ return typeof value === "string";
1427
+ }, "check")
1428
+ };
1429
+ async __transformToAdapter(options) {
1430
+ return defaultTransformToAdapter(async (adapter) => {
1431
+ return defaultTransform("string", this, adapter, adapter.string, () => ({
1432
+ type: this.__type,
1433
+ is: this.__is,
1434
+ email: this.__email,
1435
+ uuid: this.__uuid,
1436
+ minLength: this.__minLength,
1437
+ maxLength: this.__maxLength,
1438
+ regex: this.__regex,
1439
+ endsWith: this.__endsWith,
1440
+ startsWith: this.__startsWith,
1441
+ includes: this.__includes,
1442
+ nullable: this.__nullable,
1443
+ optional: this.__optional,
1444
+ parsers: {
1445
+ nullable: this.__nullable.allow,
1446
+ optional: this.__optional.allow
1447
+ }
1448
+ }), {
1449
+ maxLength,
1450
+ minLength,
1451
+ endsWith,
1452
+ startsWith,
1453
+ email,
1454
+ uuid,
1455
+ is,
1456
+ regex,
1457
+ includes,
1458
+ nullable,
1459
+ optional
1460
+ }, {
1461
+ validatorsIfFallbackOrNotSupported: stringValidation(),
1462
+ shouldAddStringVersion: options.shouldAddStringVersion,
1463
+ // eslint-disable-next-line ts/require-await
1464
+ fallbackIfNotSupported: /* @__PURE__ */ __name(async () => {
1465
+ return [];
1466
+ }, "fallbackIfNotSupported")
1467
+ });
1468
+ }, this, this.__transformedSchemas, options, "number");
1469
+ }
1470
+ /**
1471
+ * This let's you refine the schema with custom validations. This is useful when you want to validate something that
1472
+ * is not supported by default by the schema adapter.
1473
+ *
1474
+ * @example
1475
+ * ```typescript
1476
+ * import * as p from '@palmares/schemas';
1477
+ *
1478
+ * const numberSchema = p.number().refine((value) => {
1479
+ * if (value < 0) return { code: 'invalid_number', message: 'The number should be greater than 0' };
1480
+ * });
1481
+ *
1482
+ * const { errors, parsed } = await numberSchema.parse(-1);
1483
+ *
1484
+ * // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
1485
+ * console.log(errors);
1486
+ * ```
1487
+ *
1488
+ * @param refinementCallback - The callback that will be called to validate the value.
1489
+ * @param options - Options for the refinement.
1490
+ * @param options.isAsync - Whether the callback is async or not. Defaults to true.
1491
+ */
1492
+ refine(refinementCallback) {
1493
+ return super.refine(refinementCallback);
1494
+ }
1495
+ /**
1496
+ * Allows the value to be either undefined or null.
1497
+ *
1498
+ * @example
1499
+ * ```typescript
1500
+ * import * as p from '@palmares/schemas';
1501
+ *
1502
+ * const numberSchema = p.number().optional();
1503
+ *
1504
+ * const { errors, parsed } = await numberSchema.parse(undefined);
1505
+ *
1506
+ * console.log(parsed); // undefined
1507
+ *
1508
+ * const { errors, parsed } = await numberSchema.parse(null);
1509
+ *
1510
+ * console.log(parsed); // null
1511
+ *
1512
+ * const { errors, parsed } = await numberSchema.parse(1);
1513
+ *
1514
+ * console.log(parsed); // 1
1515
+ * ```
1516
+ *
1517
+ * @returns - The schema we are working with.
1518
+ */
1519
+ optional() {
1520
+ return super.optional();
1521
+ }
1522
+ /**
1523
+ * Just adds a message when the value is undefined. It's just a syntax sugar for
1524
+ *
1525
+ * ```typescript
1526
+ * p.string().optional({ message: 'This value cannot be null', allow: false })
1527
+ * ```
1528
+ *
1529
+ * @param options - The options of nonOptional function
1530
+ * @param options.message - A custom message if the value is undefined.
1531
+ *
1532
+ * @returns - The schema.
1533
+ */
1534
+ nonOptional(options) {
1535
+ return super.optional({
1536
+ message: options?.message,
1537
+ allow: false
1538
+ });
1539
+ }
1540
+ /**
1541
+ * Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
1542
+ * is NULL by setting the { message: 'Your custom message', allow: false } on the options.
1543
+ *
1544
+ * @example
1545
+ * ```typescript
1546
+ * import * as p from '@palmares/schemas';
1547
+ *
1548
+ * const numberSchema = p.number().nullable();
1549
+ *
1550
+ * const { errors, parsed } = await numberSchema.parse(null);
1551
+ *
1552
+ * console.log(parsed); // null
1553
+ *
1554
+ * const { errors, parsed } = await numberSchema.parse(undefined);
1555
+ *
1556
+ * console.log(errors); // [{ isValid: false, code: 'invalid_type', message: 'Invalid type', path: [] }]
1557
+ * ```
1558
+ *
1559
+ * @param options - The options for the nullable function.
1560
+ * @param options.message - The message to be shown when the value is not null. Defaults to 'Cannot be null'.
1561
+ * @param options.allow - Whether the value can be null or not. Defaults to true.
1562
+ *
1563
+ * @returns The schema.
1564
+ */
1565
+ nullable(options) {
1566
+ return super.nullable(options);
1567
+ }
1568
+ /**
1569
+ * Just adds a message when the value is null. It's just a syntax sugar for
1570
+ *
1571
+ * ```typescript
1572
+ * p.string().nullable({ message: 'This value cannot be null', allow: false })
1573
+ * ```
1574
+ *
1575
+ * @param options - The options of nonNullable function
1576
+ * @param options.message - A custom message if the value is null.
1577
+ *
1578
+ * @returns - The schema.
1579
+ */
1580
+ nonNullable(options) {
1581
+ return super.nullable({
1582
+ message: options?.message || "",
1583
+ allow: false
1584
+ });
1585
+ }
1586
+ /**
1587
+ * This method will remove the value from the representation of the schema. If the value is undefined it will keep
1588
+ * that way otherwise it will set the value to undefined after it's validated.
1589
+ * This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
1590
+ * function. This will remove the value from the representation of the schema.
1591
+ *
1592
+ * By default, the value will be removed just from the representation, in other words, when you call the {@link data}
1593
+ * function. But if you want to remove the value from the internal representation, you can pass the argument
1594
+ * `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
1595
+ * the argument `toRepresentation` as true as well.
1596
+ *
1597
+ * @example
1598
+ * ```typescript
1599
+ * import * as p from '@palmares/schemas';
1600
+ *
1601
+ * const userSchema = p.object({
1602
+ * id: p.number().optional(),
1603
+ * name: p.string(),
1604
+ * password: p.string().omit()
1605
+ * });
1606
+ *
1607
+ * const user = await userSchema.data({
1608
+ * id: 1,
1609
+ * name: 'John Doe',
1610
+ * password: '123456'
1611
+ * });
1612
+ *
1613
+ * console.log(user); // { id: 1, name: 'John Doe' }
1614
+ * ```
1615
+ *
1616
+ *
1617
+ * @param args - By default, the value will be removed just from the representation, in other words, when you call
1618
+ * the {@link data} function. But if you want to remove the value from the internal representation, you can pass the
1619
+ * argument `toInternal` as true. Then if you still want to remove the value from the representation, you will need
1620
+ * to pass the argument `toRepresentation` as true as well.
1621
+ *
1622
+ * @returns The schema.
1623
+ */
1624
+ omit(args) {
1625
+ return super.omit(args);
1626
+ }
1627
+ /**
1628
+ * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
1629
+ * source like a database. You should always return the schema after you save the value, that way we will always have
1630
+ * the correct type of the schema after the save operation.
1631
+ *
1632
+ * You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
1633
+ *
1634
+ * @example
1635
+ * ```typescript
1636
+ * import * as p from '@palmares/schemas';
1637
+ *
1638
+ * import { User } from './models';
1639
+ *
1640
+ * const userSchema = p.object({
1641
+ * id: p.number().optional(),
1642
+ * name: p.string(),
1643
+ * email: p.string().email(),
1644
+ * }).onSave(async (value) => {
1645
+ * // Create or update the user on the database using palmares models or any other library of your choice.
1646
+ * if (value.id)
1647
+ * await User.default.set(value, { search: { id: value.id } });
1648
+ * else
1649
+ * await User.default.set(value);
1650
+ *
1651
+ * return value;
1652
+ * });
1653
+ *
1654
+ *
1655
+ * // Then, on your controller, do something like this:
1656
+ * const { isValid, save, errors } = await userSchema.validate(req.body);
1657
+ * if (isValid) {
1658
+ * const savedValue = await save();
1659
+ * return Response.json(savedValue, { status: 201 });
1660
+ * }
1661
+ *
1662
+ * return Response.json({ errors }, { status: 400 });
1663
+ * ```
1664
+ *
1665
+ * @param callback - The callback that will be called to save the value on an external source.
1666
+ *
1667
+ * @returns The schema.
1668
+ */
1669
+ onSave(callback) {
1670
+ return super.onSave(callback);
1671
+ }
1672
+ /**
1673
+ * This function is used to add a default value to the schema. If the value is either undefined or null, the default
1674
+ * value will be used.
1675
+ *
1676
+ * @example
1677
+ * ```typescript
1678
+ * import * as p from '@palmares/schemas';
1679
+ *
1680
+ * const numberSchema = p.number().default(0);
1681
+ *
1682
+ * const { errors, parsed } = await numberSchema.parse(undefined);
1683
+ *
1684
+ * console.log(parsed); // 0
1685
+ * ```
1686
+ */
1687
+ default(defaultValueOrFunction) {
1688
+ return super.default(defaultValueOrFunction);
1689
+ }
1690
+ /**
1691
+ * This function let's you customize the schema your own way. After we translate the schema on the adapter we call
1692
+ * this function to let you customize the custom schema your own way. Our API does not support passthrough?
1693
+ * No problem, you can use this function to customize the zod schema.
1694
+ *
1695
+ * @example
1696
+ * ```typescript
1697
+ * import * as p from '@palmares/schemas';
1698
+ *
1699
+ * const numberSchema = p.number().extends((schema) => {
1700
+ * return schema.nonnegative();
1701
+ * });
1702
+ *
1703
+ * const { errors, parsed } = await numberSchema.parse(-1);
1704
+ * // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
1705
+ * console.log(errors);
1706
+ * ```
1707
+ *
1708
+ * @param callback - The callback that will be called to customize the schema.
1709
+ * @param toStringCallback - The callback that will be called to transform the schema to a string when you want to
1710
+ * compile the underlying schema to a string so you can save it for future runs.
1711
+ *
1712
+ * @returns The schema.
1713
+ */
1714
+ extends(callback, toStringCallback) {
1715
+ return super.extends(callback, toStringCallback);
1716
+ }
1717
+ /**
1718
+ * This function is used to transform the value to the representation of the schema. When using the {@link data}
1719
+ * function. With this function you have full control to add data cleaning for example, transforming the data and
1720
+ * whatever. Another use case is when you want to return deeply nested recursive data.
1721
+ * The schema maps to itself.
1722
+ *
1723
+ * @example
1724
+ * ```typescript
1725
+ * import * as p from '@palmares/schemas';
1726
+ *
1727
+ * const recursiveSchema = p.object({
1728
+ * id: p.number().optional(),
1729
+ * name: p.string(),
1730
+ * }).toRepresentation(async (value) => {
1731
+ * return {
1732
+ * id: value.id,
1733
+ * name: value.name,
1734
+ * children: await Promise.all(value.children.map(async (child) => await recursiveSchema.data(child)))
1735
+ * }
1736
+ * });
1737
+ *
1738
+ * const data = await recursiveSchema.data({
1739
+ * id: 1,
1740
+ * name: 'John Doe',
1741
+ * });
1742
+ * ```
1743
+ *
1744
+ * @example
1745
+ * ```
1746
+ * import * as p from '@palmares/schemas';
1747
+ *
1748
+ * const colorToRGBSchema = p.string().toRepresentation(async (value) => {
1749
+ * switch (value) {
1750
+ * case 'red': return { r: 255, g: 0, b: 0 };
1751
+ * case 'green': return { r: 0, g: 255, b: 0 };
1752
+ * case 'blue': return { r: 0, g: 0, b: 255 };
1753
+ * default: return { r: 0, g: 0, b: 0 };
1754
+ * }
1755
+ * });
1756
+ * ```
1757
+ * @param toRepresentationCallback - The callback that will be called to transform the value to the representation.
1758
+ *
1759
+ * @returns The schema with a new return type
1760
+ */
1761
+ toRepresentation(toRepresentationCallback) {
1762
+ return super.toRepresentation(toRepresentationCallback);
1763
+ }
1764
+ /**
1765
+ * This function is used to transform the value to the internal representation of the schema. This is useful when
1766
+ * you want to transform the value to a type that the schema adapter can understand. For example, you might want
1767
+ * to transform a string to a date. This is the function you use.
1768
+ *
1769
+ * @example
1770
+ * ```typescript
1771
+ * import * as p from '@palmares/schemas';
1772
+ *
1773
+ * const dateSchema = p.string().toInternal((value) => {
1774
+ * return new Date(value);
1775
+ * });
1776
+ *
1777
+ * const date = await dateSchema.parse('2021-01-01');
1778
+ *
1779
+ * console.log(date); // Date object
1780
+ *
1781
+ * const rgbToColorSchema = p.object({
1782
+ * r: p.number().min(0).max(255),
1783
+ * g: p.number().min(0).max(255),
1784
+ * b: p.number().min(0).max(255),
1785
+ * }).toInternal(async (value) => {
1786
+ * if (value.r === 255 && value.g === 0 && value.b === 0) return 'red';
1787
+ * if (value.r === 0 && value.g === 255 && value.b === 0) return 'green';
1788
+ * if (value.r === 0 && value.g === 0 && value.b === 255) return 'blue';
1789
+ * return `rgb(${value.r}, ${value.g}, ${value.b})`;
1790
+ * });
1791
+ * ```
1792
+ *
1793
+ * @param toInternalCallback - The callback that will be called to transform the value to the internal representation.
1794
+ *
1795
+ * @returns The schema with a new return type.
1796
+ */
1797
+ toInternal(toInternalCallback) {
1798
+ return super.toInternal(toInternalCallback);
1799
+ }
1800
+ /**
1801
+ * Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
1802
+ * you can convert that string to a date here BEFORE the validation. This pretty much transforms the value to a
1803
+ * type that the schema adapter can understand.
1804
+ *
1805
+ * @example
1806
+ * ```typescript
1807
+ * import * as p from '@palmares/schemas';
1808
+ * import * as z from 'zod';
1809
+ *
1810
+ * const customRecordToMapSchema = p.schema().appendSchema(z.map()).toValidate(async (value) => {
1811
+ * return new Map(value); // Before validating we transform the value to a map.
1812
+ * });
1813
+ *
1814
+ * const { errors, parsed } = await customRecordToMapSchema.parse({ key: 'value' });
1815
+ * ```
1816
+ *
1817
+ * @param toValidateCallback - The callback that will be called to validate the value.
1818
+ *
1819
+ * @returns The schema with a new return type.
1820
+ */
1821
+ toValidate(toValidateCallback) {
1822
+ return super.toValidate(toValidateCallback);
1823
+ }
1824
+ /**
1825
+ * Defines a list of strings that are allowed, it's useful when you want to restrict the values that are allowed.
1826
+ * Like a selector or a Choice field.
1827
+ *
1828
+ * @example
1829
+ * ```typescript
1830
+ * import * as p from '@palmares/schema';
1831
+ *
1832
+ * const schema = p.string().is(['Argentina', 'Brazil', 'Chile']);
1833
+ *
1834
+ * schema.parse('Argentina'); // { errors: [], parsed: 'Argentina' }
1835
+ * // { errors: [{
1836
+ * // code: 'invalid_value',
1837
+ * // message: 'The value should be equal to Argentina, Brazil, Chile',
1838
+ * // path: [] }], parsed: 'Uruguay' }
1839
+ * schema.parse('Uruguay');
1840
+ * ```
1841
+ *
1842
+ * @param value - The list of numbers that are allowed
1843
+ *
1844
+ * @returns - The schema instance
1845
+ */
1846
+ is(value, options) {
1847
+ this.__is = {
1848
+ value,
1849
+ message: typeof options?.message === "string" ? options.message : `The value should be equal to ${value.join(", ")}`
1850
+ };
1851
+ return this;
1852
+ }
1853
+ /**
1854
+ * Validates if the string ends with a specific value.
1855
+ *
1856
+ * @example
1857
+ * ```typescript
1858
+ * import * as p from '@palmares/schema';
1859
+ *
1860
+ * const schema = p.string().endsWith('.com');
1861
+ *
1862
+ * schema.parse('example.com'); // { errors: [], parsed: 'example.com' }
1863
+ *
1864
+ * // { errors: [{ code: 'endsWith', message: 'The value should end with .com', path: [] }], parsed: 'example.org' }
1865
+ * schema.parse('example.org');
1866
+ * ```
1867
+ *
1868
+ * @param value - The value that the string should end with.
1869
+ * @param options - The options for the endsWith function.
1870
+ * @param options.message - The message to be shown when the value does not end with the value.
1871
+ *
1872
+ * @returns - The schema instance.
1873
+ */
1874
+ endsWith(value, options) {
1875
+ this.__endsWith = {
1876
+ value,
1877
+ message: options?.message || `The value should end with ${value}`
1878
+ };
1879
+ return this;
1880
+ }
1881
+ /**
1882
+ * Validates if the string starts with a specific value.
1883
+ *
1884
+ * @example
1885
+ * ```typescript
1886
+ * import * as p from '@palmares/schema';
1887
+ *
1888
+ * const schema = p.string().startsWith('https://');
1889
+ *
1890
+ * schema.parse('https://example.com'); // { errors: [], parsed: 'https://example.com' }
1891
+ * // {
1892
+ * // errors: [{ code: 'startsWith', message: 'The value should start with https://', path: [] }],
1893
+ * // parsed: 'http://example.com'
1894
+ * // }
1895
+ * schema.parse('http://example.com');
1896
+ * ```
1897
+ *
1898
+ * @param value - The value that the string should start with.
1899
+ * @param options - The options for the startsWith function.
1900
+ * @param options.message - The message to be shown when the value does not start with the value.
1901
+ *
1902
+ * @returns - The schema instance.
1903
+ */
1904
+ startsWith(value, options) {
1905
+ this.__startsWith = {
1906
+ value,
1907
+ message: options?.message || `The value should start with ${value}`
1908
+ };
1909
+ return this;
1910
+ }
1911
+ /**
1912
+ * Checks if the string includes a specific substring.
1913
+ *
1914
+ * @example
1915
+ * ```typescript
1916
+ * import * as p from '@palmares/schema';
1917
+ *
1918
+ * const schema = p.string().includes('for babies');
1919
+ *
1920
+ * schema.parse('Computer graphics for babies'); // { errors: [], parsed: 'Computer graphics for babies' }
1921
+ * // {
1922
+ * // errors: [{
1923
+ * // code: 'includes',
1924
+ * // message: 'The string value should include the following substring 'for babies',
1925
+ * // path: []
1926
+ * // }], parsed: 'example.org' }
1927
+ * schema.parse('Learn javascript as you were 5');
1928
+ * ```
1929
+ *
1930
+ * @param value - The value that the string should include.
1931
+ * @param options - The options for the includes function.
1932
+ * @param options.message - The message to be shown when the value does not include the value.
1933
+ *
1934
+ * @returns - The schema instance.
1935
+ */
1936
+ includes(value, options) {
1937
+ this.__includes = {
1938
+ value,
1939
+ message: options?.message || `The string value should include the following substring '${value}'`
1940
+ };
1941
+ return this;
1942
+ }
1943
+ /**
1944
+ * Validates if the string matches a specific regex.
1945
+ *
1946
+ * @example
1947
+ * ```typescript
1948
+ * import * as p from '@palmares/schema';
1949
+ *
1950
+ * const schema = p.string().regex(/^[a-z]+$/);
1951
+ *
1952
+ * schema.parse('abc'); // { errors: [], parsed: 'abc' }
1953
+ * // {
1954
+ * // errors: [{
1955
+ * // code: 'regex',
1956
+ * // message: 'The value should match the following regex /^[a-z]+$/',
1957
+ * // path: []
1958
+ * // }],
1959
+ * // parsed: '123'
1960
+ * // }
1961
+ * schema.parse('123');
1962
+ * ```
1963
+ *
1964
+ * @param value - The regex that the string should match.
1965
+ * @param options - The options for the regex function.
1966
+ * @param options.message - The message to be shown when the value does not match the regex.
1967
+ *
1968
+ * @returns - The schema instance.
1969
+ */
1970
+ regex(value, options) {
1971
+ this.__regex = {
1972
+ value,
1973
+ message: options?.message || `The value should match the following regex '${value.toString()}'`
1974
+ };
1975
+ return this;
1976
+ }
1977
+ /**
1978
+ * Validates if the string has a maximum length. Use { inclusive: true } to allow the value to have the same length
1979
+ * as the maximum length.
1980
+ *
1981
+ * @example
1982
+ * ```typescript
1983
+ * import * as p from '@palmares/schema';
1984
+ *
1985
+ * const schema = p.string().maxLength(5);
1986
+ *
1987
+ * schema.parse('12345'); // { errors: [], parsed: '12345' }
1988
+ * // {
1989
+ * // errors: [{ code: 'maxLength', message: 'The value should have a maximum length of 5', path: [] }],
1990
+ * // parsed: '123
1991
+ * // }
1992
+ * schema.parse('123456');
1993
+ * ```
1994
+ *
1995
+ * @param value - The maximum length that the string should have.
1996
+ * @param options - The options for the maxLength function.
1997
+ * @param options.message - The message to be shown when the value has a length greater than the maximum length.
1998
+ * @param options.inclusive - Whether the value can have the same length as the maximum length. Defaults to false.
1999
+ *
2000
+ * @returns - The schema instance.
2001
+ */
2002
+ maxLength(value, options) {
2003
+ this.__maxLength = {
2004
+ value,
2005
+ message: options?.message || `The value should have a maximum length of ${value}`
2006
+ };
2007
+ return this;
2008
+ }
2009
+ /**
2010
+ * Validates if the string has a minimum length. Use { inclusive: true } to allow the value to have the same length
2011
+ * as the minimum length.
2012
+ *
2013
+ * @example
2014
+ * ```typescript
2015
+ * import * as p from '@palmares/schema';
2016
+ *
2017
+ * const schema = p.string().minLength(5);
2018
+ *
2019
+ * schema.parse('12345'); // { errors: [], parsed: '12345' }
2020
+ * // {
2021
+ * // errors: [{ code: 'minLength', message: 'The value should have a minimum length of 5', path: [] }],
2022
+ * // parsed: '1234'
2023
+ * // }
2024
+ * schema.parse('1234');
2025
+ * ```
2026
+ *
2027
+ * @param value - The minimum length that the string should have.
2028
+ * @param options - The options for the minLength function.
2029
+ * @param options.message - The message to be shown when the value has a length less than the minimum length.
2030
+ * @param options.inclusive - Whether the value can have the same length as the minimum length. Defaults to false.
2031
+ *
2032
+ * @returns - The schema instance.
2033
+ */
2034
+ minLength(value, options) {
2035
+ this.__minLength = {
2036
+ value,
2037
+ message: options?.message || `The value should have a minimum length of ${value}`
2038
+ };
2039
+ return this;
2040
+ }
2041
+ /**
2042
+ * Validates if the string is a valid UUID.
2043
+ *
2044
+ * @example
2045
+ * ```typescript
2046
+ * import * as p from '@palmares/schema';
2047
+ *
2048
+ * const schema = p.string().uuid();
2049
+ *
2050
+ * // { errors: [], parsed: '550e8400-e29b-41d4-a716-446655440000' }
2051
+ * schema.parse('550e8400-e29b-41d4-a716-446655440000');
2052
+ * ```
2053
+ *
2054
+ * @param options - The options for the uuid function.
2055
+ * @param options.message - The message to be shown when the value is not a valid UUID. Defaults to
2056
+ * 'The value should be a valid UUID'.
2057
+ *
2058
+ * @returns - The schema instance.
2059
+ */
2060
+ uuid(options) {
2061
+ this.__uuid = {
2062
+ message: options?.message || "The value should be a valid UUID"
2063
+ };
2064
+ return this;
2065
+ }
2066
+ /**
2067
+ * Validates if the string is a valid email or not
2068
+ *
2069
+ * @example
2070
+ * ```typescript
2071
+ *
2072
+ * import * as p from '@palmares/schema';
2073
+ *
2074
+ * const schema = p.string().email();
2075
+ *
2076
+ * schema.parse('john.doe@example.com'); // { errors: [], parsed: 'john.doe@example.com' }
2077
+ * ```
2078
+ *
2079
+ * @param options - The options for the email function.
2080
+ * @param options.message - The message to be shown when the value is not a valid email.
2081
+ * Defaults to 'The value should be a valid email'.
2082
+ *
2083
+ * @returns - The schema instance.
2084
+ */
2085
+ email(options) {
2086
+ this.__email = {
2087
+ message: options?.message || "The value should be a valid email"
2088
+ };
2089
+ return this;
2090
+ }
2091
+ static new() {
2092
+ const returnValue = new _StringSchema();
2093
+ return returnValue;
2094
+ }
2095
+ };
2096
+ var string = StringSchema.new;
2097
+ export {
2098
+ StringSchema,
2099
+ string
2100
+ };