-
Notifications
You must be signed in to change notification settings - Fork 118
/
example.js
88 lines (83 loc) · 1.65 KB
/
example.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var Parameter = require('./');
var rule = {
name: 'string',
age: {type: 'int', max: 200, message: { max: '年龄不能大于 200 岁' }},
gender: ['male', 'female'],
working: 'boolean',
salary: {type: 'number', min: 0},
birthday: 'date',
now: 'dateTime',
id: 'id',
childrens: {
type: 'array',
itemType: 'object',
required: false,
rule: {
name: 'string',
age: { type: 'int', message: { required: '子女年龄不能为空' }},
gender: ['male', 'female'],
birthday: {type: 'date', required: false}
}
},
mate: {
type: 'object',
required: false,
rule: {
name: 'string',
age: 'int',
gender: ['male', 'female'],
birthday: {type: 'date', required: false}
}
}
};
var valid = {
name: 'foo',
gender: 'male',
age: 30,
working: true,
salary: 10000.1,
birthday: '1990-01-01',
now: '2015-01-07 00:00:00',
id: '052111',
childrens: [{
name: 'bar1',
age: 1,
gender: 'female',
birthday: '2014-01-01'
}, {
name: 'bar2',
age: 2,
gender: 'male',
birthday: '2013-01-01'
}],
mate: {
name: 'hee',
age: 29,
gender: 'female'
}
};
var invalid = {
name: 1,
gender: 'male1',
age: 300,
working: 'true',
salary: '10000.1',
birthday: '1990-01-011',
id: '052111x',
childrens: [{
name: 'bar1',
gender: 'female',
birthday: '2014-01-01'
}, {
name: 'bar2',
age: 2,
birthday: '2013-01-01'
}],
mate: {
age: 29,
gender: 'female'
}
};
var p = new Parameter();
console.log('valid object result: ', p.validate(rule, valid));
console.log('invalid object result: ', p.validate(rule, invalid));