@zfqh/uview-x 0.0.0-stage → 0.0.2

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