uview-pro 0.0.18 → 0.0.19

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