-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.js
61 lines (49 loc) · 1.51 KB
/
number.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import Schema from '../dist/index.cjs'
// 普通类型
const desc = {
min: { required: true, type: 'number', min: 1 },
max: { required: true, type: 'number', max: 8 },
range: { required: true, type: 'number', min: 1, max: 8 },
length: { required: true, type: 'number', len: 8 },
}
const source = {
min: 6,
max: 8,
range: 8,
length: 6,
}
const validator = new Schema(desc)
validator.validate(source, (error) => {
console.log('number:', error)
})
// 拓展类型
const moneyDesc = {
money_min: { required: true, type: 'number', extend: 'money', min: 666666 },
money_max: { required: true, type: 'number', extend: 'money', max: 666666 },
money_range: { required: true, type: 'number', extend: 'money', min: 666666, max: 888888 },
}
// 乘于100是因为money字段解决精度问题
const moneySource = {
money_min: 66666600,
money_max: 66666600,
money_range: 99999999,
}
const moneyValidator = new Schema(moneyDesc)
moneyValidator.validate(moneySource, (error) => {
console.log('money:', error)
})
const ratioDesc = {
ratio_min: { required: true, type: 'number', extend: 'ratio', min: 66 },
ratio_max: { required: true, type: 'number', extend: 'ratio', max: 88 },
ratio_range: { required: true, type: 'number', extend: 'ratio', min: 66, max: 88 },
}
// 除于100是因为作为比例问题
const ratioSource = {
ratio_min: 0.88,
ratio_max: 0.88,
ratio_range: 1,
}
const ratioValidator = new Schema(ratioDesc)
ratioValidator.validate(ratioSource, (error) => {
console.log('ratio:', error)
})