uview-ui-wjf 1.0.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 (368) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +71 -0
  3. package/changelog.md +388 -0
  4. package/components/u--form/u--form.vue +92 -0
  5. package/components/u--image/u--image.vue +73 -0
  6. package/components/u--input/u--input.vue +115 -0
  7. package/components/u--text/u--text.vue +72 -0
  8. package/components/u--textarea/u--textarea.vue +85 -0
  9. package/components/u-action-sheet/props.js +54 -0
  10. package/components/u-action-sheet/u-action-sheet.vue +278 -0
  11. package/components/u-album/props.js +59 -0
  12. package/components/u-album/u-album.vue +259 -0
  13. package/components/u-alert/props.js +44 -0
  14. package/components/u-alert/u-alert.vue +243 -0
  15. package/components/u-avatar/props.js +78 -0
  16. package/components/u-avatar/u-avatar.vue +173 -0
  17. package/components/u-avatar-group/props.js +52 -0
  18. package/components/u-avatar-group/u-avatar-group.vue +103 -0
  19. package/components/u-back-top/props.js +54 -0
  20. package/components/u-back-top/u-back-top.vue +129 -0
  21. package/components/u-badge/props.js +72 -0
  22. package/components/u-badge/u-badge.vue +171 -0
  23. package/components/u-button/nvue.scss +46 -0
  24. package/components/u-button/props.js +161 -0
  25. package/components/u-button/u-button.vue +495 -0
  26. package/components/u-button/vue.scss +80 -0
  27. package/components/u-calendar/header.vue +99 -0
  28. package/components/u-calendar/month.vue +579 -0
  29. package/components/u-calendar/props.js +144 -0
  30. package/components/u-calendar/u-calendar.vue +384 -0
  31. package/components/u-calendar/util.js +85 -0
  32. package/components/u-car-keyboard/props.js +14 -0
  33. package/components/u-car-keyboard/u-car-keyboard.vue +311 -0
  34. package/components/u-cell/props.js +110 -0
  35. package/components/u-cell/u-cell.vue +229 -0
  36. package/components/u-cell-group/props.js +14 -0
  37. package/components/u-cell-group/u-cell-group.vue +61 -0
  38. package/components/u-checkbox/props.js +69 -0
  39. package/components/u-checkbox/u-checkbox.vue +344 -0
  40. package/components/u-checkbox-group/props.js +82 -0
  41. package/components/u-checkbox-group/u-checkbox-group.vue +103 -0
  42. package/components/u-circle-progress/props.js +8 -0
  43. package/components/u-circle-progress/u-circle-progress.vue +198 -0
  44. package/components/u-code/props.js +34 -0
  45. package/components/u-code/u-code.vue +129 -0
  46. package/components/u-code-input/props.js +79 -0
  47. package/components/u-code-input/u-code-input.vue +252 -0
  48. package/components/u-col/props.js +29 -0
  49. package/components/u-col/u-col.vue +162 -0
  50. package/components/u-collapse/props.js +19 -0
  51. package/components/u-collapse/u-collapse.vue +90 -0
  52. package/components/u-collapse-item/props.js +59 -0
  53. package/components/u-collapse-item/u-collapse-item.vue +225 -0
  54. package/components/u-column-notice/props.js +55 -0
  55. package/components/u-column-notice/u-column-notice.vue +160 -0
  56. package/components/u-count-down/props.js +24 -0
  57. package/components/u-count-down/u-count-down.vue +163 -0
  58. package/components/u-count-down/utils.js +62 -0
  59. package/components/u-count-to/props.js +59 -0
  60. package/components/u-count-to/u-count-to.vue +184 -0
  61. package/components/u-datetime-picker/props.js +120 -0
  62. package/components/u-datetime-picker/u-datetime-picker.vue +361 -0
  63. package/components/u-divider/props.js +44 -0
  64. package/components/u-divider/u-divider.vue +116 -0
  65. package/components/u-dropdown/props.js +65 -0
  66. package/components/u-dropdown/u-dropdown.vue +127 -0
  67. package/components/u-dropdown-item/props.js +36 -0
  68. package/components/u-dropdown-item/u-dropdown-item.vue +127 -0
  69. package/components/u-empty/props.js +59 -0
  70. package/components/u-empty/u-empty.vue +128 -0
  71. package/components/u-form/props.js +45 -0
  72. package/components/u-form/u-form.vue +214 -0
  73. package/components/u-form-item/props.js +48 -0
  74. package/components/u-form-item/u-form-item.vue +235 -0
  75. package/components/u-gap/props.js +24 -0
  76. package/components/u-gap/u-gap.vue +38 -0
  77. package/components/u-grid/props.js +19 -0
  78. package/components/u-grid/u-grid.vue +97 -0
  79. package/components/u-grid-item/props.js +14 -0
  80. package/components/u-grid-item/u-grid-item.vue +209 -0
  81. package/components/u-icon/icons.js +214 -0
  82. package/components/u-icon/props.js +89 -0
  83. package/components/u-icon/u-icon.vue +237 -0
  84. package/components/u-image/props.js +84 -0
  85. package/components/u-image/u-image.vue +232 -0
  86. package/components/u-index-anchor/props.js +29 -0
  87. package/components/u-index-anchor/u-index-anchor.vue +91 -0
  88. package/components/u-index-item/props.js +5 -0
  89. package/components/u-index-item/u-index-item.vue +87 -0
  90. package/components/u-index-list/props.js +29 -0
  91. package/components/u-index-list/u-index-list.vue +440 -0
  92. package/components/u-input/props.js +187 -0
  93. package/components/u-input/u-input.vue +369 -0
  94. package/components/u-keyboard/props.js +84 -0
  95. package/components/u-keyboard/u-keyboard.vue +164 -0
  96. package/components/u-line/props.js +33 -0
  97. package/components/u-line/u-line.vue +62 -0
  98. package/components/u-line-progress/props.js +28 -0
  99. package/components/u-line-progress/u-line-progress.vue +144 -0
  100. package/components/u-link/props.js +39 -0
  101. package/components/u-link/u-link.vue +83 -0
  102. package/components/u-list/props.js +76 -0
  103. package/components/u-list/u-list.vue +157 -0
  104. package/components/u-list-item/props.js +9 -0
  105. package/components/u-list-item/u-list-item.vue +116 -0
  106. package/components/u-loading-icon/props.js +59 -0
  107. package/components/u-loading-icon/u-loading-icon.vue +343 -0
  108. package/components/u-loading-page/props.js +49 -0
  109. package/components/u-loading-page/u-loading-page.vue +115 -0
  110. package/components/u-loadmore/props.js +94 -0
  111. package/components/u-loadmore/u-loadmore.vue +150 -0
  112. package/components/u-modal/props.js +89 -0
  113. package/components/u-modal/u-modal.vue +228 -0
  114. package/components/u-navbar/props.js +84 -0
  115. package/components/u-navbar/u-navbar.vue +186 -0
  116. package/components/u-no-network/props.js +19 -0
  117. package/components/u-no-network/u-no-network.vue +220 -0
  118. package/components/u-notice-bar/props.js +70 -0
  119. package/components/u-notice-bar/u-notice-bar.vue +101 -0
  120. package/components/u-notify/props.js +49 -0
  121. package/components/u-notify/u-notify.vue +211 -0
  122. package/components/u-number-box/props.js +109 -0
  123. package/components/u-number-box/u-number-box.vue +416 -0
  124. package/components/u-number-keyboard/props.js +19 -0
  125. package/components/u-number-keyboard/u-number-keyboard.vue +196 -0
  126. package/components/u-overlay/props.js +24 -0
  127. package/components/u-overlay/u-overlay.vue +68 -0
  128. package/components/u-parse/node/node.vue +499 -0
  129. package/components/u-parse/parser.js +1075 -0
  130. package/components/u-parse/props.js +45 -0
  131. package/components/u-parse/u-parse.vue +366 -0
  132. package/components/u-picker/props.js +79 -0
  133. package/components/u-picker/u-picker.vue +286 -0
  134. package/components/u-picker-column/props.js +5 -0
  135. package/components/u-picker-column/u-picker-column.vue +27 -0
  136. package/components/u-popup/props.js +79 -0
  137. package/components/u-popup/u-popup.vue +304 -0
  138. package/components/u-radio/props.js +64 -0
  139. package/components/u-radio/u-radio.vue +339 -0
  140. package/components/u-radio-group/props.js +85 -0
  141. package/components/u-radio-group/u-radio-group.vue +108 -0
  142. package/components/u-rate/props.js +69 -0
  143. package/components/u-rate/u-rate.vue +306 -0
  144. package/components/u-read-more/props.js +61 -0
  145. package/components/u-read-more/u-read-more.vue +157 -0
  146. package/components/u-row/props.js +19 -0
  147. package/components/u-row/u-row.vue +93 -0
  148. package/components/u-row-notice/props.js +39 -0
  149. package/components/u-row-notice/u-row-notice.vue +330 -0
  150. package/components/u-safe-bottom/props.js +5 -0
  151. package/components/u-safe-bottom/u-safe-bottom.vue +56 -0
  152. package/components/u-scroll-list/nvue.js +28 -0
  153. package/components/u-scroll-list/other.js +0 -0
  154. package/components/u-scroll-list/props.js +34 -0
  155. package/components/u-scroll-list/scrollWxs.wxs +50 -0
  156. package/components/u-scroll-list/u-scroll-list.vue +224 -0
  157. package/components/u-search/props.js +118 -0
  158. package/components/u-search/u-search.vue +303 -0
  159. package/components/u-skeleton/props.js +59 -0
  160. package/components/u-skeleton/u-skeleton.vue +244 -0
  161. package/components/u-slider/mpother.js +113 -0
  162. package/components/u-slider/mpwxs.js +42 -0
  163. package/components/u-slider/mpwxs.wxs +121 -0
  164. package/components/u-slider/nvue - /345/211/257/346/234/254.js" +180 -0
  165. package/components/u-slider/nvue.js +193 -0
  166. package/components/u-slider/props.js +54 -0
  167. package/components/u-slider/u-slider.vue +55 -0
  168. package/components/u-status-bar/props.js +8 -0
  169. package/components/u-status-bar/u-status-bar.vue +46 -0
  170. package/components/u-steps/props.js +39 -0
  171. package/components/u-steps/u-steps.vue +80 -0
  172. package/components/u-steps-item/props.js +24 -0
  173. package/components/u-steps-item/u-steps-item.vue +316 -0
  174. package/components/u-sticky/props.js +40 -0
  175. package/components/u-sticky/u-sticky.vue +212 -0
  176. package/components/u-subsection/props.js +49 -0
  177. package/components/u-subsection/u-subsection.vue +299 -0
  178. package/components/u-swipe-action/props.js +9 -0
  179. package/components/u-swipe-action/u-swipe-action.vue +67 -0
  180. package/components/u-swipe-action-item/index - backup.wxs +256 -0
  181. package/components/u-swipe-action-item/index.wxs +225 -0
  182. package/components/u-swipe-action-item/nvue - backup.js +270 -0
  183. package/components/u-swipe-action-item/nvue.js +174 -0
  184. package/components/u-swipe-action-item/props.js +41 -0
  185. package/components/u-swipe-action-item/u-swipe-action-item.vue +190 -0
  186. package/components/u-swipe-action-item/wxs.js +15 -0
  187. package/components/u-swiper/props.js +125 -0
  188. package/components/u-swiper/u-swiper.vue +255 -0
  189. package/components/u-swiper-indicator/props.js +29 -0
  190. package/components/u-swiper-indicator/u-swiper-indicator.vue +110 -0
  191. package/components/u-switch/props.js +54 -0
  192. package/components/u-switch/u-switch.vue +177 -0
  193. package/components/u-tabbar/props.js +44 -0
  194. package/components/u-tabbar/u-tabbar.vue +141 -0
  195. package/components/u-tabbar-item/props.js +35 -0
  196. package/components/u-tabbar-item/u-tabbar-item.vue +142 -0
  197. package/components/u-table/props.js +5 -0
  198. package/components/u-table/u-table.vue +29 -0
  199. package/components/u-tabs/props.js +64 -0
  200. package/components/u-tabs/u-tabs.vue +363 -0
  201. package/components/u-tabs-item/props.js +5 -0
  202. package/components/u-tabs-item/u-tabs-item.vue +29 -0
  203. package/components/u-tag/props.js +84 -0
  204. package/components/u-tag/u-tag.vue +358 -0
  205. package/components/u-td/props.js +5 -0
  206. package/components/u-td/u-td.vue +31 -0
  207. package/components/u-text/props.js +110 -0
  208. package/components/u-text/u-text.vue +223 -0
  209. package/components/u-text/value.js +85 -0
  210. package/components/u-textarea/props.js +119 -0
  211. package/components/u-textarea/u-textarea.vue +239 -0
  212. package/components/u-toast/u-toast.vue +291 -0
  213. package/components/u-toolbar/props.js +34 -0
  214. package/components/u-toolbar/u-toolbar.vue +102 -0
  215. package/components/u-tooltip/clipboard.min.js +58 -0
  216. package/components/u-tooltip/props.js +59 -0
  217. package/components/u-tooltip/u-tooltip.vue +366 -0
  218. package/components/u-tr/props.js +5 -0
  219. package/components/u-tr/u-tr.vue +31 -0
  220. package/components/u-transition/nvue.ani-map.js +68 -0
  221. package/components/u-transition/props.js +24 -0
  222. package/components/u-transition/transition.js +157 -0
  223. package/components/u-transition/u-transition.vue +92 -0
  224. package/components/u-transition/vue.ani-style.scss +113 -0
  225. package/components/u-upload/mixin.js +21 -0
  226. package/components/u-upload/props.js +124 -0
  227. package/components/u-upload/u-upload.vue +566 -0
  228. package/components/u-upload/utils.js +151 -0
  229. package/components/uview-ui/uview-ui.vue +15 -0
  230. package/index.js +79 -0
  231. package/index.scss +23 -0
  232. package/libs/config/color.js +17 -0
  233. package/libs/config/config.js +34 -0
  234. package/libs/config/props/actionSheet.js +25 -0
  235. package/libs/config/props/album.js +25 -0
  236. package/libs/config/props/alert.js +22 -0
  237. package/libs/config/props/avatar.js +28 -0
  238. package/libs/config/props/avatarGroup.js +23 -0
  239. package/libs/config/props/backtop.js +27 -0
  240. package/libs/config/props/badge.js +27 -0
  241. package/libs/config/props/button.js +42 -0
  242. package/libs/config/props/calendar.js +42 -0
  243. package/libs/config/props/carKeyboard.js +15 -0
  244. package/libs/config/props/cell.js +35 -0
  245. package/libs/config/props/cellGroup.js +17 -0
  246. package/libs/config/props/checkbox.js +27 -0
  247. package/libs/config/props/checkboxGroup.js +29 -0
  248. package/libs/config/props/circleProgress.js +15 -0
  249. package/libs/config/props/code.js +21 -0
  250. package/libs/config/props/codeInput.js +29 -0
  251. package/libs/config/props/col.js +19 -0
  252. package/libs/config/props/collapse.js +17 -0
  253. package/libs/config/props/collapseItem.js +25 -0
  254. package/libs/config/props/columnNotice.js +24 -0
  255. package/libs/config/props/countDown.js +18 -0
  256. package/libs/config/props/countTo.js +25 -0
  257. package/libs/config/props/datetimePicker.js +37 -0
  258. package/libs/config/props/divider.js +23 -0
  259. package/libs/config/props/empty.js +26 -0
  260. package/libs/config/props/form.js +22 -0
  261. package/libs/config/props/formItem.js +23 -0
  262. package/libs/config/props/gap.js +19 -0
  263. package/libs/config/props/grid.js +17 -0
  264. package/libs/config/props/gridItem.js +16 -0
  265. package/libs/config/props/icon.js +36 -0
  266. package/libs/config/props/image.js +30 -0
  267. package/libs/config/props/indexAnchor.js +19 -0
  268. package/libs/config/props/indexList.js +19 -0
  269. package/libs/config/props/input.js +48 -0
  270. package/libs/config/props/keyboard.js +30 -0
  271. package/libs/config/props/line.js +20 -0
  272. package/libs/config/props/lineProgress.js +19 -0
  273. package/libs/config/props/link.js +26 -0
  274. package/libs/config/props/list.js +28 -0
  275. package/libs/config/props/listItem.js +15 -0
  276. package/libs/config/props/loadingIcon.js +30 -0
  277. package/libs/config/props/loadingPage.js +23 -0
  278. package/libs/config/props/loadmore.js +32 -0
  279. package/libs/config/props/modal.js +31 -0
  280. package/libs/config/props/navbar.js +32 -0
  281. package/libs/config/props/noNetwork.js +18 -0
  282. package/libs/config/props/noticeBar.js +27 -0
  283. package/libs/config/props/notify.js +22 -0
  284. package/libs/config/props/numberBox.js +35 -0
  285. package/libs/config/props/numberKeyboard.js +17 -0
  286. package/libs/config/props/overlay.js +18 -0
  287. package/libs/config/props/parse.js +22 -0
  288. package/libs/config/props/picker.js +29 -0
  289. package/libs/config/props/popup.js +29 -0
  290. package/libs/config/props/radio.js +27 -0
  291. package/libs/config/props/radioGroup.js +30 -0
  292. package/libs/config/props/rate.js +26 -0
  293. package/libs/config/props/readMore.js +22 -0
  294. package/libs/config/props/row.js +17 -0
  295. package/libs/config/props/rowNotice.js +21 -0
  296. package/libs/config/props/scrollList.js +20 -0
  297. package/libs/config/props/search.js +37 -0
  298. package/libs/config/props/section.js +24 -0
  299. package/libs/config/props/skeleton.js +25 -0
  300. package/libs/config/props/slider.js +25 -0
  301. package/libs/config/props/statusBar.js +15 -0
  302. package/libs/config/props/steps.js +21 -0
  303. package/libs/config/props/stepsItem.js +18 -0
  304. package/libs/config/props/sticky.js +20 -0
  305. package/libs/config/props/subsection.js +23 -0
  306. package/libs/config/props/swipeAction.js +15 -0
  307. package/libs/config/props/swipeActionItem.js +21 -0
  308. package/libs/config/props/swiper.js +39 -0
  309. package/libs/config/props/swipterIndicator.js +19 -0
  310. package/libs/config/props/switch.js +24 -0
  311. package/libs/config/props/tabbar.js +22 -0
  312. package/libs/config/props/tabbarItem.js +20 -0
  313. package/libs/config/props/tabs.js +32 -0
  314. package/libs/config/props/tag.js +29 -0
  315. package/libs/config/props/text.js +38 -0
  316. package/libs/config/props/textarea.js +36 -0
  317. package/libs/config/props/toast.js +30 -0
  318. package/libs/config/props/toolbar.js +21 -0
  319. package/libs/config/props/tooltip.js +25 -0
  320. package/libs/config/props/transition.js +18 -0
  321. package/libs/config/props/upload.js +36 -0
  322. package/libs/config/props.js +190 -0
  323. package/libs/config/zIndex.js +20 -0
  324. package/libs/css/color.scss +155 -0
  325. package/libs/css/common.scss +97 -0
  326. package/libs/css/components.scss +15 -0
  327. package/libs/css/flex.scss +257 -0
  328. package/libs/css/h5.scss +0 -0
  329. package/libs/css/mixin.scss +8 -0
  330. package/libs/css/mp.scss +0 -0
  331. package/libs/css/nvue.scss +0 -0
  332. package/libs/css/vue.scss +27 -0
  333. package/libs/function/colorGradient.js +134 -0
  334. package/libs/function/debounce.js +29 -0
  335. package/libs/function/digit.js +167 -0
  336. package/libs/function/index.js +731 -0
  337. package/libs/function/platform.js +75 -0
  338. package/libs/function/test.js +288 -0
  339. package/libs/function/throttle.js +30 -0
  340. package/libs/luch-request/adapters/index.js +97 -0
  341. package/libs/luch-request/core/InterceptorManager.js +50 -0
  342. package/libs/luch-request/core/Request.js +198 -0
  343. package/libs/luch-request/core/buildFullPath.js +20 -0
  344. package/libs/luch-request/core/defaults.js +29 -0
  345. package/libs/luch-request/core/dispatchRequest.js +3 -0
  346. package/libs/luch-request/core/mergeConfig.js +103 -0
  347. package/libs/luch-request/core/settle.js +16 -0
  348. package/libs/luch-request/helpers/buildURL.js +69 -0
  349. package/libs/luch-request/helpers/combineURLs.js +14 -0
  350. package/libs/luch-request/helpers/isAbsoluteURL.js +14 -0
  351. package/libs/luch-request/index.d.ts +116 -0
  352. package/libs/luch-request/index.js +3 -0
  353. package/libs/luch-request/utils/clone.js +264 -0
  354. package/libs/luch-request/utils.js +131 -0
  355. package/libs/mixin/button.js +13 -0
  356. package/libs/mixin/mixin.js +160 -0
  357. package/libs/mixin/mpMixin.js +8 -0
  358. package/libs/mixin/mpShare.js +13 -0
  359. package/libs/mixin/openType.js +25 -0
  360. package/libs/mixin/style.js +228 -0
  361. package/libs/mixin/touch.js +59 -0
  362. package/libs/util/async-validator.js +1343 -0
  363. package/libs/util/calendar.js +546 -0
  364. package/libs/util/dayjs.js +308 -0
  365. package/libs/util/emitter.js +51 -0
  366. package/libs/util/route.js +124 -0
  367. package/package.json +83 -0
  368. package/theme.scss +44 -0
@@ -0,0 +1,1343 @@
1
+ function _extends() {
2
+ _extends = Object.assign || function (target) {
3
+ for (let i = 1; i < arguments.length; i++) {
4
+ const source = arguments[i]
5
+
6
+ for (const key in source) {
7
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
8
+ target[key] = source[key]
9
+ }
10
+ }
11
+ }
12
+
13
+ return target
14
+ }
15
+
16
+ return _extends.apply(this, arguments)
17
+ }
18
+
19
+ /* eslint no-console:0 */
20
+ const formatRegExp = /%[sdj%]/g
21
+ let warning = function warning() {} // don't print warning message when in production env or node runtime
22
+
23
+ if (typeof process !== 'undefined' && process.env && process.env.NODE_ENV !== 'production' && typeof window
24
+ !== 'undefined' && typeof document !== 'undefined') {
25
+ warning = function warning(type, errors) {
26
+ if (typeof console !== 'undefined' && console.warn) {
27
+ if (errors.every((e) => typeof e === 'string')) {
28
+ console.warn(type, errors)
29
+ }
30
+ }
31
+ }
32
+ }
33
+
34
+ function convertFieldsError(errors) {
35
+ if (!errors || !errors.length) return null
36
+ const fields = {}
37
+ errors.forEach((error) => {
38
+ const { field } = error
39
+ fields[field] = fields[field] || []
40
+ fields[field].push(error)
41
+ })
42
+ return fields
43
+ }
44
+
45
+ function format() {
46
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
47
+ args[_key] = arguments[_key]
48
+ }
49
+
50
+ let i = 1
51
+ const f = args[0]
52
+ const len = args.length
53
+
54
+ if (typeof f === 'function') {
55
+ return f.apply(null, args.slice(1))
56
+ }
57
+
58
+ if (typeof f === 'string') {
59
+ let str = String(f).replace(formatRegExp, (x) => {
60
+ if (x === '%%') {
61
+ return '%'
62
+ }
63
+
64
+ if (i >= len) {
65
+ return x
66
+ }
67
+
68
+ switch (x) {
69
+ case '%s':
70
+ return String(args[i++])
71
+
72
+ case '%d':
73
+ return Number(args[i++])
74
+
75
+ case '%j':
76
+ try {
77
+ return JSON.stringify(args[i++])
78
+ } catch (_) {
79
+ return '[Circular]'
80
+ }
81
+
82
+ break
83
+
84
+ default:
85
+ return x
86
+ }
87
+ })
88
+
89
+ for (let arg = args[i]; i < len; arg = args[++i]) {
90
+ str += ` ${arg}`
91
+ }
92
+
93
+ return str
94
+ }
95
+
96
+ return f
97
+ }
98
+
99
+ function isNativeStringType(type) {
100
+ return type === 'string' || type === 'url' || type === 'hex' || type === 'email' || type === 'pattern'
101
+ }
102
+
103
+ function isEmptyValue(value, type) {
104
+ if (value === undefined || value === null) {
105
+ return true
106
+ }
107
+
108
+ if (type === 'array' && Array.isArray(value) && !value.length) {
109
+ return true
110
+ }
111
+
112
+ if (isNativeStringType(type) && typeof value === 'string' && !value) {
113
+ return true
114
+ }
115
+
116
+ return false
117
+ }
118
+
119
+ function asyncParallelArray(arr, func, callback) {
120
+ const results = []
121
+ let total = 0
122
+ const arrLength = arr.length
123
+
124
+ function count(errors) {
125
+ results.push.apply(results, errors)
126
+ total++
127
+
128
+ if (total === arrLength) {
129
+ callback(results)
130
+ }
131
+ }
132
+
133
+ arr.forEach((a) => {
134
+ func(a, count)
135
+ })
136
+ }
137
+
138
+ function asyncSerialArray(arr, func, callback) {
139
+ let index = 0
140
+ const arrLength = arr.length
141
+
142
+ function next(errors) {
143
+ if (errors && errors.length) {
144
+ callback(errors)
145
+ return
146
+ }
147
+
148
+ const original = index
149
+ index += 1
150
+
151
+ if (original < arrLength) {
152
+ func(arr[original], next)
153
+ } else {
154
+ callback([])
155
+ }
156
+ }
157
+
158
+ next([])
159
+ }
160
+
161
+ function flattenObjArr(objArr) {
162
+ const ret = []
163
+ Object.keys(objArr).forEach((k) => {
164
+ ret.push.apply(ret, objArr[k])
165
+ })
166
+ return ret
167
+ }
168
+
169
+ function asyncMap(objArr, option, func, callback) {
170
+ if (option.first) {
171
+ const _pending = new Promise((resolve, reject) => {
172
+ const next = function next(errors) {
173
+ callback(errors)
174
+ return errors.length ? reject({
175
+ errors,
176
+ fields: convertFieldsError(errors)
177
+ }) : resolve()
178
+ }
179
+
180
+ const flattenArr = flattenObjArr(objArr)
181
+ asyncSerialArray(flattenArr, func, next)
182
+ })
183
+
184
+ _pending.catch((e) => e)
185
+
186
+ return _pending
187
+ }
188
+
189
+ let firstFields = option.firstFields || []
190
+
191
+ if (firstFields === true) {
192
+ firstFields = Object.keys(objArr)
193
+ }
194
+
195
+ const objArrKeys = Object.keys(objArr)
196
+ const objArrLength = objArrKeys.length
197
+ let total = 0
198
+ const results = []
199
+ const pending = new Promise((resolve, reject) => {
200
+ const next = function next(errors) {
201
+ results.push.apply(results, errors)
202
+ total++
203
+
204
+ if (total === objArrLength) {
205
+ callback(results)
206
+ return results.length ? reject({
207
+ errors: results,
208
+ fields: convertFieldsError(results)
209
+ }) : resolve()
210
+ }
211
+ }
212
+
213
+ if (!objArrKeys.length) {
214
+ callback(results)
215
+ resolve()
216
+ }
217
+
218
+ objArrKeys.forEach((key) => {
219
+ const arr = objArr[key]
220
+
221
+ if (firstFields.indexOf(key) !== -1) {
222
+ asyncSerialArray(arr, func, next)
223
+ } else {
224
+ asyncParallelArray(arr, func, next)
225
+ }
226
+ })
227
+ })
228
+ pending.catch((e) => e)
229
+ return pending
230
+ }
231
+
232
+ function complementError(rule) {
233
+ return function (oe) {
234
+ if (oe && oe.message) {
235
+ oe.field = oe.field || rule.fullField
236
+ return oe
237
+ }
238
+
239
+ return {
240
+ message: typeof oe === 'function' ? oe() : oe,
241
+ field: oe.field || rule.fullField
242
+ }
243
+ }
244
+ }
245
+
246
+ function deepMerge(target, source) {
247
+ if (source) {
248
+ for (const s in source) {
249
+ if (source.hasOwnProperty(s)) {
250
+ const value = source[s]
251
+
252
+ if (typeof value === 'object' && typeof target[s] === 'object') {
253
+ target[s] = { ...target[s], ...value }
254
+ } else {
255
+ target[s] = value
256
+ }
257
+ }
258
+ }
259
+ }
260
+
261
+ return target
262
+ }
263
+
264
+ /**
265
+ * Rule for validating required fields.
266
+ *
267
+ * @param rule The validation rule.
268
+ * @param value The value of the field on the source object.
269
+ * @param source The source object being validated.
270
+ * @param errors An array of errors that this rule may add
271
+ * validation errors to.
272
+ * @param options The validation options.
273
+ * @param options.messages The validation messages.
274
+ */
275
+
276
+ function required(rule, value, source, errors, options, type) {
277
+ if (rule.required && (!source.hasOwnProperty(rule.field) || isEmptyValue(value, type || rule.type))) {
278
+ errors.push(format(options.messages.required, rule.fullField))
279
+ }
280
+ }
281
+
282
+ /**
283
+ * Rule for validating whitespace.
284
+ *
285
+ * @param rule The validation rule.
286
+ * @param value The value of the field on the source object.
287
+ * @param source The source object being validated.
288
+ * @param errors An array of errors that this rule may add
289
+ * validation errors to.
290
+ * @param options The validation options.
291
+ * @param options.messages The validation messages.
292
+ */
293
+
294
+ function whitespace(rule, value, source, errors, options) {
295
+ if (/^\s+$/.test(value) || value === '') {
296
+ errors.push(format(options.messages.whitespace, rule.fullField))
297
+ }
298
+ }
299
+
300
+ /* eslint max-len:0 */
301
+
302
+ const pattern = {
303
+ // http://emailregex.com/
304
+ email: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
305
+ url: new RegExp(
306
+ '^(?!mailto:)(?:(?:http|https|ftp)://|//)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$',
307
+ 'i'
308
+ ),
309
+ hex: /^#?([a-f0-9]{6}|[a-f0-9]{3})$/i
310
+ }
311
+ var types = {
312
+ integer: function integer(value) {
313
+ return /^(-)?\d+$/.test(value);
314
+ },
315
+ float: function float(value) {
316
+ return /^(-)?\d+(\.\d+)?$/.test(value);
317
+ },
318
+ array: function array(value) {
319
+ return Array.isArray(value)
320
+ },
321
+ regexp: function regexp(value) {
322
+ if (value instanceof RegExp) {
323
+ return true
324
+ }
325
+
326
+ try {
327
+ return !!new RegExp(value)
328
+ } catch (e) {
329
+ return false
330
+ }
331
+ },
332
+ date: function date(value) {
333
+ return typeof value.getTime === 'function' && typeof value.getMonth === 'function' && typeof value.getYear
334
+ === 'function'
335
+ },
336
+ number: function number(value) {
337
+ if (isNaN(value)) {
338
+ return false
339
+ }
340
+
341
+ // 修改源码,将字符串数值先转为数值
342
+ return typeof +value === 'number'
343
+ },
344
+ object: function object(value) {
345
+ return typeof value === 'object' && !types.array(value)
346
+ },
347
+ method: function method(value) {
348
+ return typeof value === 'function'
349
+ },
350
+ email: function email(value) {
351
+ return typeof value === 'string' && !!value.match(pattern.email) && value.length < 255
352
+ },
353
+ url: function url(value) {
354
+ return typeof value === 'string' && !!value.match(pattern.url)
355
+ },
356
+ hex: function hex(value) {
357
+ return typeof value === 'string' && !!value.match(pattern.hex)
358
+ }
359
+ }
360
+ /**
361
+ * Rule for validating the type of a value.
362
+ *
363
+ * @param rule The validation rule.
364
+ * @param value The value of the field on the source object.
365
+ * @param source The source object being validated.
366
+ * @param errors An array of errors that this rule may add
367
+ * validation errors to.
368
+ * @param options The validation options.
369
+ * @param options.messages The validation messages.
370
+ */
371
+
372
+ function type(rule, value, source, errors, options) {
373
+ if (rule.required && value === undefined) {
374
+ required(rule, value, source, errors, options)
375
+ return
376
+ }
377
+
378
+ const custom = ['integer', 'float', 'array', 'regexp', 'object', 'method', 'email', 'number', 'date', 'url', 'hex']
379
+ const ruleType = rule.type
380
+
381
+ if (custom.indexOf(ruleType) > -1) {
382
+ if (!types[ruleType](value)) {
383
+ errors.push(format(options.messages.types[ruleType], rule.fullField, rule.type))
384
+ } // straight typeof check
385
+ } else if (ruleType && typeof value !== rule.type) {
386
+ errors.push(format(options.messages.types[ruleType], rule.fullField, rule.type))
387
+ }
388
+ }
389
+
390
+ /**
391
+ * Rule for validating minimum and maximum allowed values.
392
+ *
393
+ * @param rule The validation rule.
394
+ * @param value The value of the field on the source object.
395
+ * @param source The source object being validated.
396
+ * @param errors An array of errors that this rule may add
397
+ * validation errors to.
398
+ * @param options The validation options.
399
+ * @param options.messages The validation messages.
400
+ */
401
+
402
+ function range(rule, value, source, errors, options) {
403
+ const len = typeof rule.len === 'number'
404
+ const min = typeof rule.min === 'number'
405
+ const max = typeof rule.max === 'number' // 正则匹配码点范围从U+010000一直到U+10FFFF的文字(补充平面Supplementary Plane)
406
+
407
+ const spRegexp = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g
408
+ let val = value
409
+ let key = null
410
+ const num = typeof value === 'number'
411
+ const str = typeof value === 'string'
412
+ const arr = Array.isArray(value)
413
+
414
+ if (num) {
415
+ key = 'number'
416
+ } else if (str) {
417
+ key = 'string'
418
+ } else if (arr) {
419
+ key = 'array'
420
+ } // if the value is not of a supported type for range validation
421
+ // the validation rule rule should use the
422
+ // type property to also test for a particular type
423
+
424
+ if (!key) {
425
+ return false
426
+ }
427
+
428
+ if (arr) {
429
+ val = value.length
430
+ }
431
+
432
+ if (str) {
433
+ // 处理码点大于U+010000的文字length属性不准确的bug,如"𠮷𠮷𠮷".lenght !== 3
434
+ val = value.replace(spRegexp, '_').length
435
+ }
436
+
437
+ if (len) {
438
+ if (val !== rule.len) {
439
+ errors.push(format(options.messages[key].len, rule.fullField, rule.len))
440
+ }
441
+ } else if (min && !max && val < rule.min) {
442
+ errors.push(format(options.messages[key].min, rule.fullField, rule.min))
443
+ } else if (max && !min && val > rule.max) {
444
+ errors.push(format(options.messages[key].max, rule.fullField, rule.max))
445
+ } else if (min && max && (val < rule.min || val > rule.max)) {
446
+ errors.push(format(options.messages[key].range, rule.fullField, rule.min, rule.max))
447
+ }
448
+ }
449
+
450
+ const ENUM = 'enum'
451
+ /**
452
+ * Rule for validating a value exists in an enumerable list.
453
+ *
454
+ * @param rule The validation rule.
455
+ * @param value The value of the field on the source object.
456
+ * @param source The source object being validated.
457
+ * @param errors An array of errors that this rule may add
458
+ * validation errors to.
459
+ * @param options The validation options.
460
+ * @param options.messages The validation messages.
461
+ */
462
+
463
+ function enumerable(rule, value, source, errors, options) {
464
+ rule[ENUM] = Array.isArray(rule[ENUM]) ? rule[ENUM] : []
465
+
466
+ if (rule[ENUM].indexOf(value) === -1) {
467
+ errors.push(format(options.messages[ENUM], rule.fullField, rule[ENUM].join(', ')))
468
+ }
469
+ }
470
+
471
+ /**
472
+ * Rule for validating a regular expression pattern.
473
+ *
474
+ * @param rule The validation rule.
475
+ * @param value The value of the field on the source object.
476
+ * @param source The source object being validated.
477
+ * @param errors An array of errors that this rule may add
478
+ * validation errors to.
479
+ * @param options The validation options.
480
+ * @param options.messages The validation messages.
481
+ */
482
+
483
+ function pattern$1(rule, value, source, errors, options) {
484
+ if (rule.pattern) {
485
+ if (rule.pattern instanceof RegExp) {
486
+ // if a RegExp instance is passed, reset `lastIndex` in case its `global`
487
+ // flag is accidentally set to `true`, which in a validation scenario
488
+ // is not necessary and the result might be misleading
489
+ rule.pattern.lastIndex = 0
490
+
491
+ if (!rule.pattern.test(value)) {
492
+ errors.push(format(options.messages.pattern.mismatch, rule.fullField, value, rule.pattern))
493
+ }
494
+ } else if (typeof rule.pattern === 'string') {
495
+ const _pattern = new RegExp(rule.pattern)
496
+
497
+ if (!_pattern.test(value)) {
498
+ errors.push(format(options.messages.pattern.mismatch, rule.fullField, value, rule.pattern))
499
+ }
500
+ }
501
+ }
502
+ }
503
+
504
+ const rules = {
505
+ required,
506
+ whitespace,
507
+ type,
508
+ range,
509
+ enum: enumerable,
510
+ pattern: pattern$1
511
+ }
512
+
513
+ /**
514
+ * Performs validation for string types.
515
+ *
516
+ * @param rule The validation rule.
517
+ * @param value The value of the field on the source object.
518
+ * @param callback The callback function.
519
+ * @param source The source object being validated.
520
+ * @param options The validation options.
521
+ * @param options.messages The validation messages.
522
+ */
523
+
524
+ function string(rule, value, callback, source, options) {
525
+ const errors = []
526
+ const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field)
527
+
528
+ if (validate) {
529
+ if (isEmptyValue(value, 'string') && !rule.required) {
530
+ return callback()
531
+ }
532
+
533
+ rules.required(rule, value, source, errors, options, 'string')
534
+
535
+ if (!isEmptyValue(value, 'string')) {
536
+ rules.type(rule, value, source, errors, options)
537
+ rules.range(rule, value, source, errors, options)
538
+ rules.pattern(rule, value, source, errors, options)
539
+
540
+ if (rule.whitespace === true) {
541
+ rules.whitespace(rule, value, source, errors, options)
542
+ }
543
+ }
544
+ }
545
+
546
+ callback(errors)
547
+ }
548
+
549
+ /**
550
+ * Validates a function.
551
+ *
552
+ * @param rule The validation rule.
553
+ * @param value The value of the field on the source object.
554
+ * @param callback The callback function.
555
+ * @param source The source object being validated.
556
+ * @param options The validation options.
557
+ * @param options.messages The validation messages.
558
+ */
559
+
560
+ function method(rule, value, callback, source, options) {
561
+ const errors = []
562
+ const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field)
563
+
564
+ if (validate) {
565
+ if (isEmptyValue(value) && !rule.required) {
566
+ return callback()
567
+ }
568
+
569
+ rules.required(rule, value, source, errors, options)
570
+
571
+ if (value !== undefined) {
572
+ rules.type(rule, value, source, errors, options)
573
+ }
574
+ }
575
+
576
+ callback(errors)
577
+ }
578
+
579
+ /**
580
+ * Validates a number.
581
+ *
582
+ * @param rule The validation rule.
583
+ * @param value The value of the field on the source object.
584
+ * @param callback The callback function.
585
+ * @param source The source object being validated.
586
+ * @param options The validation options.
587
+ * @param options.messages The validation messages.
588
+ */
589
+
590
+ function number(rule, value, callback, source, options) {
591
+ const errors = []
592
+ const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field)
593
+
594
+ if (validate) {
595
+ if (value === '') {
596
+ value = undefined
597
+ }
598
+
599
+ if (isEmptyValue(value) && !rule.required) {
600
+ return callback()
601
+ }
602
+
603
+ rules.required(rule, value, source, errors, options)
604
+
605
+ if (value !== undefined) {
606
+ rules.type(rule, value, source, errors, options)
607
+ rules.range(rule, value, source, errors, options)
608
+ }
609
+ }
610
+
611
+ callback(errors)
612
+ }
613
+
614
+ /**
615
+ * Validates a boolean.
616
+ *
617
+ * @param rule The validation rule.
618
+ * @param value The value of the field on the source object.
619
+ * @param callback The callback function.
620
+ * @param source The source object being validated.
621
+ * @param options The validation options.
622
+ * @param options.messages The validation messages.
623
+ */
624
+
625
+ function _boolean(rule, value, callback, source, options) {
626
+ const errors = []
627
+ const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field)
628
+
629
+ if (validate) {
630
+ if (isEmptyValue(value) && !rule.required) {
631
+ return callback()
632
+ }
633
+
634
+ rules.required(rule, value, source, errors, options)
635
+
636
+ if (value !== undefined) {
637
+ rules.type(rule, value, source, errors, options)
638
+ }
639
+ }
640
+
641
+ callback(errors)
642
+ }
643
+
644
+ /**
645
+ * Validates the regular expression type.
646
+ *
647
+ * @param rule The validation rule.
648
+ * @param value The value of the field on the source object.
649
+ * @param callback The callback function.
650
+ * @param source The source object being validated.
651
+ * @param options The validation options.
652
+ * @param options.messages The validation messages.
653
+ */
654
+
655
+ function regexp(rule, value, callback, source, options) {
656
+ const errors = []
657
+ const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field)
658
+
659
+ if (validate) {
660
+ if (isEmptyValue(value) && !rule.required) {
661
+ return callback()
662
+ }
663
+
664
+ rules.required(rule, value, source, errors, options)
665
+
666
+ if (!isEmptyValue(value)) {
667
+ rules.type(rule, value, source, errors, options)
668
+ }
669
+ }
670
+
671
+ callback(errors)
672
+ }
673
+
674
+ /**
675
+ * Validates a number is an integer.
676
+ *
677
+ * @param rule The validation rule.
678
+ * @param value The value of the field on the source object.
679
+ * @param callback The callback function.
680
+ * @param source The source object being validated.
681
+ * @param options The validation options.
682
+ * @param options.messages The validation messages.
683
+ */
684
+
685
+ function integer(rule, value, callback, source, options) {
686
+ const errors = []
687
+ const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field)
688
+
689
+ if (validate) {
690
+ if (isEmptyValue(value) && !rule.required) {
691
+ return callback()
692
+ }
693
+
694
+ rules.required(rule, value, source, errors, options)
695
+
696
+ if (value !== undefined) {
697
+ rules.type(rule, value, source, errors, options)
698
+ rules.range(rule, value, source, errors, options)
699
+ }
700
+ }
701
+
702
+ callback(errors)
703
+ }
704
+
705
+ /**
706
+ * Validates a number is a floating point number.
707
+ *
708
+ * @param rule The validation rule.
709
+ * @param value The value of the field on the source object.
710
+ * @param callback The callback function.
711
+ * @param source The source object being validated.
712
+ * @param options The validation options.
713
+ * @param options.messages The validation messages.
714
+ */
715
+
716
+ function floatFn(rule, value, callback, source, options) {
717
+ const errors = []
718
+ const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field)
719
+
720
+ if (validate) {
721
+ if (isEmptyValue(value) && !rule.required) {
722
+ return callback()
723
+ }
724
+
725
+ rules.required(rule, value, source, errors, options)
726
+
727
+ if (value !== undefined) {
728
+ rules.type(rule, value, source, errors, options)
729
+ rules.range(rule, value, source, errors, options)
730
+ }
731
+ }
732
+
733
+ callback(errors)
734
+ }
735
+
736
+ /**
737
+ * Validates an array.
738
+ *
739
+ * @param rule The validation rule.
740
+ * @param value The value of the field on the source object.
741
+ * @param callback The callback function.
742
+ * @param source The source object being validated.
743
+ * @param options The validation options.
744
+ * @param options.messages The validation messages.
745
+ */
746
+
747
+ function array(rule, value, callback, source, options) {
748
+ const errors = []
749
+ const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field)
750
+
751
+ if (validate) {
752
+ if (isEmptyValue(value, 'array') && !rule.required) {
753
+ return callback()
754
+ }
755
+
756
+ rules.required(rule, value, source, errors, options, 'array')
757
+
758
+ if (!isEmptyValue(value, 'array')) {
759
+ rules.type(rule, value, source, errors, options)
760
+ rules.range(rule, value, source, errors, options)
761
+ }
762
+ }
763
+
764
+ callback(errors)
765
+ }
766
+
767
+ /**
768
+ * Validates an object.
769
+ *
770
+ * @param rule The validation rule.
771
+ * @param value The value of the field on the source object.
772
+ * @param callback The callback function.
773
+ * @param source The source object being validated.
774
+ * @param options The validation options.
775
+ * @param options.messages The validation messages.
776
+ */
777
+
778
+ function object(rule, value, callback, source, options) {
779
+ const errors = []
780
+ const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field)
781
+
782
+ if (validate) {
783
+ if (isEmptyValue(value) && !rule.required) {
784
+ return callback()
785
+ }
786
+
787
+ rules.required(rule, value, source, errors, options)
788
+
789
+ if (value !== undefined) {
790
+ rules.type(rule, value, source, errors, options)
791
+ }
792
+ }
793
+
794
+ callback(errors)
795
+ }
796
+
797
+ const ENUM$1 = 'enum'
798
+ /**
799
+ * Validates an enumerable list.
800
+ *
801
+ * @param rule The validation rule.
802
+ * @param value The value of the field on the source object.
803
+ * @param callback The callback function.
804
+ * @param source The source object being validated.
805
+ * @param options The validation options.
806
+ * @param options.messages The validation messages.
807
+ */
808
+
809
+ function enumerable$1(rule, value, callback, source, options) {
810
+ const errors = []
811
+ const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field)
812
+
813
+ if (validate) {
814
+ if (isEmptyValue(value) && !rule.required) {
815
+ return callback()
816
+ }
817
+
818
+ rules.required(rule, value, source, errors, options)
819
+
820
+ if (value !== undefined) {
821
+ rules[ENUM$1](rule, value, source, errors, options)
822
+ }
823
+ }
824
+
825
+ callback(errors)
826
+ }
827
+
828
+ /**
829
+ * Validates a regular expression pattern.
830
+ *
831
+ * Performs validation when a rule only contains
832
+ * a pattern property but is not declared as a string type.
833
+ *
834
+ * @param rule The validation rule.
835
+ * @param value The value of the field on the source object.
836
+ * @param callback The callback function.
837
+ * @param source The source object being validated.
838
+ * @param options The validation options.
839
+ * @param options.messages The validation messages.
840
+ */
841
+
842
+ function pattern$2(rule, value, callback, source, options) {
843
+ const errors = []
844
+ const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field)
845
+
846
+ if (validate) {
847
+ if (isEmptyValue(value, 'string') && !rule.required) {
848
+ return callback()
849
+ }
850
+
851
+ rules.required(rule, value, source, errors, options)
852
+
853
+ if (!isEmptyValue(value, 'string')) {
854
+ rules.pattern(rule, value, source, errors, options)
855
+ }
856
+ }
857
+
858
+ callback(errors)
859
+ }
860
+
861
+ function date(rule, value, callback, source, options) {
862
+ const errors = []
863
+ const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field)
864
+
865
+ if (validate) {
866
+ if (isEmptyValue(value) && !rule.required) {
867
+ return callback()
868
+ }
869
+
870
+ rules.required(rule, value, source, errors, options)
871
+
872
+ if (!isEmptyValue(value)) {
873
+ let dateObject
874
+
875
+ if (typeof value === 'number') {
876
+ dateObject = new Date(value)
877
+ } else {
878
+ dateObject = value
879
+ }
880
+
881
+ rules.type(rule, dateObject, source, errors, options)
882
+
883
+ if (dateObject) {
884
+ rules.range(rule, dateObject.getTime(), source, errors, options)
885
+ }
886
+ }
887
+ }
888
+
889
+ callback(errors)
890
+ }
891
+
892
+ function required$1(rule, value, callback, source, options) {
893
+ const errors = []
894
+ const type = Array.isArray(value) ? 'array' : typeof value
895
+ rules.required(rule, value, source, errors, options, type)
896
+ callback(errors)
897
+ }
898
+
899
+ function type$1(rule, value, callback, source, options) {
900
+ const ruleType = rule.type
901
+ const errors = []
902
+ const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field)
903
+
904
+ if (validate) {
905
+ if (isEmptyValue(value, ruleType) && !rule.required) {
906
+ return callback()
907
+ }
908
+
909
+ rules.required(rule, value, source, errors, options, ruleType)
910
+
911
+ if (!isEmptyValue(value, ruleType)) {
912
+ rules.type(rule, value, source, errors, options)
913
+ }
914
+ }
915
+
916
+ callback(errors)
917
+ }
918
+
919
+ /**
920
+ * Performs validation for any type.
921
+ *
922
+ * @param rule The validation rule.
923
+ * @param value The value of the field on the source object.
924
+ * @param callback The callback function.
925
+ * @param source The source object being validated.
926
+ * @param options The validation options.
927
+ * @param options.messages The validation messages.
928
+ */
929
+
930
+ function any(rule, value, callback, source, options) {
931
+ const errors = []
932
+ const validate = rule.required || !rule.required && source.hasOwnProperty(rule.field)
933
+
934
+ if (validate) {
935
+ if (isEmptyValue(value) && !rule.required) {
936
+ return callback()
937
+ }
938
+
939
+ rules.required(rule, value, source, errors, options)
940
+ }
941
+
942
+ callback(errors)
943
+ }
944
+
945
+ const validators = {
946
+ string,
947
+ method,
948
+ number,
949
+ boolean: _boolean,
950
+ regexp,
951
+ integer,
952
+ float: floatFn,
953
+ array,
954
+ object,
955
+ enum: enumerable$1,
956
+ pattern: pattern$2,
957
+ date,
958
+ url: type$1,
959
+ hex: type$1,
960
+ email: type$1,
961
+ required: required$1,
962
+ any
963
+ }
964
+
965
+ function newMessages() {
966
+ return {
967
+ default: 'Validation error on field %s',
968
+ required: '%s is required',
969
+ enum: '%s must be one of %s',
970
+ whitespace: '%s cannot be empty',
971
+ date: {
972
+ format: '%s date %s is invalid for format %s',
973
+ parse: '%s date could not be parsed, %s is invalid ',
974
+ invalid: '%s date %s is invalid'
975
+ },
976
+ types: {
977
+ string: '%s is not a %s',
978
+ method: '%s is not a %s (function)',
979
+ array: '%s is not an %s',
980
+ object: '%s is not an %s',
981
+ number: '%s is not a %s',
982
+ date: '%s is not a %s',
983
+ boolean: '%s is not a %s',
984
+ integer: '%s is not an %s',
985
+ float: '%s is not a %s',
986
+ regexp: '%s is not a valid %s',
987
+ email: '%s is not a valid %s',
988
+ url: '%s is not a valid %s',
989
+ hex: '%s is not a valid %s'
990
+ },
991
+ string: {
992
+ len: '%s must be exactly %s characters',
993
+ min: '%s must be at least %s characters',
994
+ max: '%s cannot be longer than %s characters',
995
+ range: '%s must be between %s and %s characters'
996
+ },
997
+ number: {
998
+ len: '%s must equal %s',
999
+ min: '%s cannot be less than %s',
1000
+ max: '%s cannot be greater than %s',
1001
+ range: '%s must be between %s and %s'
1002
+ },
1003
+ array: {
1004
+ len: '%s must be exactly %s in length',
1005
+ min: '%s cannot be less than %s in length',
1006
+ max: '%s cannot be greater than %s in length',
1007
+ range: '%s must be between %s and %s in length'
1008
+ },
1009
+ pattern: {
1010
+ mismatch: '%s value %s does not match pattern %s'
1011
+ },
1012
+ clone: function clone() {
1013
+ const cloned = JSON.parse(JSON.stringify(this))
1014
+ cloned.clone = this.clone
1015
+ return cloned
1016
+ }
1017
+ }
1018
+ }
1019
+ const messages = newMessages()
1020
+
1021
+ /**
1022
+ * Encapsulates a validation schema.
1023
+ *
1024
+ * @param descriptor An object declaring validation rules
1025
+ * for this schema.
1026
+ */
1027
+
1028
+ function Schema(descriptor) {
1029
+ this.rules = null
1030
+ this._messages = messages
1031
+ this.define(descriptor)
1032
+ }
1033
+
1034
+ Schema.prototype = {
1035
+ messages: function messages(_messages) {
1036
+ if (_messages) {
1037
+ this._messages = deepMerge(newMessages(), _messages)
1038
+ }
1039
+
1040
+ return this._messages
1041
+ },
1042
+ define: function define(rules) {
1043
+ if (!rules) {
1044
+ throw new Error('Cannot configure a schema with no rules')
1045
+ }
1046
+
1047
+ if (typeof rules !== 'object' || Array.isArray(rules)) {
1048
+ throw new Error('Rules must be an object')
1049
+ }
1050
+
1051
+ this.rules = {}
1052
+ let z
1053
+ let item
1054
+
1055
+ for (z in rules) {
1056
+ if (rules.hasOwnProperty(z)) {
1057
+ item = rules[z]
1058
+ this.rules[z] = Array.isArray(item) ? item : [item]
1059
+ }
1060
+ }
1061
+ },
1062
+ validate: function validate(source_, o, oc) {
1063
+ const _this = this
1064
+
1065
+ if (o === void 0) {
1066
+ o = {}
1067
+ }
1068
+
1069
+ if (oc === void 0) {
1070
+ oc = function oc() {}
1071
+ }
1072
+
1073
+ let source = source_
1074
+ let options = o
1075
+ let callback = oc
1076
+
1077
+ if (typeof options === 'function') {
1078
+ callback = options
1079
+ options = {}
1080
+ }
1081
+
1082
+ if (!this.rules || Object.keys(this.rules).length === 0) {
1083
+ if (callback) {
1084
+ callback()
1085
+ }
1086
+
1087
+ return Promise.resolve()
1088
+ }
1089
+
1090
+ function complete(results) {
1091
+ let i
1092
+ let errors = []
1093
+ let fields = {}
1094
+
1095
+ function add(e) {
1096
+ if (Array.isArray(e)) {
1097
+ let _errors
1098
+
1099
+ errors = (_errors = errors).concat.apply(_errors, e)
1100
+ } else {
1101
+ errors.push(e)
1102
+ }
1103
+ }
1104
+
1105
+ for (i = 0; i < results.length; i++) {
1106
+ add(results[i])
1107
+ }
1108
+
1109
+ if (!errors.length) {
1110
+ errors = null
1111
+ fields = null
1112
+ } else {
1113
+ fields = convertFieldsError(errors)
1114
+ }
1115
+
1116
+ callback(errors, fields)
1117
+ }
1118
+
1119
+ if (options.messages) {
1120
+ let messages$1 = this.messages()
1121
+
1122
+ if (messages$1 === messages) {
1123
+ messages$1 = newMessages()
1124
+ }
1125
+
1126
+ deepMerge(messages$1, options.messages)
1127
+ options.messages = messages$1
1128
+ } else {
1129
+ options.messages = this.messages()
1130
+ }
1131
+
1132
+ let arr
1133
+ let value
1134
+ const series = {}
1135
+ const keys = options.keys || Object.keys(this.rules)
1136
+ keys.forEach((z) => {
1137
+ arr = _this.rules[z]
1138
+ value = source[z]
1139
+ arr.forEach((r) => {
1140
+ let rule = r
1141
+
1142
+ if (typeof rule.transform === 'function') {
1143
+ if (source === source_) {
1144
+ source = { ...source }
1145
+ }
1146
+
1147
+ value = source[z] = rule.transform(value)
1148
+ }
1149
+
1150
+ if (typeof rule === 'function') {
1151
+ rule = {
1152
+ validator: rule
1153
+ }
1154
+ } else {
1155
+ rule = { ...rule }
1156
+ }
1157
+
1158
+ rule.validator = _this.getValidationMethod(rule)
1159
+ rule.field = z
1160
+ rule.fullField = rule.fullField || z
1161
+ rule.type = _this.getType(rule)
1162
+
1163
+ if (!rule.validator) {
1164
+ return
1165
+ }
1166
+
1167
+ series[z] = series[z] || []
1168
+ series[z].push({
1169
+ rule,
1170
+ value,
1171
+ source,
1172
+ field: z
1173
+ })
1174
+ })
1175
+ })
1176
+ const errorFields = {}
1177
+ return asyncMap(series, options, (data, doIt) => {
1178
+ const { rule } = data
1179
+ let deep = (rule.type === 'object' || rule.type === 'array') && (typeof rule.fields === 'object' || typeof rule.defaultField
1180
+ === 'object')
1181
+ deep = deep && (rule.required || !rule.required && data.value)
1182
+ rule.field = data.field
1183
+
1184
+ function addFullfield(key, schema) {
1185
+ return { ...schema, fullField: `${rule.fullField}.${key}` }
1186
+ }
1187
+
1188
+ function cb(e) {
1189
+ if (e === void 0) {
1190
+ e = []
1191
+ }
1192
+
1193
+ let errors = e
1194
+
1195
+ if (!Array.isArray(errors)) {
1196
+ errors = [errors]
1197
+ }
1198
+
1199
+ if (!options.suppressWarning && errors.length) {
1200
+ Schema.warning('async-validator:', errors)
1201
+ }
1202
+
1203
+ if (errors.length && rule.message) {
1204
+ errors = [].concat(rule.message)
1205
+ }
1206
+
1207
+ errors = errors.map(complementError(rule))
1208
+
1209
+ if (options.first && errors.length) {
1210
+ errorFields[rule.field] = 1
1211
+ return doIt(errors)
1212
+ }
1213
+
1214
+ if (!deep) {
1215
+ doIt(errors)
1216
+ } else {
1217
+ // if rule is required but the target object
1218
+ // does not exist fail at the rule level and don't
1219
+ // go deeper
1220
+ if (rule.required && !data.value) {
1221
+ if (rule.message) {
1222
+ errors = [].concat(rule.message).map(complementError(rule))
1223
+ } else if (options.error) {
1224
+ errors = [options.error(rule, format(options.messages.required, rule.field))]
1225
+ } else {
1226
+ errors = []
1227
+ }
1228
+
1229
+ return doIt(errors)
1230
+ }
1231
+
1232
+ let fieldsSchema = {}
1233
+
1234
+ if (rule.defaultField) {
1235
+ for (const k in data.value) {
1236
+ if (data.value.hasOwnProperty(k)) {
1237
+ fieldsSchema[k] = rule.defaultField
1238
+ }
1239
+ }
1240
+ }
1241
+
1242
+ fieldsSchema = { ...fieldsSchema, ...data.rule.fields }
1243
+
1244
+ for (const f in fieldsSchema) {
1245
+ if (fieldsSchema.hasOwnProperty(f)) {
1246
+ const fieldSchema = Array.isArray(fieldsSchema[f]) ? fieldsSchema[f] : [fieldsSchema[f]]
1247
+ fieldsSchema[f] = fieldSchema.map(addFullfield.bind(null, f))
1248
+ }
1249
+ }
1250
+
1251
+ const schema = new Schema(fieldsSchema)
1252
+ schema.messages(options.messages)
1253
+
1254
+ if (data.rule.options) {
1255
+ data.rule.options.messages = options.messages
1256
+ data.rule.options.error = options.error
1257
+ }
1258
+
1259
+ schema.validate(data.value, data.rule.options || options, (errs) => {
1260
+ const finalErrors = []
1261
+
1262
+ if (errors && errors.length) {
1263
+ finalErrors.push.apply(finalErrors, errors)
1264
+ }
1265
+
1266
+ if (errs && errs.length) {
1267
+ finalErrors.push.apply(finalErrors, errs)
1268
+ }
1269
+
1270
+ doIt(finalErrors.length ? finalErrors : null)
1271
+ })
1272
+ }
1273
+ }
1274
+
1275
+ let res
1276
+
1277
+ if (rule.asyncValidator) {
1278
+ res = rule.asyncValidator(rule, data.value, cb, data.source, options)
1279
+ } else if (rule.validator) {
1280
+ res = rule.validator(rule, data.value, cb, data.source, options)
1281
+
1282
+ if (res === true) {
1283
+ cb()
1284
+ } else if (res === false) {
1285
+ cb(rule.message || `${rule.field} fails`)
1286
+ } else if (res instanceof Array) {
1287
+ cb(res)
1288
+ } else if (res instanceof Error) {
1289
+ cb(res.message)
1290
+ }
1291
+ }
1292
+
1293
+ if (res && res.then) {
1294
+ res.then(() => cb(), (e) => cb(e))
1295
+ }
1296
+ }, (results) => {
1297
+ complete(results)
1298
+ })
1299
+ },
1300
+ getType: function getType(rule) {
1301
+ if (rule.type === undefined && rule.pattern instanceof RegExp) {
1302
+ rule.type = 'pattern'
1303
+ }
1304
+
1305
+ if (typeof rule.validator !== 'function' && rule.type && !validators.hasOwnProperty(rule.type)) {
1306
+ throw new Error(format('Unknown rule type %s', rule.type))
1307
+ }
1308
+
1309
+ return rule.type || 'string'
1310
+ },
1311
+ getValidationMethod: function getValidationMethod(rule) {
1312
+ if (typeof rule.validator === 'function') {
1313
+ return rule.validator
1314
+ }
1315
+
1316
+ const keys = Object.keys(rule)
1317
+ const messageIndex = keys.indexOf('message')
1318
+
1319
+ if (messageIndex !== -1) {
1320
+ keys.splice(messageIndex, 1)
1321
+ }
1322
+
1323
+ if (keys.length === 1 && keys[0] === 'required') {
1324
+ return validators.required
1325
+ }
1326
+
1327
+ return validators[this.getType(rule)] || false
1328
+ }
1329
+ }
1330
+
1331
+ Schema.register = function register(type, validator) {
1332
+ if (typeof validator !== 'function') {
1333
+ throw new Error('Cannot register a validator by type, validator is not a function')
1334
+ }
1335
+
1336
+ validators[type] = validator
1337
+ }
1338
+
1339
+ Schema.warning = warning
1340
+ Schema.messages = messages
1341
+
1342
+ export default Schema
1343
+ // # sourceMappingURL=index.js.map