check-report.js
2.45 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const schemeDatas = require('../schemes');
const _ = require('lodash');
const assertType = require('./assert-type');
const equalsValidate = scheme => {
return log => {
const equals = scheme.equals;
const conditions = Object.keys(equals);
return _.every(conditions, key => {
const value = _.get(log.param, key);
if (equals[key] instanceof RegExp) {
return value && equals[key].test(value);
}
return value && value === equals[key]
});
};
}
const typesValidate = scheme => {
return log => {
const types = scheme.types;
const conditions = Object.keys(types);
return _.every(conditions, key => {
const value = _.get(log.param, key);
return value && assertType(value, types[key])
});
};
}
const validate = scheme => {
return log => {
return scheme.validate(log);
};
}
const convertSchemes = (schemes) => {
_.each(schemes, scheme => {
scheme._validates = [];
if (scheme.equals) {
if (!scheme.priority) {
scheme.priority = 1;
}
scheme._validates.push(equalsValidate(scheme));
}
if (scheme.types) {
if (!scheme.priority) {
scheme.priority = 1;
}
scheme._validates.push(typesValidate(scheme));
}
if (scheme.validate) {
if (!scheme.priority) {
scheme.priority = 1;
}
scheme._validates.push(validate(scheme));
}
if (scheme.single) {
if (!scheme.priority) {
scheme.priority = 1;
}
}
if (!scheme.priority) {
scheme.priority = 0;
}
})
return _.orderBy(schemes, 'priority', 'desc');
}
const matchIndex = (scheme, logs) => {
return _.findIndex(logs, log =>
log.op === scheme.op && _.every(scheme._validates, vali => vali(log)));
}
const validateSchemes = (schemes, logs) => {
const notValidates = _.filter(schemes, scheme => {
const index = matchIndex(scheme, logs);
if (index >= 0) {
logs.splice(index, 1);
if (scheme.single) {
const repeatIndex = matchIndex(scheme, logs);
if (repeatIndex >= 0) {
return true;
}
}
return false;
}
return true;
});
const overLogs = logs;
return {
notValidates,
overLogs
}
}
module.exports = function checkScene(scene, sceneData = {}) {
if (!schemeDatas[scene]) {
throw `未找到${scene}对应的scheme`;
}
const matchScheme = convertSchemes(schemeDatas[scene])
return validateSchemes(matchScheme, sceneData[scene])
}