vscroll 1.5.3 → 1.5.5

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 (335) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +283 -273
  3. package/dist/bundles/vscroll.esm5.js +5558 -5525
  4. package/dist/bundles/vscroll.esm5.js.map +1 -1
  5. package/dist/bundles/vscroll.esm5.min.js +2 -2
  6. package/dist/bundles/vscroll.esm5.min.js.map +1 -1
  7. package/dist/bundles/vscroll.esm6.js +5045 -5015
  8. package/dist/bundles/vscroll.esm6.js.map +1 -1
  9. package/dist/bundles/vscroll.esm6.min.js +2 -2
  10. package/dist/bundles/vscroll.esm6.min.js.map +1 -1
  11. package/dist/bundles/vscroll.umd.js +5559 -5526
  12. package/dist/bundles/vscroll.umd.js.map +1 -1
  13. package/dist/bundles/vscroll.umd.min.js +2 -2
  14. package/dist/bundles/vscroll.umd.min.js.map +1 -1
  15. package/dist/esm2015/classes/adapter/context.js +43 -43
  16. package/dist/esm2015/classes/adapter/context.js.map +1 -1
  17. package/dist/esm2015/classes/adapter/props.js +268 -268
  18. package/dist/esm2015/classes/adapter/props.js.map +1 -1
  19. package/dist/esm2015/classes/adapter/wanted.js +28 -28
  20. package/dist/esm2015/classes/adapter/wanted.js.map +1 -1
  21. package/dist/esm2015/classes/adapter.js +455 -442
  22. package/dist/esm2015/classes/adapter.js.map +1 -1
  23. package/dist/esm2015/classes/buffer/cache.js +246 -246
  24. package/dist/esm2015/classes/buffer/cache.js.map +1 -1
  25. package/dist/esm2015/classes/buffer/checkCall.js +48 -48
  26. package/dist/esm2015/classes/buffer/checkCall.js.map +1 -1
  27. package/dist/esm2015/classes/buffer/defaultSize.js +127 -127
  28. package/dist/esm2015/classes/buffer/defaultSize.js.map +1 -1
  29. package/dist/esm2015/classes/buffer.js +342 -342
  30. package/dist/esm2015/classes/buffer.js.map +1 -1
  31. package/dist/esm2015/classes/datasource.js +28 -28
  32. package/dist/esm2015/classes/domRoutines.js +166 -166
  33. package/dist/esm2015/classes/domRoutines.js.map +1 -1
  34. package/dist/esm2015/classes/item.js +61 -61
  35. package/dist/esm2015/classes/item.js.map +1 -1
  36. package/dist/esm2015/classes/logger.js +186 -186
  37. package/dist/esm2015/classes/logger.js.map +1 -1
  38. package/dist/esm2015/classes/paddings.js +72 -72
  39. package/dist/esm2015/classes/reactive.js +54 -54
  40. package/dist/esm2015/classes/reactive.js.map +1 -1
  41. package/dist/esm2015/classes/settings.js +39 -39
  42. package/dist/esm2015/classes/settings.js.map +1 -1
  43. package/dist/esm2015/classes/state/clip.js +16 -16
  44. package/dist/esm2015/classes/state/clip.js.map +1 -1
  45. package/dist/esm2015/classes/state/cycle.js +60 -60
  46. package/dist/esm2015/classes/state/cycle.js.map +1 -1
  47. package/dist/esm2015/classes/state/fetch.js +122 -122
  48. package/dist/esm2015/classes/state/fetch.js.map +1 -1
  49. package/dist/esm2015/classes/state/render.js +14 -14
  50. package/dist/esm2015/classes/state/render.js.map +1 -1
  51. package/dist/esm2015/classes/state/scroll.js +30 -30
  52. package/dist/esm2015/classes/state/scroll.js.map +1 -1
  53. package/dist/esm2015/classes/state.js +59 -59
  54. package/dist/esm2015/classes/viewport.js +72 -72
  55. package/dist/esm2015/classes/viewport.js.map +1 -1
  56. package/dist/esm2015/index.js +8 -8
  57. package/dist/esm2015/index.js.map +1 -1
  58. package/dist/esm2015/inputs/adapter.js +230 -230
  59. package/dist/esm2015/inputs/adapter.js.map +1 -1
  60. package/dist/esm2015/inputs/common.js +11 -11
  61. package/dist/esm2015/inputs/common.js.map +1 -1
  62. package/dist/esm2015/inputs/datasource.js +20 -20
  63. package/dist/esm2015/inputs/datasource.js.map +1 -1
  64. package/dist/esm2015/inputs/index.js +6 -6
  65. package/dist/esm2015/inputs/index.js.map +1 -1
  66. package/dist/esm2015/inputs/settings.js +146 -146
  67. package/dist/esm2015/inputs/settings.js.map +1 -1
  68. package/dist/esm2015/inputs/validation.js +374 -374
  69. package/dist/esm2015/inputs/validation.js.map +1 -1
  70. package/dist/esm2015/interfaces/adapter.js +1 -1
  71. package/dist/esm2015/interfaces/datasource.js +1 -1
  72. package/dist/esm2015/interfaces/datasource.js.map +1 -1
  73. package/dist/esm2015/interfaces/index.js +1 -1
  74. package/dist/esm2015/interfaces/index.js.map +1 -1
  75. package/dist/esm2015/interfaces/item.js +1 -1
  76. package/dist/esm2015/interfaces/item.js.map +1 -1
  77. package/dist/esm2015/interfaces/process.js +1 -1
  78. package/dist/esm2015/interfaces/routines.js +1 -1
  79. package/dist/esm2015/interfaces/routines.js.map +1 -1
  80. package/dist/esm2015/interfaces/settings.js +1 -1
  81. package/dist/esm2015/interfaces/state.js +1 -1
  82. package/dist/esm2015/interfaces/validation.js +1 -1
  83. package/dist/esm2015/interfaces/validation.js.map +1 -1
  84. package/dist/esm2015/interfaces/workflow.js +1 -1
  85. package/dist/esm2015/interfaces/workflow.js.map +1 -1
  86. package/dist/esm2015/processes/adapter/append.js +35 -35
  87. package/dist/esm2015/processes/adapter/append.js.map +1 -1
  88. package/dist/esm2015/processes/adapter/check.js +32 -32
  89. package/dist/esm2015/processes/adapter/check.js.map +1 -1
  90. package/dist/esm2015/processes/adapter/clip.js +12 -12
  91. package/dist/esm2015/processes/adapter/clip.js.map +1 -1
  92. package/dist/esm2015/processes/adapter/fix.js +77 -77
  93. package/dist/esm2015/processes/adapter/fix.js.map +1 -1
  94. package/dist/esm2015/processes/adapter/insert.js +73 -73
  95. package/dist/esm2015/processes/adapter/insert.js.map +1 -1
  96. package/dist/esm2015/processes/adapter/reload.js +19 -20
  97. package/dist/esm2015/processes/adapter/reload.js.map +1 -1
  98. package/dist/esm2015/processes/adapter/remove.js +102 -102
  99. package/dist/esm2015/processes/adapter/remove.js.map +1 -1
  100. package/dist/esm2015/processes/adapter/replace.js +39 -39
  101. package/dist/esm2015/processes/adapter/replace.js.map +1 -1
  102. package/dist/esm2015/processes/adapter/reset.js +35 -35
  103. package/dist/esm2015/processes/adapter/reset.js.map +1 -1
  104. package/dist/esm2015/processes/adapter/update.js +43 -43
  105. package/dist/esm2015/processes/adapter/update.js.map +1 -1
  106. package/dist/esm2015/processes/adjust.js +141 -141
  107. package/dist/esm2015/processes/clip.js +49 -49
  108. package/dist/esm2015/processes/end.js +52 -52
  109. package/dist/esm2015/processes/end.js.map +1 -1
  110. package/dist/esm2015/processes/fetch.js +91 -90
  111. package/dist/esm2015/processes/fetch.js.map +1 -1
  112. package/dist/esm2015/processes/index.js +23 -23
  113. package/dist/esm2015/processes/init.js +14 -14
  114. package/dist/esm2015/processes/init.js.map +1 -1
  115. package/dist/esm2015/processes/misc/base.js +30 -30
  116. package/dist/esm2015/processes/misc/base.js.map +1 -1
  117. package/dist/esm2015/processes/misc/enums.js +35 -35
  118. package/dist/esm2015/processes/misc/enums.js.map +1 -1
  119. package/dist/esm2015/processes/misc/index.js +2 -2
  120. package/dist/esm2015/processes/misc/index.js.map +1 -1
  121. package/dist/esm2015/processes/postFetch.js +64 -64
  122. package/dist/esm2015/processes/postFetch.js.map +1 -1
  123. package/dist/esm2015/processes/preClip.js +85 -85
  124. package/dist/esm2015/processes/preClip.js.map +1 -1
  125. package/dist/esm2015/processes/preFetch.js +240 -240
  126. package/dist/esm2015/processes/preFetch.js.map +1 -1
  127. package/dist/esm2015/processes/render.js +54 -54
  128. package/dist/esm2015/processes/render.js.map +1 -1
  129. package/dist/esm2015/processes/scroll.js +97 -97
  130. package/dist/esm2015/processes/scroll.js.map +1 -1
  131. package/dist/esm2015/processes/start.js +11 -11
  132. package/dist/esm2015/processes/start.js.map +1 -1
  133. package/dist/esm2015/scroller.js +76 -69
  134. package/dist/esm2015/scroller.js.map +1 -1
  135. package/dist/esm2015/version.js +4 -4
  136. package/dist/esm2015/version.js.map +1 -1
  137. package/dist/esm2015/workflow-transducer.js +210 -210
  138. package/dist/esm2015/workflow-transducer.js.map +1 -1
  139. package/dist/esm2015/workflow.js +161 -151
  140. package/dist/esm2015/workflow.js.map +1 -1
  141. package/dist/esm5/classes/adapter/context.js +50 -50
  142. package/dist/esm5/classes/adapter/context.js.map +1 -1
  143. package/dist/esm5/classes/adapter/props.js +268 -268
  144. package/dist/esm5/classes/adapter/props.js.map +1 -1
  145. package/dist/esm5/classes/adapter/wanted.js +29 -29
  146. package/dist/esm5/classes/adapter/wanted.js.map +1 -1
  147. package/dist/esm5/classes/adapter.js +504 -490
  148. package/dist/esm5/classes/adapter.js.map +1 -1
  149. package/dist/esm5/classes/buffer/cache.js +260 -260
  150. package/dist/esm5/classes/buffer/cache.js.map +1 -1
  151. package/dist/esm5/classes/buffer/checkCall.js +56 -56
  152. package/dist/esm5/classes/buffer/checkCall.js.map +1 -1
  153. package/dist/esm5/classes/buffer/defaultSize.js +145 -145
  154. package/dist/esm5/classes/buffer/defaultSize.js.map +1 -1
  155. package/dist/esm5/classes/buffer.js +409 -409
  156. package/dist/esm5/classes/buffer.js.map +1 -1
  157. package/dist/esm5/classes/datasource.js +37 -37
  158. package/dist/esm5/classes/domRoutines.js +182 -182
  159. package/dist/esm5/classes/domRoutines.js.map +1 -1
  160. package/dist/esm5/classes/item.js +76 -76
  161. package/dist/esm5/classes/item.js.map +1 -1
  162. package/dist/esm5/classes/logger.js +210 -210
  163. package/dist/esm5/classes/logger.js.map +1 -1
  164. package/dist/esm5/classes/paddings.js +80 -80
  165. package/dist/esm5/classes/reactive.js +69 -69
  166. package/dist/esm5/classes/reactive.js.map +1 -1
  167. package/dist/esm5/classes/settings.js +47 -47
  168. package/dist/esm5/classes/settings.js.map +1 -1
  169. package/dist/esm5/classes/state/clip.js +22 -22
  170. package/dist/esm5/classes/state/clip.js.map +1 -1
  171. package/dist/esm5/classes/state/cycle.js +75 -75
  172. package/dist/esm5/classes/state/cycle.js.map +1 -1
  173. package/dist/esm5/classes/state/fetch.js +148 -148
  174. package/dist/esm5/classes/state/fetch.js.map +1 -1
  175. package/dist/esm5/classes/state/render.js +20 -20
  176. package/dist/esm5/classes/state/render.js.map +1 -1
  177. package/dist/esm5/classes/state/scroll.js +32 -32
  178. package/dist/esm5/classes/state/scroll.js.map +1 -1
  179. package/dist/esm5/classes/state.js +66 -66
  180. package/dist/esm5/classes/viewport.js +79 -79
  181. package/dist/esm5/classes/viewport.js.map +1 -1
  182. package/dist/esm5/index.js +8 -8
  183. package/dist/esm5/index.js.map +1 -1
  184. package/dist/esm5/inputs/adapter.js +231 -231
  185. package/dist/esm5/inputs/adapter.js.map +1 -1
  186. package/dist/esm5/inputs/common.js +11 -11
  187. package/dist/esm5/inputs/common.js.map +1 -1
  188. package/dist/esm5/inputs/datasource.js +21 -21
  189. package/dist/esm5/inputs/datasource.js.map +1 -1
  190. package/dist/esm5/inputs/index.js +6 -6
  191. package/dist/esm5/inputs/index.js.map +1 -1
  192. package/dist/esm5/inputs/settings.js +147 -147
  193. package/dist/esm5/inputs/settings.js.map +1 -1
  194. package/dist/esm5/inputs/validation.js +396 -396
  195. package/dist/esm5/inputs/validation.js.map +1 -1
  196. package/dist/esm5/interfaces/adapter.js +1 -1
  197. package/dist/esm5/interfaces/datasource.js +1 -1
  198. package/dist/esm5/interfaces/datasource.js.map +1 -1
  199. package/dist/esm5/interfaces/index.js +1 -1
  200. package/dist/esm5/interfaces/index.js.map +1 -1
  201. package/dist/esm5/interfaces/item.js +1 -1
  202. package/dist/esm5/interfaces/item.js.map +1 -1
  203. package/dist/esm5/interfaces/process.js +1 -1
  204. package/dist/esm5/interfaces/routines.js +1 -1
  205. package/dist/esm5/interfaces/routines.js.map +1 -1
  206. package/dist/esm5/interfaces/settings.js +1 -1
  207. package/dist/esm5/interfaces/state.js +1 -1
  208. package/dist/esm5/interfaces/validation.js +1 -1
  209. package/dist/esm5/interfaces/validation.js.map +1 -1
  210. package/dist/esm5/interfaces/workflow.js +1 -1
  211. package/dist/esm5/interfaces/workflow.js.map +1 -1
  212. package/dist/esm5/processes/adapter/append.js +43 -43
  213. package/dist/esm5/processes/adapter/append.js.map +1 -1
  214. package/dist/esm5/processes/adapter/check.js +39 -39
  215. package/dist/esm5/processes/adapter/check.js.map +1 -1
  216. package/dist/esm5/processes/adapter/clip.js +19 -19
  217. package/dist/esm5/processes/adapter/clip.js.map +1 -1
  218. package/dist/esm5/processes/adapter/fix.js +89 -89
  219. package/dist/esm5/processes/adapter/fix.js.map +1 -1
  220. package/dist/esm5/processes/adapter/insert.js +81 -81
  221. package/dist/esm5/processes/adapter/insert.js.map +1 -1
  222. package/dist/esm5/processes/adapter/reload.js +26 -27
  223. package/dist/esm5/processes/adapter/reload.js.map +1 -1
  224. package/dist/esm5/processes/adapter/remove.js +115 -115
  225. package/dist/esm5/processes/adapter/remove.js.map +1 -1
  226. package/dist/esm5/processes/adapter/replace.js +47 -47
  227. package/dist/esm5/processes/adapter/replace.js.map +1 -1
  228. package/dist/esm5/processes/adapter/reset.js +42 -42
  229. package/dist/esm5/processes/adapter/reset.js.map +1 -1
  230. package/dist/esm5/processes/adapter/update.js +59 -59
  231. package/dist/esm5/processes/adapter/update.js.map +1 -1
  232. package/dist/esm5/processes/adjust.js +152 -152
  233. package/dist/esm5/processes/clip.js +60 -60
  234. package/dist/esm5/processes/end.js +63 -63
  235. package/dist/esm5/processes/end.js.map +1 -1
  236. package/dist/esm5/processes/fetch.js +102 -101
  237. package/dist/esm5/processes/fetch.js.map +1 -1
  238. package/dist/esm5/processes/index.js +23 -23
  239. package/dist/esm5/processes/init.js +21 -21
  240. package/dist/esm5/processes/init.js.map +1 -1
  241. package/dist/esm5/processes/misc/base.js +44 -44
  242. package/dist/esm5/processes/misc/base.js.map +1 -1
  243. package/dist/esm5/processes/misc/enums.js +35 -35
  244. package/dist/esm5/processes/misc/enums.js.map +1 -1
  245. package/dist/esm5/processes/misc/index.js +2 -2
  246. package/dist/esm5/processes/misc/index.js.map +1 -1
  247. package/dist/esm5/processes/postFetch.js +73 -73
  248. package/dist/esm5/processes/postFetch.js.map +1 -1
  249. package/dist/esm5/processes/preClip.js +94 -94
  250. package/dist/esm5/processes/preClip.js.map +1 -1
  251. package/dist/esm5/processes/preFetch.js +247 -247
  252. package/dist/esm5/processes/preFetch.js.map +1 -1
  253. package/dist/esm5/processes/render.js +63 -63
  254. package/dist/esm5/processes/render.js.map +1 -1
  255. package/dist/esm5/processes/scroll.js +107 -107
  256. package/dist/esm5/processes/scroll.js.map +1 -1
  257. package/dist/esm5/processes/start.js +18 -18
  258. package/dist/esm5/processes/start.js.map +1 -1
  259. package/dist/esm5/scroller.js +82 -73
  260. package/dist/esm5/scroller.js.map +1 -1
  261. package/dist/esm5/version.js +4 -4
  262. package/dist/esm5/version.js.map +1 -1
  263. package/dist/esm5/workflow-transducer.js +212 -212
  264. package/dist/esm5/workflow-transducer.js.map +1 -1
  265. package/dist/esm5/workflow.js +186 -176
  266. package/dist/esm5/workflow.js.map +1 -1
  267. package/dist/typings/classes/adapter/context.d.ts +4 -4
  268. package/dist/typings/classes/adapter/props.d.ts +47 -47
  269. package/dist/typings/classes/adapter/wanted.d.ts +14 -14
  270. package/dist/typings/classes/adapter.d.ts +74 -64
  271. package/dist/typings/classes/buffer/cache.d.ts +95 -95
  272. package/dist/typings/classes/buffer/checkCall.d.ts +12 -12
  273. package/dist/typings/classes/buffer/defaultSize.d.ts +32 -32
  274. package/dist/typings/classes/buffer.d.ts +64 -64
  275. package/dist/typings/classes/datasource.d.ts +27 -27
  276. package/dist/typings/classes/domRoutines.d.ts +33 -33
  277. package/dist/typings/classes/item.d.ts +28 -28
  278. package/dist/typings/classes/logger.d.ts +28 -28
  279. package/dist/typings/classes/paddings.d.ts +22 -22
  280. package/dist/typings/classes/reactive.d.ts +21 -21
  281. package/dist/typings/classes/settings.d.ts +119 -119
  282. package/dist/typings/classes/state/clip.d.ts +9 -9
  283. package/dist/typings/classes/state/cycle.d.ts +29 -29
  284. package/dist/typings/classes/state/fetch.d.ts +62 -62
  285. package/dist/typings/classes/state/render.d.ts +9 -9
  286. package/dist/typings/classes/state/scroll.d.ts +16 -16
  287. package/dist/typings/classes/state.d.ts +27 -27
  288. package/dist/typings/classes/viewport.d.ts +32 -32
  289. package/dist/typings/index.d.ts +9 -9
  290. package/dist/typings/inputs/adapter.d.ts +5 -5
  291. package/dist/typings/inputs/common.d.ts +9 -9
  292. package/dist/typings/inputs/datasource.d.ts +7 -7
  293. package/dist/typings/inputs/index.d.ts +6 -6
  294. package/dist/typings/inputs/settings.d.ts +41 -41
  295. package/dist/typings/inputs/validation.d.ts +83 -83
  296. package/dist/typings/interfaces/adapter.d.ts +156 -156
  297. package/dist/typings/interfaces/datasource.d.ts +26 -26
  298. package/dist/typings/interfaces/index.d.ts +10 -10
  299. package/dist/typings/interfaces/item.d.ts +8 -8
  300. package/dist/typings/interfaces/process.d.ts +35 -35
  301. package/dist/typings/interfaces/routines.d.ts +157 -157
  302. package/dist/typings/interfaces/settings.d.ts +31 -31
  303. package/dist/typings/interfaces/state.d.ts +22 -22
  304. package/dist/typings/interfaces/validation.d.ts +33 -33
  305. package/dist/typings/interfaces/workflow.d.ts +53 -53
  306. package/dist/typings/processes/adapter/append.d.ts +14 -14
  307. package/dist/typings/processes/adapter/check.d.ts +6 -6
  308. package/dist/typings/processes/adapter/clip.d.ts +7 -7
  309. package/dist/typings/processes/adapter/fix.d.ts +13 -13
  310. package/dist/typings/processes/adapter/insert.d.ts +11 -11
  311. package/dist/typings/processes/adapter/reload.d.ts +6 -6
  312. package/dist/typings/processes/adapter/remove.d.ts +11 -11
  313. package/dist/typings/processes/adapter/replace.d.ts +8 -8
  314. package/dist/typings/processes/adapter/reset.d.ts +7 -7
  315. package/dist/typings/processes/adapter/update.d.ts +8 -8
  316. package/dist/typings/processes/adjust.d.ts +10 -10
  317. package/dist/typings/processes/clip.d.ts +7 -7
  318. package/dist/typings/processes/end.d.ts +10 -10
  319. package/dist/typings/processes/fetch.d.ts +18 -18
  320. package/dist/typings/processes/index.d.ts +23 -23
  321. package/dist/typings/processes/init.d.ts +7 -7
  322. package/dist/typings/processes/misc/base.d.ts +4 -4
  323. package/dist/typings/processes/misc/enums.d.ts +32 -32
  324. package/dist/typings/processes/misc/index.d.ts +2 -2
  325. package/dist/typings/processes/postFetch.d.ts +8 -8
  326. package/dist/typings/processes/preClip.d.ts +12 -12
  327. package/dist/typings/processes/preFetch.d.ts +17 -17
  328. package/dist/typings/processes/render.d.ts +9 -9
  329. package/dist/typings/processes/scroll.d.ts +13 -13
  330. package/dist/typings/processes/start.d.ts +6 -6
  331. package/dist/typings/scroller.d.ts +26 -26
  332. package/dist/typings/version.d.ts +5 -5
  333. package/dist/typings/workflow-transducer.d.ts +2 -2
  334. package/dist/typings/workflow.d.ts +29 -28
  335. package/package.json +68 -68
@@ -1,375 +1,375 @@
1
- export var ValidatorType;
2
- (function (ValidatorType) {
3
- ValidatorType["number"] = "must be a number";
4
- ValidatorType["integer"] = "must be an integer";
5
- ValidatorType["integerUnlimited"] = "must be an integer or infinity";
6
- ValidatorType["moreOrEqual"] = "must be a number greater than (or equal to) {arg1}";
7
- ValidatorType["itemList"] = "must be an array of items {arg1}";
8
- ValidatorType["boolean"] = "must be a boolean";
9
- ValidatorType["object"] = "must be an object";
10
- ValidatorType["element"] = "must be an html element";
11
- ValidatorType["function"] = "must be a function";
12
- ValidatorType["funcOfxArguments"] = "must have {arg1} argument(s)";
13
- ValidatorType["funcOfxAndMoreArguments"] = "must have at least {arg1} argument(s)";
14
- ValidatorType["funcOfXToYArguments"] = "must have {arg1} to {arg2} arguments";
15
- ValidatorType["oneOfCan"] = "can be present as only one item of {arg1} list";
16
- ValidatorType["oneOfMust"] = "must be present as only one item of {arg1} list";
17
- ValidatorType["or"] = "must satisfy at least 1 validator from {arg1} list";
18
- ValidatorType["enum"] = "must belong to {arg1} list";
19
- })(ValidatorType || (ValidatorType = {}));
20
- const getError = (msg, args) => (args || ['']).reduce((acc, arg, index) => acc.replace(`{arg${index + 1}}`, arg), msg);
21
- const getNumber = (value) => typeof value === 'number' || (typeof value === 'string' && value !== '')
22
- ? Number(value)
23
- : NaN;
24
- const onNumber = (value) => {
25
- const parsedValue = getNumber(value);
26
- const errors = [];
27
- if (Number.isNaN(parsedValue)) {
28
- errors.push(ValidatorType.number);
29
- }
30
- return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
31
- };
32
- const onInteger = (value) => {
33
- const errors = [];
34
- value = getNumber(value);
35
- const parsedValue = parseInt(String(value), 10);
36
- if (value !== parsedValue) {
37
- errors.push(ValidatorType.integer);
38
- }
39
- return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
40
- };
41
- const onIntegerUnlimited = (value) => {
42
- let parsedValue = value;
43
- const errors = [];
44
- value = getNumber(value);
45
- if (!Number.isFinite(value)) {
46
- parsedValue = value;
47
- }
48
- else {
49
- parsedValue = parseInt(String(value), 10);
50
- }
51
- if (value !== parsedValue) {
52
- errors.push(ValidatorType.integerUnlimited);
53
- }
54
- return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
55
- };
56
- const onMoreOrEqual = (limit, fallback) => (value) => {
57
- const result = onNumber(value);
58
- if (!result.isValid) {
59
- return result;
60
- }
61
- let parsedValue = result.value;
62
- const errors = [];
63
- if (parsedValue < limit) {
64
- if (!fallback) {
65
- errors.push(getError(ValidatorType.moreOrEqual, [String(limit)]));
66
- }
67
- else {
68
- parsedValue = limit;
69
- }
70
- }
71
- return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
72
- };
73
- const onBoolean = (value) => {
74
- const errors = [];
75
- let parsedValue = value;
76
- if (value === 'true') {
77
- parsedValue = true;
78
- }
79
- else if (value === 'false') {
80
- parsedValue = false;
81
- }
82
- if (typeof parsedValue !== 'boolean') {
83
- errors.push(ValidatorType.boolean);
84
- }
85
- return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
86
- };
87
- const onObject = (value) => {
88
- const errors = [];
89
- if (Object.prototype.toString.call(value) !== '[object Object]') {
90
- errors.push(ValidatorType.object);
91
- }
92
- return { value, isSet: true, isValid: !errors.length, errors };
93
- };
94
- const onHtmlElement = (value) => {
95
- const errors = [];
96
- if (!(value instanceof Element) && !(value instanceof HTMLDocument)) {
97
- errors.push(ValidatorType.element);
98
- }
99
- return { value, isSet: true, isValid: !errors.length, errors };
100
- };
101
- const onItemList = (value) => {
102
- let parsedValue = value;
103
- const errors = [];
104
- if (!Array.isArray(value)) {
105
- errors.push(ValidatorType.itemList);
106
- parsedValue = [];
107
- }
108
- else if (!value.length) {
109
- errors.push(getError(ValidatorType.itemList, ['with at least 1 item']));
110
- }
111
- else if (value.length > 1) {
112
- const type = typeof value[0];
113
- for (let i = value.length - 1; i >= 0; i--) {
114
- if (typeof value[i] !== type) {
115
- errors.push(getError(ValidatorType.itemList, ['of items of the same type']));
116
- break;
117
- }
118
- }
119
- }
120
- return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
121
- };
122
- const onFunction = (value) => {
123
- const errors = [];
124
- if (typeof value !== 'function') {
125
- errors.push(ValidatorType.function);
126
- }
127
- return { value: value, isSet: true, isValid: !errors.length, errors };
128
- };
129
- const onFunctionWithXArguments = (argsCount) => (value) => {
130
- const result = onFunction(value);
131
- if (!result.isValid) {
132
- return result;
133
- }
134
- value = result.value;
135
- const errors = [];
136
- if (value.length !== argsCount) {
137
- errors.push(getError(ValidatorType.funcOfxArguments, [String(argsCount)]));
138
- }
139
- return { value: value, isSet: true, isValid: !errors.length, errors };
140
- };
141
- const onFunctionWithXAndMoreArguments = (argsCount) => (value) => {
142
- const result = onFunction(value);
143
- if (!result.isValid) {
144
- return result;
145
- }
146
- value = result.value;
147
- const errors = [];
148
- if (value.length < argsCount) {
149
- errors.push(getError(ValidatorType.funcOfxArguments, [String(argsCount)]));
150
- }
151
- return { value: value, isSet: true, isValid: !errors.length, errors };
152
- };
153
- const onFunctionWithXToYArguments = (from, to) => (value) => {
154
- const result = onFunction(value);
155
- if (!result.isValid) {
156
- return result;
157
- }
158
- value = result.value;
159
- const errors = [];
160
- if (value.length < from || value.length > to) {
161
- errors.push(getError(ValidatorType.funcOfXToYArguments, [String(from), String(to)]));
162
- }
163
- return { value: value, isSet: true, isValid: !errors.length, errors };
164
- };
165
- const onOneOf = (tokens, must) => (value, context) => {
166
- const errors = [];
167
- const isSet = value !== void 0;
168
- let noOneIsPresent = !isSet;
169
- const err = must ? ValidatorType.oneOfMust : ValidatorType.oneOfCan;
170
- if (!Array.isArray(tokens) || !tokens.length) {
171
- errors.push(getError(err, ['undefined']));
172
- }
173
- else {
174
- for (let i = tokens.length - 1; i >= 0; i--) {
175
- const token = tokens[i];
176
- if (typeof token !== 'string') {
177
- errors.push(getError(err, [tokens.join('", "')]) + ' (non-string token)');
178
- break;
179
- }
180
- const isAnotherPresent = context && Object.prototype.hasOwnProperty.call(context, token);
181
- if (isSet && isAnotherPresent) {
182
- errors.push(getError(err, [tokens.join('", "')]) + ` (${token} is present)`);
183
- break;
184
- }
185
- if (noOneIsPresent && isAnotherPresent) {
186
- noOneIsPresent = false;
187
- }
188
- }
189
- if (must && noOneIsPresent) {
190
- errors.push(getError(err, [tokens.join('", "')]));
191
- }
192
- }
193
- return { value, isSet, isValid: !errors.length, errors };
194
- };
195
- const onOr = (validators) => (value) => {
196
- const errors = [];
197
- if (validators.every(validator => !validator.method(value).isValid)) {
198
- errors.push(validators.map(v => v.type).join(' OR '));
199
- }
200
- return { value, isSet: true, isValid: !errors.length, errors };
201
- };
202
- var AbstractEnum;
203
- (function (AbstractEnum) {
204
- })(AbstractEnum || (AbstractEnum = {}));
205
- const onEnum = (list) => (value) => {
206
- const errors = [];
207
- const values = Object.keys(list).filter(k => isNaN(Number(k))).map(k => list[k]);
208
- if (!values.some(item => item === value)) {
209
- errors.push(getError(ValidatorType.enum, ['[' + values.join(',') + ']']));
210
- }
211
- return { value, isSet: true, isValid: !errors.length, errors };
212
- };
213
- export const VALIDATORS = {
214
- NUMBER: {
215
- type: ValidatorType.number,
216
- method: onNumber
217
- },
218
- INTEGER: {
219
- type: ValidatorType.integer,
220
- method: onInteger
221
- },
222
- INTEGER_UNLIMITED: {
223
- type: ValidatorType.integerUnlimited,
224
- method: onIntegerUnlimited
225
- },
226
- MORE_OR_EQUAL: (limit, fallback) => ({
227
- type: ValidatorType.moreOrEqual,
228
- method: onMoreOrEqual(limit, fallback)
229
- }),
230
- BOOLEAN: {
231
- type: ValidatorType.boolean,
232
- method: onBoolean
233
- },
234
- OBJECT: {
235
- type: ValidatorType.object,
236
- method: onObject
237
- },
238
- ITEM_LIST: {
239
- type: ValidatorType.itemList,
240
- method: onItemList
241
- },
242
- ELEMENT: {
243
- type: ValidatorType.element,
244
- method: onHtmlElement
245
- },
246
- FUNC: {
247
- type: ValidatorType.function,
248
- method: onFunction
249
- },
250
- FUNC_WITH_X_ARGUMENTS: (count) => ({
251
- type: ValidatorType.funcOfxArguments,
252
- method: onFunctionWithXArguments(count)
253
- }),
254
- FUNC_WITH_X_AND_MORE_ARGUMENTS: (count) => ({
255
- type: ValidatorType.funcOfxAndMoreArguments,
256
- method: onFunctionWithXAndMoreArguments(count)
257
- }),
258
- FUNC_WITH_X_TO_Y_ARGUMENTS: (from, to) => ({
259
- type: ValidatorType.funcOfXToYArguments,
260
- method: onFunctionWithXToYArguments(from, to)
261
- }),
262
- ONE_OF_CAN: (list) => ({
263
- type: ValidatorType.oneOfCan,
264
- method: onOneOf(list, false)
265
- }),
266
- ONE_OF_MUST: (list) => ({
267
- type: ValidatorType.oneOfMust,
268
- method: onOneOf(list, true)
269
- }),
270
- OR: (list) => ({
271
- type: ValidatorType.or,
272
- method: onOr(list)
273
- }),
274
- ENUM: (list) => ({
275
- type: ValidatorType.enum,
276
- method: onEnum(list)
277
- })
278
- };
279
- export class ValidatedData {
280
- constructor(context) {
281
- this.params = {};
282
- this.contextErrors = [];
283
- this.errors = [];
284
- this.isValid = true;
285
- this.setContext(context);
286
- }
287
- setContext(context) {
288
- if (!context || Object.prototype.toString.call(context) !== '[object Object]') {
289
- this.setCommonError('context is not an object');
290
- this.isValidContext = false;
291
- }
292
- else {
293
- this.isValidContext = true;
294
- }
295
- this.context = context;
296
- }
297
- setValidity() {
298
- this.errors = Object.keys(this.params).reduce((acc, key) => [
299
- ...acc, ...this.params[key].errors
300
- ], []);
301
- this.isValid = !this.errors.length;
302
- }
303
- setCommonError(error) {
304
- this.contextErrors.push(error);
305
- this.errors.push(error);
306
- this.isValid = false;
307
- }
308
- setParam(token, value) {
309
- if (!value.isValid) {
310
- value.errors = !value.isSet
311
- ? [`"${token}" must be set`]
312
- : value.errors.map((err) => `"${token}" ${err}`);
313
- }
314
- this.params[token] = value;
315
- this.setValidity();
316
- }
317
- showErrors() {
318
- return this.errors.length
319
- ? 'validation failed: ' + this.errors.join(', ')
320
- : '';
321
- }
322
- }
323
- export const runValidator = (current, validator, context) => {
324
- const { value, errors } = current;
325
- const result = validator.method(value, context);
326
- const _errors = [...errors, ...result.errors];
327
- return {
328
- value: result.value,
329
- isSet: result.isSet,
330
- isValid: !_errors.length,
331
- errors: _errors
332
- };
333
- };
334
- const getDefault = (value, prop) => {
335
- const empty = value === void 0;
336
- const auto = !prop.mandatory && prop.defaultValue !== void 0;
337
- return {
338
- value: !empty ? value : (auto ? prop.defaultValue : void 0),
339
- isSet: !empty || auto,
340
- isValid: !empty || !prop.mandatory,
341
- errors: []
342
- };
343
- };
344
- export const validateOne = (context, name, prop) => {
345
- const result = getDefault(context[name], prop);
346
- if (!result.isSet) {
347
- const oneOfMust = prop.validators.find(v => v.type === ValidatorType.oneOfMust);
348
- if (oneOfMust) {
349
- return runValidator(result, oneOfMust, context);
350
- }
351
- }
352
- else {
353
- for (const validator of Object.values(prop.validators)) {
354
- const current = runValidator(result, validator, context);
355
- if (!current.isValid && prop.defaultValue !== void 0) {
356
- return {
357
- value: prop.defaultValue,
358
- isSet: true,
359
- isValid: true,
360
- errors: []
361
- };
362
- }
363
- Object.assign(result, current);
364
- }
365
- }
366
- return result;
367
- };
368
- export const validate = (context, params) => {
369
- const data = new ValidatedData(context);
370
- Object.entries(params).forEach(([key, prop]) => data.setParam(key, data.isValidContext
371
- ? validateOne(data.context, key, prop)
372
- : getDefault(void 0, prop)));
373
- return data;
374
- };
1
+ export var ValidatorType;
2
+ (function (ValidatorType) {
3
+ ValidatorType["number"] = "must be a number";
4
+ ValidatorType["integer"] = "must be an integer";
5
+ ValidatorType["integerUnlimited"] = "must be an integer or infinity";
6
+ ValidatorType["moreOrEqual"] = "must be a number greater than (or equal to) {arg1}";
7
+ ValidatorType["itemList"] = "must be an array of items {arg1}";
8
+ ValidatorType["boolean"] = "must be a boolean";
9
+ ValidatorType["object"] = "must be an object";
10
+ ValidatorType["element"] = "must be an html element";
11
+ ValidatorType["function"] = "must be a function";
12
+ ValidatorType["funcOfxArguments"] = "must have {arg1} argument(s)";
13
+ ValidatorType["funcOfxAndMoreArguments"] = "must have at least {arg1} argument(s)";
14
+ ValidatorType["funcOfXToYArguments"] = "must have {arg1} to {arg2} arguments";
15
+ ValidatorType["oneOfCan"] = "can be present as only one item of {arg1} list";
16
+ ValidatorType["oneOfMust"] = "must be present as only one item of {arg1} list";
17
+ ValidatorType["or"] = "must satisfy at least 1 validator from {arg1} list";
18
+ ValidatorType["enum"] = "must belong to {arg1} list";
19
+ })(ValidatorType || (ValidatorType = {}));
20
+ const getError = (msg, args) => (args || ['']).reduce((acc, arg, index) => acc.replace(`{arg${index + 1}}`, arg), msg);
21
+ const getNumber = (value) => typeof value === 'number' || (typeof value === 'string' && value !== '')
22
+ ? Number(value)
23
+ : NaN;
24
+ const onNumber = (value) => {
25
+ const parsedValue = getNumber(value);
26
+ const errors = [];
27
+ if (Number.isNaN(parsedValue)) {
28
+ errors.push(ValidatorType.number);
29
+ }
30
+ return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
31
+ };
32
+ const onInteger = (value) => {
33
+ const errors = [];
34
+ value = getNumber(value);
35
+ const parsedValue = parseInt(String(value), 10);
36
+ if (value !== parsedValue) {
37
+ errors.push(ValidatorType.integer);
38
+ }
39
+ return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
40
+ };
41
+ const onIntegerUnlimited = (value) => {
42
+ let parsedValue = value;
43
+ const errors = [];
44
+ value = getNumber(value);
45
+ if (!Number.isFinite(value)) {
46
+ parsedValue = value;
47
+ }
48
+ else {
49
+ parsedValue = parseInt(String(value), 10);
50
+ }
51
+ if (value !== parsedValue) {
52
+ errors.push(ValidatorType.integerUnlimited);
53
+ }
54
+ return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
55
+ };
56
+ const onMoreOrEqual = (limit, fallback) => (value) => {
57
+ const result = onNumber(value);
58
+ if (!result.isValid) {
59
+ return result;
60
+ }
61
+ let parsedValue = result.value;
62
+ const errors = [];
63
+ if (parsedValue < limit) {
64
+ if (!fallback) {
65
+ errors.push(getError(ValidatorType.moreOrEqual, [String(limit)]));
66
+ }
67
+ else {
68
+ parsedValue = limit;
69
+ }
70
+ }
71
+ return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
72
+ };
73
+ const onBoolean = (value) => {
74
+ const errors = [];
75
+ let parsedValue = value;
76
+ if (value === 'true') {
77
+ parsedValue = true;
78
+ }
79
+ else if (value === 'false') {
80
+ parsedValue = false;
81
+ }
82
+ if (typeof parsedValue !== 'boolean') {
83
+ errors.push(ValidatorType.boolean);
84
+ }
85
+ return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
86
+ };
87
+ const onObject = (value) => {
88
+ const errors = [];
89
+ if (Object.prototype.toString.call(value) !== '[object Object]') {
90
+ errors.push(ValidatorType.object);
91
+ }
92
+ return { value, isSet: true, isValid: !errors.length, errors };
93
+ };
94
+ const onHtmlElement = (value) => {
95
+ const errors = [];
96
+ if (!(value instanceof Element) && !(value instanceof HTMLDocument)) {
97
+ errors.push(ValidatorType.element);
98
+ }
99
+ return { value, isSet: true, isValid: !errors.length, errors };
100
+ };
101
+ const onItemList = (value) => {
102
+ let parsedValue = value;
103
+ const errors = [];
104
+ if (!Array.isArray(value)) {
105
+ errors.push(ValidatorType.itemList);
106
+ parsedValue = [];
107
+ }
108
+ else if (!value.length) {
109
+ errors.push(getError(ValidatorType.itemList, ['with at least 1 item']));
110
+ }
111
+ else if (value.length > 1) {
112
+ const type = typeof value[0];
113
+ for (let i = value.length - 1; i >= 0; i--) {
114
+ if (typeof value[i] !== type) {
115
+ errors.push(getError(ValidatorType.itemList, ['of items of the same type']));
116
+ break;
117
+ }
118
+ }
119
+ }
120
+ return { value: parsedValue, isSet: true, isValid: !errors.length, errors };
121
+ };
122
+ const onFunction = (value) => {
123
+ const errors = [];
124
+ if (typeof value !== 'function') {
125
+ errors.push(ValidatorType.function);
126
+ }
127
+ return { value: value, isSet: true, isValid: !errors.length, errors };
128
+ };
129
+ const onFunctionWithXArguments = (argsCount) => (value) => {
130
+ const result = onFunction(value);
131
+ if (!result.isValid) {
132
+ return result;
133
+ }
134
+ value = result.value;
135
+ const errors = [];
136
+ if (value.length !== argsCount) {
137
+ errors.push(getError(ValidatorType.funcOfxArguments, [String(argsCount)]));
138
+ }
139
+ return { value: value, isSet: true, isValid: !errors.length, errors };
140
+ };
141
+ const onFunctionWithXAndMoreArguments = (argsCount) => (value) => {
142
+ const result = onFunction(value);
143
+ if (!result.isValid) {
144
+ return result;
145
+ }
146
+ value = result.value;
147
+ const errors = [];
148
+ if (value.length < argsCount) {
149
+ errors.push(getError(ValidatorType.funcOfxArguments, [String(argsCount)]));
150
+ }
151
+ return { value: value, isSet: true, isValid: !errors.length, errors };
152
+ };
153
+ const onFunctionWithXToYArguments = (from, to) => (value) => {
154
+ const result = onFunction(value);
155
+ if (!result.isValid) {
156
+ return result;
157
+ }
158
+ value = result.value;
159
+ const errors = [];
160
+ if (value.length < from || value.length > to) {
161
+ errors.push(getError(ValidatorType.funcOfXToYArguments, [String(from), String(to)]));
162
+ }
163
+ return { value: value, isSet: true, isValid: !errors.length, errors };
164
+ };
165
+ const onOneOf = (tokens, must) => (value, context) => {
166
+ const errors = [];
167
+ const isSet = value !== void 0;
168
+ let noOneIsPresent = !isSet;
169
+ const err = must ? ValidatorType.oneOfMust : ValidatorType.oneOfCan;
170
+ if (!Array.isArray(tokens) || !tokens.length) {
171
+ errors.push(getError(err, ['undefined']));
172
+ }
173
+ else {
174
+ for (let i = tokens.length - 1; i >= 0; i--) {
175
+ const token = tokens[i];
176
+ if (typeof token !== 'string') {
177
+ errors.push(getError(err, [tokens.join('", "')]) + ' (non-string token)');
178
+ break;
179
+ }
180
+ const isAnotherPresent = context && Object.prototype.hasOwnProperty.call(context, token);
181
+ if (isSet && isAnotherPresent) {
182
+ errors.push(getError(err, [tokens.join('", "')]) + ` (${token} is present)`);
183
+ break;
184
+ }
185
+ if (noOneIsPresent && isAnotherPresent) {
186
+ noOneIsPresent = false;
187
+ }
188
+ }
189
+ if (must && noOneIsPresent) {
190
+ errors.push(getError(err, [tokens.join('", "')]));
191
+ }
192
+ }
193
+ return { value, isSet, isValid: !errors.length, errors };
194
+ };
195
+ const onOr = (validators) => (value) => {
196
+ const errors = [];
197
+ if (validators.every(validator => !validator.method(value).isValid)) {
198
+ errors.push(validators.map(v => v.type).join(' OR '));
199
+ }
200
+ return { value, isSet: true, isValid: !errors.length, errors };
201
+ };
202
+ var AbstractEnum;
203
+ (function (AbstractEnum) {
204
+ })(AbstractEnum || (AbstractEnum = {}));
205
+ const onEnum = (list) => (value) => {
206
+ const errors = [];
207
+ const values = Object.keys(list).filter(k => isNaN(Number(k))).map(k => list[k]);
208
+ if (!values.some(item => item === value)) {
209
+ errors.push(getError(ValidatorType.enum, ['[' + values.join(',') + ']']));
210
+ }
211
+ return { value, isSet: true, isValid: !errors.length, errors };
212
+ };
213
+ export const VALIDATORS = {
214
+ NUMBER: {
215
+ type: ValidatorType.number,
216
+ method: onNumber
217
+ },
218
+ INTEGER: {
219
+ type: ValidatorType.integer,
220
+ method: onInteger
221
+ },
222
+ INTEGER_UNLIMITED: {
223
+ type: ValidatorType.integerUnlimited,
224
+ method: onIntegerUnlimited
225
+ },
226
+ MORE_OR_EQUAL: (limit, fallback) => ({
227
+ type: ValidatorType.moreOrEqual,
228
+ method: onMoreOrEqual(limit, fallback)
229
+ }),
230
+ BOOLEAN: {
231
+ type: ValidatorType.boolean,
232
+ method: onBoolean
233
+ },
234
+ OBJECT: {
235
+ type: ValidatorType.object,
236
+ method: onObject
237
+ },
238
+ ITEM_LIST: {
239
+ type: ValidatorType.itemList,
240
+ method: onItemList
241
+ },
242
+ ELEMENT: {
243
+ type: ValidatorType.element,
244
+ method: onHtmlElement
245
+ },
246
+ FUNC: {
247
+ type: ValidatorType.function,
248
+ method: onFunction
249
+ },
250
+ FUNC_WITH_X_ARGUMENTS: (count) => ({
251
+ type: ValidatorType.funcOfxArguments,
252
+ method: onFunctionWithXArguments(count)
253
+ }),
254
+ FUNC_WITH_X_AND_MORE_ARGUMENTS: (count) => ({
255
+ type: ValidatorType.funcOfxAndMoreArguments,
256
+ method: onFunctionWithXAndMoreArguments(count)
257
+ }),
258
+ FUNC_WITH_X_TO_Y_ARGUMENTS: (from, to) => ({
259
+ type: ValidatorType.funcOfXToYArguments,
260
+ method: onFunctionWithXToYArguments(from, to)
261
+ }),
262
+ ONE_OF_CAN: (list) => ({
263
+ type: ValidatorType.oneOfCan,
264
+ method: onOneOf(list, false)
265
+ }),
266
+ ONE_OF_MUST: (list) => ({
267
+ type: ValidatorType.oneOfMust,
268
+ method: onOneOf(list, true)
269
+ }),
270
+ OR: (list) => ({
271
+ type: ValidatorType.or,
272
+ method: onOr(list)
273
+ }),
274
+ ENUM: (list) => ({
275
+ type: ValidatorType.enum,
276
+ method: onEnum(list)
277
+ })
278
+ };
279
+ export class ValidatedData {
280
+ constructor(context) {
281
+ this.params = {};
282
+ this.contextErrors = [];
283
+ this.errors = [];
284
+ this.isValid = true;
285
+ this.setContext(context);
286
+ }
287
+ setContext(context) {
288
+ if (!context || Object.prototype.toString.call(context) !== '[object Object]') {
289
+ this.setCommonError('context is not an object');
290
+ this.isValidContext = false;
291
+ }
292
+ else {
293
+ this.isValidContext = true;
294
+ }
295
+ this.context = context;
296
+ }
297
+ setValidity() {
298
+ this.errors = Object.keys(this.params).reduce((acc, key) => [
299
+ ...acc, ...this.params[key].errors
300
+ ], []);
301
+ this.isValid = !this.errors.length;
302
+ }
303
+ setCommonError(error) {
304
+ this.contextErrors.push(error);
305
+ this.errors.push(error);
306
+ this.isValid = false;
307
+ }
308
+ setParam(token, value) {
309
+ if (!value.isValid) {
310
+ value.errors = !value.isSet
311
+ ? [`"${token}" must be set`]
312
+ : value.errors.map((err) => `"${token}" ${err}`);
313
+ }
314
+ this.params[token] = value;
315
+ this.setValidity();
316
+ }
317
+ showErrors() {
318
+ return this.errors.length
319
+ ? 'validation failed: ' + this.errors.join(', ')
320
+ : '';
321
+ }
322
+ }
323
+ export const runValidator = (current, validator, context) => {
324
+ const { value, errors } = current;
325
+ const result = validator.method(value, context);
326
+ const _errors = [...errors, ...result.errors];
327
+ return {
328
+ value: result.value,
329
+ isSet: result.isSet,
330
+ isValid: !_errors.length,
331
+ errors: _errors
332
+ };
333
+ };
334
+ const getDefault = (value, prop) => {
335
+ const empty = value === void 0;
336
+ const auto = !prop.mandatory && prop.defaultValue !== void 0;
337
+ return {
338
+ value: !empty ? value : (auto ? prop.defaultValue : void 0),
339
+ isSet: !empty || auto,
340
+ isValid: !empty || !prop.mandatory,
341
+ errors: []
342
+ };
343
+ };
344
+ export const validateOne = (context, name, prop) => {
345
+ const result = getDefault(context[name], prop);
346
+ if (!result.isSet) {
347
+ const oneOfMust = prop.validators.find(v => v.type === ValidatorType.oneOfMust);
348
+ if (oneOfMust) {
349
+ return runValidator(result, oneOfMust, context);
350
+ }
351
+ }
352
+ else {
353
+ for (const validator of Object.values(prop.validators)) {
354
+ const current = runValidator(result, validator, context);
355
+ if (!current.isValid && prop.defaultValue !== void 0) {
356
+ return {
357
+ value: prop.defaultValue,
358
+ isSet: true,
359
+ isValid: true,
360
+ errors: []
361
+ };
362
+ }
363
+ Object.assign(result, current);
364
+ }
365
+ }
366
+ return result;
367
+ };
368
+ export const validate = (context, params) => {
369
+ const data = new ValidatedData(context);
370
+ Object.entries(params).forEach(([key, prop]) => data.setParam(key, data.isValidContext
371
+ ? validateOne(data.context, key, prop)
372
+ : getDefault(void 0, prop)));
373
+ return data;
374
+ };
375
375
  //# sourceMappingURL=validation.js.map