@palmares/schemas 0.1.25 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (305) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/dist/src/adapter/fields/array.cjs +96 -0
  3. package/dist/src/adapter/fields/array.d.ts.map +1 -0
  4. package/dist/src/adapter/fields/array.js +70 -0
  5. package/dist/src/adapter/fields/boolean.cjs +102 -0
  6. package/dist/src/adapter/fields/boolean.d.ts.map +1 -0
  7. package/dist/src/adapter/fields/boolean.js +76 -0
  8. package/dist/src/adapter/fields/datetime.cjs +102 -0
  9. package/dist/src/adapter/fields/datetime.d.ts.map +1 -0
  10. package/dist/src/adapter/fields/datetime.js +76 -0
  11. package/dist/src/adapter/fields/index.cjs +87 -0
  12. package/dist/src/adapter/fields/index.d.ts.map +1 -0
  13. package/dist/src/adapter/fields/index.js +61 -0
  14. package/dist/src/adapter/fields/number.cjs +102 -0
  15. package/dist/src/adapter/fields/number.d.ts.map +1 -0
  16. package/dist/src/adapter/fields/number.js +76 -0
  17. package/dist/src/adapter/fields/object.cjs +108 -0
  18. package/dist/src/adapter/fields/object.d.ts.map +1 -0
  19. package/dist/src/adapter/fields/object.js +82 -0
  20. package/dist/src/adapter/fields/string.cjs +102 -0
  21. package/dist/src/adapter/fields/string.d.ts.map +1 -0
  22. package/dist/src/adapter/fields/string.js +76 -0
  23. package/dist/src/adapter/fields/union.cjs +102 -0
  24. package/dist/src/adapter/fields/union.d.ts.map +1 -0
  25. package/dist/src/adapter/fields/union.js +76 -0
  26. package/dist/src/adapter/index.cjs +65 -0
  27. package/dist/src/adapter/index.d.ts.map +1 -0
  28. package/dist/src/adapter/index.js +40 -0
  29. package/dist/src/adapter/types.cjs +18 -0
  30. package/dist/src/adapter/types.d.ts.map +1 -0
  31. package/dist/src/adapter/types.js +0 -0
  32. package/dist/src/compile.cjs +59 -0
  33. package/dist/src/compile.d.ts.map +1 -0
  34. package/dist/src/compile.js +36 -0
  35. package/dist/src/conf.cjs +53 -0
  36. package/dist/src/conf.d.ts.map +1 -0
  37. package/dist/src/conf.js +27 -0
  38. package/dist/src/constants.cjs +40 -0
  39. package/dist/src/constants.d.ts.map +1 -0
  40. package/dist/src/constants.js +14 -0
  41. package/dist/src/domain.cjs +63 -0
  42. package/dist/src/domain.d.ts.map +1 -0
  43. package/dist/src/domain.js +40 -0
  44. package/dist/src/exceptions.cjs +48 -0
  45. package/dist/{cjs/types → src}/exceptions.d.ts +0 -3
  46. package/dist/src/exceptions.d.ts.map +1 -0
  47. package/dist/src/exceptions.js +24 -0
  48. package/dist/src/index.cjs +6509 -0
  49. package/dist/src/index.d.ts.map +1 -0
  50. package/dist/src/index.js +6445 -0
  51. package/dist/src/middleware.cjs +58 -0
  52. package/dist/src/middleware.d.ts.map +1 -0
  53. package/dist/src/middleware.js +35 -0
  54. package/dist/src/model.cjs +5992 -0
  55. package/dist/src/model.d.ts.map +1 -0
  56. package/dist/src/model.js +5967 -0
  57. package/dist/src/parsers/convert-from-number.cjs +34 -0
  58. package/dist/src/parsers/convert-from-number.d.ts.map +1 -0
  59. package/dist/src/parsers/convert-from-number.js +11 -0
  60. package/dist/src/parsers/convert-from-string.cjs +40 -0
  61. package/dist/src/parsers/convert-from-string.d.ts.map +1 -0
  62. package/dist/src/parsers/convert-from-string.js +17 -0
  63. package/dist/src/parsers/index.cjs +50 -0
  64. package/dist/src/parsers/index.d.ts.map +1 -0
  65. package/dist/src/parsers/index.js +24 -0
  66. package/dist/src/schema/array.cjs +1863 -0
  67. package/dist/src/schema/array.d.ts.map +1 -0
  68. package/dist/src/schema/array.js +1837 -0
  69. package/dist/src/schema/boolean.cjs +1800 -0
  70. package/dist/src/schema/boolean.d.ts.map +1 -0
  71. package/dist/src/schema/boolean.js +1774 -0
  72. package/dist/src/schema/datetime.cjs +1789 -0
  73. package/dist/src/schema/datetime.d.ts.map +1 -0
  74. package/dist/src/schema/datetime.js +1763 -0
  75. package/dist/src/schema/index.cjs +2026 -0
  76. package/dist/src/schema/index.d.ts.map +1 -0
  77. package/dist/src/schema/index.js +2001 -0
  78. package/dist/src/schema/number.cjs +2026 -0
  79. package/dist/src/schema/number.d.ts.map +1 -0
  80. package/dist/src/schema/number.js +2000 -0
  81. package/dist/src/schema/object.cjs +1795 -0
  82. package/dist/src/schema/object.d.ts.map +1 -0
  83. package/dist/src/schema/object.js +1769 -0
  84. package/dist/src/schema/schema.cjs +844 -0
  85. package/dist/src/schema/schema.d.ts.map +1 -0
  86. package/dist/src/schema/schema.js +818 -0
  87. package/dist/src/schema/string.cjs +2126 -0
  88. package/dist/src/schema/string.d.ts.map +1 -0
  89. package/dist/src/schema/string.js +2100 -0
  90. package/dist/src/schema/types.cjs +18 -0
  91. package/dist/src/schema/types.d.ts.map +1 -0
  92. package/dist/src/schema/types.js +0 -0
  93. package/dist/src/schema/union.cjs +1737 -0
  94. package/dist/src/schema/union.d.ts.map +1 -0
  95. package/dist/src/schema/union.js +1711 -0
  96. package/dist/src/types.cjs +18 -0
  97. package/dist/src/types.d.ts.map +1 -0
  98. package/dist/src/types.js +0 -0
  99. package/dist/src/utils.cjs +477 -0
  100. package/dist/src/utils.d.ts.map +1 -0
  101. package/dist/src/utils.js +445 -0
  102. package/dist/src/validators/array.cjs +176 -0
  103. package/dist/src/validators/array.d.ts.map +1 -0
  104. package/dist/src/validators/array.js +150 -0
  105. package/dist/src/validators/boolean.cjs +71 -0
  106. package/dist/src/validators/boolean.d.ts.map +1 -0
  107. package/dist/src/validators/boolean.js +47 -0
  108. package/dist/src/validators/datetime.cjs +129 -0
  109. package/dist/src/validators/datetime.d.ts.map +1 -0
  110. package/dist/src/validators/datetime.js +103 -0
  111. package/dist/src/validators/number.cjs +194 -0
  112. package/dist/src/validators/number.d.ts.map +1 -0
  113. package/dist/src/validators/number.js +166 -0
  114. package/dist/src/validators/object.cjs +69 -0
  115. package/dist/src/validators/object.d.ts.map +1 -0
  116. package/dist/src/validators/object.js +46 -0
  117. package/dist/src/validators/schema.cjs +158 -0
  118. package/dist/src/validators/schema.d.ts.map +1 -0
  119. package/dist/src/validators/schema.js +132 -0
  120. package/dist/src/validators/string.cjs +255 -0
  121. package/dist/src/validators/string.d.ts.map +1 -0
  122. package/dist/src/validators/string.js +224 -0
  123. package/dist/src/validators/types.cjs +18 -0
  124. package/dist/src/validators/types.d.ts.map +1 -0
  125. package/dist/src/validators/types.js +0 -0
  126. package/dist/src/validators/union.cjs +69 -0
  127. package/dist/src/validators/union.d.ts.map +1 -0
  128. package/dist/src/validators/union.js +46 -0
  129. package/dist/src/validators/utils.cjs +165 -0
  130. package/dist/src/validators/utils.d.ts.map +1 -0
  131. package/dist/src/validators/utils.js +142 -0
  132. package/dist/tsconfig.types.tsbuildinfo +1 -0
  133. package/package.json +15 -23
  134. package/dist/cjs/src/adapter/fields/array.js +0 -152
  135. package/dist/cjs/src/adapter/fields/boolean.js +0 -162
  136. package/dist/cjs/src/adapter/fields/datetime.js +0 -162
  137. package/dist/cjs/src/adapter/fields/index.js +0 -311
  138. package/dist/cjs/src/adapter/fields/number.js +0 -162
  139. package/dist/cjs/src/adapter/fields/object.js +0 -171
  140. package/dist/cjs/src/adapter/fields/string.js +0 -162
  141. package/dist/cjs/src/adapter/fields/union.js +0 -162
  142. package/dist/cjs/src/adapter/index.js +0 -201
  143. package/dist/cjs/src/adapter/types.js +0 -4
  144. package/dist/cjs/src/compile.js +0 -262
  145. package/dist/cjs/src/conf.js +0 -26
  146. package/dist/cjs/src/constants.js +0 -36
  147. package/dist/cjs/src/domain.js +0 -156
  148. package/dist/cjs/src/exceptions.js +0 -168
  149. package/dist/cjs/src/index.js +0 -317
  150. package/dist/cjs/src/middleware.js +0 -213
  151. package/dist/cjs/src/model.js +0 -628
  152. package/dist/cjs/src/parsers/convert-from-number.js +0 -15
  153. package/dist/cjs/src/parsers/convert-from-string.js +0 -19
  154. package/dist/cjs/src/parsers/index.js +0 -20
  155. package/dist/cjs/src/schema/array.js +0 -1014
  156. package/dist/cjs/src/schema/boolean.js +0 -883
  157. package/dist/cjs/src/schema/datetime.js +0 -840
  158. package/dist/cjs/src/schema/index.js +0 -12
  159. package/dist/cjs/src/schema/number.js +0 -976
  160. package/dist/cjs/src/schema/object.js +0 -1052
  161. package/dist/cjs/src/schema/schema.js +0 -1840
  162. package/dist/cjs/src/schema/string.js +0 -1038
  163. package/dist/cjs/src/schema/types.js +0 -4
  164. package/dist/cjs/src/schema/union.js +0 -1070
  165. package/dist/cjs/src/types.js +0 -4
  166. package/dist/cjs/src/utils.js +0 -647
  167. package/dist/cjs/src/validators/array.js +0 -462
  168. package/dist/cjs/src/validators/boolean.js +0 -201
  169. package/dist/cjs/src/validators/datetime.js +0 -291
  170. package/dist/cjs/src/validators/number.js +0 -375
  171. package/dist/cjs/src/validators/object.js +0 -291
  172. package/dist/cjs/src/validators/schema.js +0 -322
  173. package/dist/cjs/src/validators/string.js +0 -467
  174. package/dist/cjs/src/validators/types.js +0 -4
  175. package/dist/cjs/src/validators/union.js +0 -233
  176. package/dist/cjs/src/validators/utils.js +0 -499
  177. package/dist/cjs/tsconfig.types.tsbuildinfo +0 -1
  178. package/dist/cjs/types/adapter/fields/array.d.ts.map +0 -1
  179. package/dist/cjs/types/adapter/fields/boolean.d.ts.map +0 -1
  180. package/dist/cjs/types/adapter/fields/datetime.d.ts.map +0 -1
  181. package/dist/cjs/types/adapter/fields/index.d.ts.map +0 -1
  182. package/dist/cjs/types/adapter/fields/number.d.ts.map +0 -1
  183. package/dist/cjs/types/adapter/fields/object.d.ts.map +0 -1
  184. package/dist/cjs/types/adapter/fields/string.d.ts.map +0 -1
  185. package/dist/cjs/types/adapter/fields/union.d.ts.map +0 -1
  186. package/dist/cjs/types/adapter/index.d.ts.map +0 -1
  187. package/dist/cjs/types/adapter/types.d.ts.map +0 -1
  188. package/dist/cjs/types/compile.d.ts.map +0 -1
  189. package/dist/cjs/types/conf.d.ts.map +0 -1
  190. package/dist/cjs/types/constants.d.ts.map +0 -1
  191. package/dist/cjs/types/domain.d.ts.map +0 -1
  192. package/dist/cjs/types/exceptions.d.ts.map +0 -1
  193. package/dist/cjs/types/index.d.ts.map +0 -1
  194. package/dist/cjs/types/middleware.d.ts.map +0 -1
  195. package/dist/cjs/types/model.d.ts.map +0 -1
  196. package/dist/cjs/types/parsers/convert-from-number.d.ts.map +0 -1
  197. package/dist/cjs/types/parsers/convert-from-string.d.ts.map +0 -1
  198. package/dist/cjs/types/parsers/index.d.ts.map +0 -1
  199. package/dist/cjs/types/schema/array.d.ts.map +0 -1
  200. package/dist/cjs/types/schema/boolean.d.ts.map +0 -1
  201. package/dist/cjs/types/schema/datetime.d.ts.map +0 -1
  202. package/dist/cjs/types/schema/index.d.ts.map +0 -1
  203. package/dist/cjs/types/schema/number.d.ts.map +0 -1
  204. package/dist/cjs/types/schema/object.d.ts.map +0 -1
  205. package/dist/cjs/types/schema/schema.d.ts.map +0 -1
  206. package/dist/cjs/types/schema/string.d.ts.map +0 -1
  207. package/dist/cjs/types/schema/types.d.ts.map +0 -1
  208. package/dist/cjs/types/schema/union.d.ts.map +0 -1
  209. package/dist/cjs/types/types.d.ts.map +0 -1
  210. package/dist/cjs/types/utils.d.ts.map +0 -1
  211. package/dist/cjs/types/validators/array.d.ts.map +0 -1
  212. package/dist/cjs/types/validators/boolean.d.ts.map +0 -1
  213. package/dist/cjs/types/validators/datetime.d.ts.map +0 -1
  214. package/dist/cjs/types/validators/number.d.ts.map +0 -1
  215. package/dist/cjs/types/validators/object.d.ts.map +0 -1
  216. package/dist/cjs/types/validators/schema.d.ts.map +0 -1
  217. package/dist/cjs/types/validators/string.d.ts.map +0 -1
  218. package/dist/cjs/types/validators/types.d.ts.map +0 -1
  219. package/dist/cjs/types/validators/union.d.ts.map +0 -1
  220. package/dist/cjs/types/validators/utils.d.ts.map +0 -1
  221. package/dist/esm/src/adapter/fields/array.js +0 -13
  222. package/dist/esm/src/adapter/fields/boolean.js +0 -20
  223. package/dist/esm/src/adapter/fields/datetime.js +0 -20
  224. package/dist/esm/src/adapter/fields/index.js +0 -37
  225. package/dist/esm/src/adapter/fields/number.js +0 -20
  226. package/dist/esm/src/adapter/fields/object.js +0 -26
  227. package/dist/esm/src/adapter/fields/string.js +0 -20
  228. package/dist/esm/src/adapter/fields/union.js +0 -20
  229. package/dist/esm/src/adapter/index.js +0 -21
  230. package/dist/esm/src/adapter/types.js +0 -1
  231. package/dist/esm/src/compile.js +0 -10
  232. package/dist/esm/src/conf.js +0 -18
  233. package/dist/esm/src/constants.js +0 -4
  234. package/dist/esm/src/domain.js +0 -11
  235. package/dist/esm/src/exceptions.js +0 -15
  236. package/dist/esm/src/index.js +0 -126
  237. package/dist/esm/src/middleware.js +0 -29
  238. package/dist/esm/src/model.js +0 -264
  239. package/dist/esm/src/parsers/convert-from-number.js +0 -8
  240. package/dist/esm/src/parsers/convert-from-string.js +0 -14
  241. package/dist/esm/src/parsers/index.js +0 -2
  242. package/dist/esm/src/schema/array.js +0 -487
  243. package/dist/esm/src/schema/boolean.js +0 -520
  244. package/dist/esm/src/schema/datetime.js +0 -477
  245. package/dist/esm/src/schema/index.js +0 -3
  246. package/dist/esm/src/schema/number.js +0 -610
  247. package/dist/esm/src/schema/object.js +0 -521
  248. package/dist/esm/src/schema/schema.js +0 -767
  249. package/dist/esm/src/schema/string.js +0 -668
  250. package/dist/esm/src/schema/types.js +0 -1
  251. package/dist/esm/src/schema/union.js +0 -468
  252. package/dist/esm/src/types.js +0 -14
  253. package/dist/esm/src/utils.js +0 -207
  254. package/dist/esm/src/validators/array.js +0 -140
  255. package/dist/esm/src/validators/boolean.js +0 -37
  256. package/dist/esm/src/validators/datetime.js +0 -89
  257. package/dist/esm/src/validators/number.js +0 -148
  258. package/dist/esm/src/validators/object.js +0 -39
  259. package/dist/esm/src/validators/schema.js +0 -118
  260. package/dist/esm/src/validators/string.js +0 -202
  261. package/dist/esm/src/validators/types.js +0 -1
  262. package/dist/esm/src/validators/union.js +0 -39
  263. package/dist/esm/src/validators/utils.js +0 -147
  264. /package/dist/{cjs/types → src}/adapter/fields/array.d.ts +0 -0
  265. /package/dist/{cjs/types → src}/adapter/fields/boolean.d.ts +0 -0
  266. /package/dist/{cjs/types → src}/adapter/fields/datetime.d.ts +0 -0
  267. /package/dist/{cjs/types → src}/adapter/fields/index.d.ts +0 -0
  268. /package/dist/{cjs/types → src}/adapter/fields/number.d.ts +0 -0
  269. /package/dist/{cjs/types → src}/adapter/fields/object.d.ts +0 -0
  270. /package/dist/{cjs/types → src}/adapter/fields/string.d.ts +0 -0
  271. /package/dist/{cjs/types → src}/adapter/fields/union.d.ts +0 -0
  272. /package/dist/{cjs/types → src}/adapter/index.d.ts +0 -0
  273. /package/dist/{cjs/types → src}/adapter/types.d.ts +0 -0
  274. /package/dist/{cjs/types → src}/compile.d.ts +0 -0
  275. /package/dist/{cjs/types → src}/conf.d.ts +0 -0
  276. /package/dist/{cjs/types → src}/constants.d.ts +0 -0
  277. /package/dist/{cjs/types → src}/domain.d.ts +0 -0
  278. /package/dist/{cjs/types → src}/index.d.ts +0 -0
  279. /package/dist/{cjs/types → src}/middleware.d.ts +0 -0
  280. /package/dist/{cjs/types → src}/model.d.ts +0 -0
  281. /package/dist/{cjs/types → src}/parsers/convert-from-number.d.ts +0 -0
  282. /package/dist/{cjs/types → src}/parsers/convert-from-string.d.ts +0 -0
  283. /package/dist/{cjs/types → src}/parsers/index.d.ts +0 -0
  284. /package/dist/{cjs/types → src}/schema/array.d.ts +0 -0
  285. /package/dist/{cjs/types → src}/schema/boolean.d.ts +0 -0
  286. /package/dist/{cjs/types → src}/schema/datetime.d.ts +0 -0
  287. /package/dist/{cjs/types → src}/schema/index.d.ts +0 -0
  288. /package/dist/{cjs/types → src}/schema/number.d.ts +0 -0
  289. /package/dist/{cjs/types → src}/schema/object.d.ts +0 -0
  290. /package/dist/{cjs/types → src}/schema/schema.d.ts +0 -0
  291. /package/dist/{cjs/types → src}/schema/string.d.ts +0 -0
  292. /package/dist/{cjs/types → src}/schema/types.d.ts +0 -0
  293. /package/dist/{cjs/types → src}/schema/union.d.ts +0 -0
  294. /package/dist/{cjs/types → src}/types.d.ts +0 -0
  295. /package/dist/{cjs/types → src}/utils.d.ts +0 -0
  296. /package/dist/{cjs/types → src}/validators/array.d.ts +0 -0
  297. /package/dist/{cjs/types → src}/validators/boolean.d.ts +0 -0
  298. /package/dist/{cjs/types → src}/validators/datetime.d.ts +0 -0
  299. /package/dist/{cjs/types → src}/validators/number.d.ts +0 -0
  300. /package/dist/{cjs/types → src}/validators/object.d.ts +0 -0
  301. /package/dist/{cjs/types → src}/validators/schema.d.ts +0 -0
  302. /package/dist/{cjs/types → src}/validators/string.d.ts +0 -0
  303. /package/dist/{cjs/types → src}/validators/types.d.ts +0 -0
  304. /package/dist/{cjs/types → src}/validators/union.d.ts +0 -0
  305. /package/dist/{cjs/types → src}/validators/utils.d.ts +0 -0
@@ -1,202 +0,0 @@
1
- export function stringValidation() {
2
- return {
3
- name: 'string',
4
- type: 'medium',
5
- // eslint-disable-next-line ts/require-await
6
- callback: async (value, path, _options)=>{
7
- return {
8
- parsed: value,
9
- errors: typeof value === 'string' ? [] : [
10
- {
11
- isValid: typeof value === 'string',
12
- code: 'string',
13
- // eslint-disable-next-line ts/no-unnecessary-condition
14
- path: path || [],
15
- message: 'The value must be a string. Received: ' + typeof value
16
- }
17
- ]
18
- };
19
- }
20
- };
21
- }
22
- export function maxLength(args) {
23
- return {
24
- name: 'maxLength',
25
- type: 'low',
26
- // eslint-disable-next-line ts/require-await
27
- callback: async (value, path, _options)=>{
28
- const isValid = value.length <= args.value;
29
- return {
30
- parsed: value,
31
- errors: isValid ? [] : [
32
- {
33
- isValid: false,
34
- code: 'maxLength',
35
- // eslint-disable-next-line ts/no-unnecessary-condition
36
- path: path || [],
37
- message: args.message
38
- }
39
- ]
40
- };
41
- }
42
- };
43
- }
44
- export function minLength(args) {
45
- return {
46
- name: 'minLength',
47
- type: 'low',
48
- // eslint-disable-next-line ts/require-await
49
- callback: async (value, path, _options)=>{
50
- const isValid = value.length >= args.value;
51
- return {
52
- parsed: value,
53
- errors: isValid ? [] : [
54
- {
55
- isValid: false,
56
- code: 'minLength',
57
- // eslint-disable-next-line ts/no-unnecessary-condition
58
- path: path || [],
59
- message: args.message
60
- }
61
- ]
62
- };
63
- }
64
- };
65
- }
66
- export function endsWith(args) {
67
- return {
68
- name: 'endsWith',
69
- type: 'low',
70
- // eslint-disable-next-line ts/require-await
71
- callback: async (value, path, _options)=>{
72
- const isValid = value.endsWith(args.value);
73
- return {
74
- parsed: value,
75
- errors: isValid ? [] : [
76
- {
77
- isValid: false,
78
- code: 'endsWith',
79
- // eslint-disable-next-line ts/no-unnecessary-condition
80
- path: path || [],
81
- message: args.message
82
- }
83
- ]
84
- };
85
- }
86
- };
87
- }
88
- export function startsWith(args) {
89
- return {
90
- name: 'startsWith',
91
- type: 'low',
92
- // eslint-disable-next-line ts/require-await
93
- callback: async (value, path, _options)=>{
94
- const isValid = value.startsWith(args.value);
95
- return {
96
- parsed: value,
97
- errors: isValid ? [] : [
98
- {
99
- isValid: false,
100
- code: 'startsWith',
101
- // eslint-disable-next-line ts/no-unnecessary-condition
102
- path: path || [],
103
- message: args.message
104
- }
105
- ]
106
- };
107
- }
108
- };
109
- }
110
- export function includes(args) {
111
- return {
112
- name: 'includes',
113
- type: 'low',
114
- // eslint-disable-next-line ts/require-await
115
- callback: async (value, path, _options)=>{
116
- const isValid = value.includes(args.value);
117
- return {
118
- parsed: value,
119
- errors: isValid ? [] : [
120
- {
121
- isValid: false,
122
- code: 'includes',
123
- // eslint-disable-next-line ts/no-unnecessary-condition
124
- path: path || [],
125
- message: args.message
126
- }
127
- ]
128
- };
129
- }
130
- };
131
- }
132
- export function regex(args) {
133
- return {
134
- name: 'regex',
135
- type: 'low',
136
- // eslint-disable-next-line ts/require-await
137
- callback: async (value, path, _options)=>{
138
- const isValid = args.value.test(value);
139
- return {
140
- parsed: value,
141
- errors: isValid ? [] : [
142
- {
143
- isValid: false,
144
- code: 'regex',
145
- // eslint-disable-next-line ts/no-unnecessary-condition
146
- path: path || [],
147
- message: args.message
148
- }
149
- ]
150
- };
151
- }
152
- };
153
- }
154
- export function uuid(args) {
155
- // const uuidRegex =
156
- // /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
157
- 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;
158
- return {
159
- name: 'uuid',
160
- type: 'low',
161
- // eslint-disable-next-line ts/require-await
162
- callback: async (value, path, _options)=>{
163
- const isValid = uuidRegex.test(value);
164
- return {
165
- parsed: value,
166
- errors: isValid ? [] : [
167
- {
168
- isValid: false,
169
- code: 'uuid',
170
- // eslint-disable-next-line ts/no-unnecessary-condition
171
- path: path || [],
172
- message: args.message
173
- }
174
- ]
175
- };
176
- }
177
- };
178
- }
179
- export function email(args) {
180
- const emailRegex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
181
- console.log('aquiiiiii');
182
- return {
183
- name: 'email',
184
- type: 'low',
185
- // eslint-disable-next-line ts/require-await
186
- callback: async (value, path, _options)=>{
187
- const isValid = emailRegex.test(value);
188
- return {
189
- parsed: value,
190
- errors: isValid ? [] : [
191
- {
192
- isValid: false,
193
- code: 'email',
194
- // eslint-disable-next-line ts/no-unnecessary-condition
195
- path: path || [],
196
- message: args.message
197
- }
198
- ]
199
- };
200
- }
201
- };
202
- }
@@ -1 +0,0 @@
1
- export { };
@@ -1,39 +0,0 @@
1
- export function unionValidation(schemas) {
2
- return {
3
- name: 'union',
4
- type: 'medium',
5
- callback: async (value, path, options)=>{
6
- const parsedValues = {
7
- parsed: value,
8
- errors: []
9
- };
10
- const startingToInternalBubbleUpLength = options.toInternalToBubbleUp?.length || 0;
11
- // eslint-disable-next-line ts/prefer-for-of
12
- for(let i = 0; i < schemas.length; i++){
13
- const schemaWithProtected = schemas[i];
14
- const parsedData = await schemaWithProtected.__parse(value, path, options);
15
- parsedValues.parsed = parsedData.parsed;
16
- if (Array.isArray(parsedData.errors)) if (Array.isArray(parsedValues.errors)) parsedValues.errors.push(...parsedData.errors);
17
- else parsedValues.errors = parsedData.errors;
18
- const hasNoErrorsSoItsAValidSchemaAndShouldResetOldErrors = // eslint-disable-next-line ts/no-unnecessary-condition
19
- parsedData.errors === undefined || (parsedData.errors || []).length === 0;
20
- if (hasNoErrorsSoItsAValidSchemaAndShouldResetOldErrors) {
21
- return {
22
- parsed: parsedValues.parsed,
23
- errors: []
24
- };
25
- } else if (startingToInternalBubbleUpLength < (options.toInternalToBubbleUp?.length || 0)) {
26
- // If there is a new toInternalToBubbleUp we should remove the ones that we added since this is not a
27
- // valid schema, we shouldn't be calling the `toInternal` on that schemas.
28
- const numberOfElementsToRemove = (options.toInternalToBubbleUp?.length || 0) - startingToInternalBubbleUpLength;
29
- options.toInternalToBubbleUp?.splice(startingToInternalBubbleUpLength, numberOfElementsToRemove);
30
- }
31
- }
32
- return {
33
- parsed: parsedValues.parsed,
34
- // eslint-disable-next-line ts/no-unnecessary-condition
35
- errors: Array.isArray(parsedValues.errors) ? parsedValues.errors : []
36
- };
37
- }
38
- };
39
- }
@@ -1,147 +0,0 @@
1
- const priorityByType = {
2
- low: 0,
3
- medium: 1,
4
- high: 2
5
- };
6
- const typeByPriority = Object.entries(priorityByType).reduce((acc, [key, value])=>{
7
- acc[value] = key;
8
- return acc;
9
- }, {});
10
- /**
11
- * Okay, so what is this? This is a validator class, it represents a Node on a linked list. The linked list
12
- * has lower priority validators on the end of the list and higher priority validators on the start of the
13
- * list. Maybe in the future we can change that to a binary tree, but for now this is enough.
14
- *
15
- * Why did we choose this approach? Because what i was doing was that i saw myself repeating the same code 3
16
- * times on the schema in order to make the validation work. Each validator had a different return type, i
17
- * didn't like that. I wanted to add more power and control on the validator, not on the schema. So i created
18
- * this class. So pretty much, over here and on each validator we can define the type it is. It can actually
19
- * be three: `low`, `medium` and `high`. The `low` validators are the ones that are going to be executed last,
20
- * The `high` validators are the ones that are going to be executed first. High validators validate if the value
21
- * is null or undefined, if it allows that. It can stop the execution of the other validators if it wants to.
22
- *
23
- * Example: Let's say that the value is null, if the value is null, is there a reason to check if it's a number?
24
- * No, right? So the high validator can stop the execution of the other validators.
25
- * Same as before, if the value is not a number, is there a reason to check if it's value is greater than the
26
- * `max` allowed 10? No, right? So the medium validator can stop the execution of the other validators.
27
- *
28
- * That's what this solve, it's a better approach than repeating the same code 3 times on the schema. It's also
29
- * more powerful, because if we need to add any extra priorities we can do that easily without changing the schema.
30
- */ export class Validator {
31
- $$type = '$PValidator';
32
- child;
33
- parent;
34
- fallbackNamesAdded = new Set();
35
- priority;
36
- fallbacks = [];
37
- constructor(type){
38
- this.fallbackNamesAdded = new Set();
39
- this.fallbacks = [];
40
- this.priority = priorityByType[type];
41
- }
42
- /**
43
- * We create all of the validators on the schema in order, i actually didn't want to go on that route but i
44
- * found it easier to do so.
45
- *
46
- * The logic here is simple, if it's not the same priority we will walk on the linked list until we find
47
- * a validator that matches the priority we are expecting. If we can't walk anymore, we create the next
48
- * priority validator and append it to the linked list. Be aware that it's a double linked list, so we
49
- * can walk both ways, from the end to the start and from the start to the end.
50
- * So you don't really need to start from the root, the linked list can start from anywhere and it will
51
- * find it's way through.
52
- *
53
- * I know there are better ways to do this instead of walking through the linked list, but like i explained
54
- * before, this is enough for now.
55
- *
56
- * If the priority is higher than the current priority saved on the schema, we should substitute the
57
- * rootValidator on the schema with the new one.
58
- *
59
- * @param schema - The schema that we are working on right now, all fallbacks are tied to that specific schema.
60
- * @param type - The type of the fallback that we are adding.
61
- * @param fallback - The fallback function that we are adding.
62
- * @param childOrParent - If we are adding a fallback to the child or to the parent.
63
- * @param options - The options that we are passing to the fallback.
64
- */ checkAppendOrCreate(schema, type, fallbackName, fallback, childOrParent, options) {
65
- const schemaWithProtected = schema;
66
- if (this[childOrParent]) this[childOrParent].addFallback(schemaWithProtected, type, fallbackName, fallback, options);
67
- else {
68
- const nextPriority = childOrParent === 'child' ? this.priority - 1 : this.priority + 1;
69
- if (Object.keys(typeByPriority).includes(String(nextPriority))) {
70
- const nextType = typeByPriority[nextPriority];
71
- const validatorInstance = new Validator(nextType);
72
- this[childOrParent] = validatorInstance;
73
- this[childOrParent][childOrParent === 'parent' ? 'child' : 'parent'] = this;
74
- this[childOrParent].addFallback(schemaWithProtected, type, fallbackName, fallback, options);
75
- if (nextPriority > schemaWithProtected.__rootFallbacksValidator.priority) schemaWithProtected.__rootFallbacksValidator = validatorInstance;
76
- }
77
- }
78
- }
79
- addFallback(schema, type, fallbackName, fallback, options) {
80
- if (this.fallbackNamesAdded.has(fallbackName) && options?.removeCurrent !== true) return;
81
- this.fallbackNamesAdded.add(fallbackName);
82
- const priority = priorityByType[type];
83
- if (this.priority === priority) {
84
- if (typeof options?.at === 'number') this.fallbacks.splice(options.at, options.removeCurrent === true ? 1 : 0, fallback);
85
- else this.fallbacks.push(fallback);
86
- } else if (priority > this.priority) this.checkAppendOrCreate(schema, type, fallbackName, fallback, 'parent', options);
87
- else if (priority < this.priority) this.checkAppendOrCreate(schema, type, fallbackName, fallback, 'child', options);
88
- }
89
- /**
90
- * Validates the value against all of the fallbacks, the fallbacks are executed in order, from the highest
91
- * priority to the lowest priority. A validator can stop the execution of the other validators if it feels
92
- * like so. Like on the example of a value being null or undefined.
93
- *
94
- * @param errorsAsHashedSet - This is a set that contains all of the errors that we already found, this is
95
- * used to avoid duplicated errors.
96
- * @param path - The path that we are validating right now.
97
- * @param parseResult - The result of the parsing, it contains the parsed value and the errors that we found.
98
- * @param options - The options that we are passing to the fallback.
99
- */ async validate(errorsAsHashedSet, path, parseResult, options) {
100
- let doesItShouldPreventChildValidation = false;
101
- for (const fallback of this.fallbacks){
102
- const { parsed, errors, preventChildValidation } = await fallback(parseResult.parsed, path, options);
103
- parseResult.parsed = parsed;
104
- for (const error of errors){
105
- if (error.isValid === false) {
106
- const sortedError = Object.fromEntries(Object.entries(error).sort(([a], [b])=>a.localeCompare(b)));
107
- const hashedError = JSON.stringify(sortedError);
108
- if (errorsAsHashedSet.has(hashedError)) continue;
109
- errorsAsHashedSet.add(hashedError);
110
- if (!Array.isArray(parseResult.errors)) parseResult.errors = [];
111
- parseResult.errors.push({
112
- ...error,
113
- received: parseResult.parsed
114
- });
115
- }
116
- }
117
- doesItShouldPreventChildValidation = doesItShouldPreventChildValidation || preventChildValidation || false;
118
- }
119
- if (this.child && doesItShouldPreventChildValidation === false) return await this.child.validate(errorsAsHashedSet, path, parseResult, options);
120
- return parseResult;
121
- }
122
- /**
123
- * This static method takes care of everything for you. This means that you should only call this method
124
- * for appending new fallbacks, it takes care of creating the root validator and making sure that the
125
- * rootValidator on the schema is the highest priority one.
126
- *
127
- * @param schema - The schema that we are working on right now, all fallbacks are tied to that specific
128
- * schema. We automatically define the rootValidator on the schema so you don't need to worry about that.
129
- * @param fallback - The fallback that we are adding. This is an object that contains the type of the
130
- * fallback and the callback that we are adding.
131
- * @param options - The options that we are passing to the fallback. Options like `at` and `removeCurrent`
132
- * are passed to the `addFallback` method.
133
- */ static createAndAppendFallback(schema, fallback, options) {
134
- const schemaWithProtected = schema;
135
- let validatorInstance = schemaWithProtected.__rootFallbacksValidator;
136
- // eslint-disable-next-line ts/no-unnecessary-condition
137
- if (schemaWithProtected.__rootFallbacksValidator === undefined) {
138
- validatorInstance = new Validator(fallback.type);
139
- schemaWithProtected.__rootFallbacksValidator = validatorInstance;
140
- }
141
- validatorInstance.addFallback(schema, fallback.type, fallback.name, fallback.callback, options);
142
- return validatorInstance;
143
- }
144
- toString(ident = 0) {
145
- return `Priority: ${this.priority}\nFallbacks: ${this.fallbacks.length}\n${this.child ? `Children:\n${this.child.toString(ident + 2)}` : ''}`;
146
- }
147
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes