2 * @fileoverview Rule Validator
3 * @author Nicholas C. Zakas
8 //-----------------------------------------------------------------------------
10 //-----------------------------------------------------------------------------
12 const ajv = require("../shared/ajv")();
14 //-----------------------------------------------------------------------------
16 //-----------------------------------------------------------------------------
19 * Finds a rule with the given ID in the given config.
20 * @param {string} ruleId The ID of the rule to find.
21 * @param {Object} config The config to search in.
22 * @returns {{create: Function, schema: (Array|null)}} THe rule object.
24 function findRuleDefinition(ruleId, config) {
25 const ruleIdParts = ruleId.split("/");
26 let pluginName, ruleName;
29 if (ruleIdParts.length === 1) {
31 ruleName = ruleIdParts[0];
33 ruleName = ruleIdParts.pop();
34 pluginName = ruleIdParts.join("/");
37 if (!config.plugins || !config.plugins[pluginName]) {
38 throw new TypeError(`Key "rules": Key "${ruleId}": Could not find plugin "${pluginName}".`);
41 if (!config.plugins[pluginName].rules || !config.plugins[pluginName].rules[ruleName]) {
42 throw new TypeError(`Key "rules": Key "${ruleId}": Could not find "${ruleName}" in plugin "${pluginName}".`);
45 return config.plugins[pluginName].rules[ruleName];
50 * Gets a complete options schema for a rule.
51 * @param {{create: Function, schema: (Array|null)}} rule A new-style rule object
52 * @returns {Object} JSON Schema for the rule's options.
54 function getRuleOptionsSchema(rule) {
60 const schema = rule.schema || rule.meta && rule.meta.schema;
62 if (Array.isArray(schema)) {
68 maxItems: schema.length
79 // Given a full schema, leave it alone
80 return schema || null;
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
88 * Implements validation functionality for the rules portion of a config.
93 * Creates a new instance.
98 * A collection of compiled validators for rules that have already
101 * @property validators
103 this.validators = new WeakMap();
107 * Validates all of the rule configurations in a config against each
109 * @param {Object} config The full config to validate. This object must
110 * contain both the rules section and the plugins section.
112 * @throws {Error} If a rule's configuration does not match its schema.
120 for (const [ruleId, ruleOptions] of Object.entries(config.rules)) {
122 // check for edge case
123 if (ruleId === "__proto__") {
128 * If a rule is disabled, we don't do any validation. This allows
129 * users to safely set any value to 0 or "off" without worrying
130 * that it will cause a validation error.
132 * Note: ruleOptions is always an array at this point because
133 * this validation occurs after FlatConfigArray has merged and
136 if (ruleOptions[0] === 0) {
140 const rule = findRuleDefinition(ruleId, config);
142 // Precompile and cache validator the first time
143 if (!this.validators.has(rule)) {
144 const schema = getRuleOptionsSchema(rule);
147 this.validators.set(rule, ajv.compile(schema));
151 const validateRule = this.validators.get(rule);
155 validateRule(ruleOptions.slice(1));
157 if (validateRule.errors) {
158 throw new Error(`Key "rules": Key "${ruleId}": ${
159 validateRule.errors.map(
160 error => `\tValue ${JSON.stringify(error.data)} ${error.message}.\n`
169 exports.RuleValidator = RuleValidator;