/
ConfigValidator.js
executable file
·146 lines (128 loc) · 4.15 KB
/
ConfigValidator.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const ConfigSchema = require('./ConfigSchema');
/**
* Validates object rule config
*
* @param {Object} ruleConfig Object rule
* @return {Boolean} True if config is valid, false if not
* @static
*/
const isObjectRuleConfigValid = ruleConfig => {
if (typeof ruleConfig === 'string' && ruleConfig === 'off') {
return true;
}
if (typeof ruleConfig === 'string' && ruleConfig !== 'off') {
throw new Error('\t- is an object type rule. It must be set to "off" if an object is not supplied.');
}
return ConfigSchema.isObjectRuleSchemaValid(ruleConfig);
};
/**
* Validates optional object rule config
*
* @param {Object} ruleConfig Object rule
* @return {Boolean} True if config is valid, false if not
* @static
*/
const isOptionalObjRuleConfigValid = ruleConfig => {
const object = 1;
if (typeof ruleConfig === 'string') {
return ConfigSchema.isStandardRuleSchemaValid(ruleConfig);
}
if (ConfigSchema.isObjectRuleSchemaValid(ruleConfig)) {
if (ruleConfig[object].hasOwnProperty('exceptions')) {
return ConfigSchema.isOptionalObjExceptSchemaValid(ruleConfig[object].exceptions);
}
}
return true;
};
/**
* Validates array rule config
*
* @param {Array} ruleConfig Array rule
* @return {Boolean} True if config is valid, false if not
* @static
*/
const isArrayRuleConfigValid = ruleConfig => {
if (typeof ruleConfig === 'string' && ruleConfig === 'off') {
return true;
}
if (typeof ruleConfig === 'string' && ruleConfig !== 'off') {
throw new Error('\t- is an array type rule. It must be set to "off" if an array is not supplied.');
}
return ConfigSchema.isArrayRuleSchemaValid(ruleConfig);
};
/**
* Validates standard rule config
*
* @param {Object} ruleConfig Value for standard rule config
* @return {Boolean} True if config is valid, error if not
* @static
*/
const isStandardRuleConfigValid = ruleConfig => {
return ConfigSchema.isStandardRuleSchemaValid(ruleConfig);
};
/**
* Validates configuration of a rule
*
* @param {Object} ruleModule The rule object.
* @param {string} ruleName The rule's unique name.
* @param {Array|String} userConfig The user's configuration for a rule.
* @param {String|null} source The name of the configuration source to report in any errors.
* @returns {undefined} No return
*/
const validateRule = (ruleModule, ruleName, userConfig, source) => {
if (ruleModule) {
try {
if (ruleModule.ruleType === 'array') {
isArrayRuleConfigValid(userConfig);
} else if (ruleModule.ruleType === 'object') {
isObjectRuleConfigValid(userConfig);
} else if (ruleModule.ruleType === 'optionalObject') {
isOptionalObjRuleConfigValid(userConfig);
} else {
isStandardRuleConfigValid(userConfig);
}
} catch (err) {
const modifiedErrorMessage = `Configuration for rule "${ruleName}" is invalid:\n${err.message}`;
if (typeof source === 'string') {
throw new Error(`${source}:\n\t${modifiedErrorMessage}`);
} else {
throw new Error(modifiedErrorMessage);
}
}
}
};
/**
* Validates only the rules of a config object
*
* @param {Object} rulesConfig The rules config object to validate.
* @param {String} source The name of the configuration source to report in any errors.
* @param {Object} rules Rules object
* @returns {undefined} No return
* @static
*/
const validateRules = (rulesConfig, source, rules) => {
if (!rulesConfig) {
return;
}
Object.keys(rulesConfig).forEach(ruleName => {
const ruleModule = rules.get(ruleName);
validateRule(ruleModule, ruleName, rulesConfig[ruleName], source);
});
};
/**
* Validates entire config object, including top-level properties.
*
* @param {Object} config The config object to validate.
* @param {String} source The name of the configuration source to report in any errors.
* @param {Object} rules Rules object
* @returns {undefined} No return
* @static
*/
const validate = (config, source, rules) => {
ConfigSchema.isConfigObjectSchemaValid(config, source);
validateRules(config.rules, source, rules);
};
module.exports = {
validate,
validateRules
};