sadais-piui 1.2.7-beta113 → 1.2.7-beta115

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