2 * @fileoverview Validates configs.
3 * @author Brandon Mills
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
13 util = require("util"),
14 configSchema = require("../../conf/config-schema"),
15 BuiltInEnvironments = require("../../conf/environments"),
16 ConfigOps = require("./config-ops"),
17 { emitDeprecationWarning } = require("./deprecation-warnings");
19 const ajv = require("./ajv")();
20 const ruleValidators = new WeakMap();
21 const noop = Function.prototype;
23 //------------------------------------------------------------------------------
25 //------------------------------------------------------------------------------
33 const validated = new WeakSet();
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 module.exports = class ConfigValidator {
40 constructor({ builtInRules = new Map() } = {}) {
41 this.builtInRules = builtInRules;
45 * Gets a complete options schema for a rule.
46 * @param {{create: Function, schema: (Array|null)}} rule A new-style rule object
47 * @returns {Object} JSON Schema for the rule's options.
49 getRuleOptionsSchema(rule) {
54 const schema = rule.schema || rule.meta && rule.meta.schema;
56 // Given a tuple of schemas, insert warning level at the beginning
57 if (Array.isArray(schema)) {
63 maxItems: schema.length
74 // Given a full schema, leave it alone
75 return schema || null;
79 * Validates a rule's severity and returns the severity value. Throws an error if the severity is invalid.
80 * @param {options} options The given options for the rule.
81 * @returns {number|string} The rule's severity value
83 validateRuleSeverity(options) {
84 const severity = Array.isArray(options) ? options[0] : options;
85 const normSeverity = typeof severity === "string" ? severityMap[severity.toLowerCase()] : severity;
87 if (normSeverity === 0 || normSeverity === 1 || normSeverity === 2) {
91 throw new Error(`\tSeverity should be one of the following: 0 = off, 1 = warn, 2 = error (you passed '${util.inspect(severity).replace(/'/gu, "\"").replace(/\n/gu, "")}').\n`);
96 * Validates the non-severity options passed to a rule, based on its schema.
97 * @param {{create: Function}} rule The rule to validate
98 * @param {Array} localOptions The options for the rule, excluding severity
101 validateRuleSchema(rule, localOptions) {
102 if (!ruleValidators.has(rule)) {
103 const schema = this.getRuleOptionsSchema(rule);
106 ruleValidators.set(rule, ajv.compile(schema));
110 const validateRule = ruleValidators.get(rule);
113 validateRule(localOptions);
114 if (validateRule.errors) {
115 throw new Error(validateRule.errors.map(
116 error => `\tValue ${JSON.stringify(error.data)} ${error.message}.\n`
123 * Validates a rule's options against its schema.
124 * @param {{create: Function}|null} rule The rule that the config is being validated for
125 * @param {string} ruleId The rule's unique name.
126 * @param {Array|number} options The given options for the rule.
127 * @param {string|null} source The name of the configuration source to report in any errors. If null or undefined,
128 * no source is prepended to the message.
131 validateRuleOptions(rule, ruleId, options, source = null) {
133 const severity = this.validateRuleSeverity(options);
135 if (severity !== 0) {
136 this.validateRuleSchema(rule, Array.isArray(options) ? options.slice(1) : []);
139 const enhancedMessage = `Configuration for rule "${ruleId}" is invalid:\n${err.message}`;
141 if (typeof source === "string") {
142 throw new Error(`${source}:\n\t${enhancedMessage}`);
144 throw new Error(enhancedMessage);
150 * Validates an environment object
151 * @param {Object} environment The environment config object to validate.
152 * @param {string} source The name of the configuration source to report in any errors.
153 * @param {function(envId:string): Object} [getAdditionalEnv] A map from strings to loaded environments.
159 getAdditionalEnv = noop
162 // not having an environment is ok
167 Object.keys(environment).forEach(id => {
168 const env = getAdditionalEnv(id) || BuiltInEnvironments.get(id) || null;
171 const message = `${source}:\n\tEnvironment key "${id}" is unknown\n`;
173 throw new Error(message);
179 * Validates a rules config object
180 * @param {Object} rulesConfig The rules config object to validate.
181 * @param {string} source The name of the configuration source to report in any errors.
182 * @param {function(ruleId:string): Object} getAdditionalRule A map from strings to loaded rules
188 getAdditionalRule = noop
194 Object.keys(rulesConfig).forEach(id => {
195 const rule = getAdditionalRule(id) || this.builtInRules.get(id) || null;
197 this.validateRuleOptions(rule, id, rulesConfig[id], source);
202 * Validates a `globals` section of a config file
203 * @param {Object} globalsConfig The `globals` section
204 * @param {string|null} source The name of the configuration source to report in the event of an error.
207 validateGlobals(globalsConfig, source = null) {
208 if (!globalsConfig) {
212 Object.entries(globalsConfig)
213 .forEach(([configuredGlobal, configuredValue]) => {
215 ConfigOps.normalizeConfigGlobal(configuredValue);
217 throw new Error(`ESLint configuration of global '${configuredGlobal}' in ${source} is invalid:\n${err.message}`);
223 * Validate `processor` configuration.
224 * @param {string|undefined} processorName The processor name.
225 * @param {string} source The name of config file.
226 * @param {function(id:string): Processor} getProcessor The getter of defined processors.
229 validateProcessor(processorName, source, getProcessor) {
230 if (processorName && !getProcessor(processorName)) {
231 throw new Error(`ESLint configuration of processor in '${source}' is invalid: '${processorName}' was not found.`);
236 * Formats an array of schema validation errors.
237 * @param {Array} errors An array of error messages to format.
238 * @returns {string} Formatted error message
240 formatErrors(errors) {
241 return errors.map(error => {
242 if (error.keyword === "additionalProperties") {
243 const formattedPropertyPath = error.dataPath.length ? `${error.dataPath.slice(1)}.${error.params.additionalProperty}` : error.params.additionalProperty;
245 return `Unexpected top-level property "${formattedPropertyPath}"`;
247 if (error.keyword === "type") {
248 const formattedField = error.dataPath.slice(1);
249 const formattedExpectedType = Array.isArray(error.schema) ? error.schema.join("/") : error.schema;
250 const formattedValue = JSON.stringify(error.data);
252 return `Property "${formattedField}" is the wrong type (expected ${formattedExpectedType} but got \`${formattedValue}\`)`;
255 const field = error.dataPath[0] === "." ? error.dataPath.slice(1) : error.dataPath;
257 return `"${field}" ${error.message}. Value: ${JSON.stringify(error.data)}`;
258 }).map(message => `\t- ${message}.\n`).join("");
262 * Validates the top level properties of the config object.
263 * @param {Object} config The config object to validate.
264 * @param {string} source The name of the configuration source to report in any errors.
267 validateConfigSchema(config, source = null) {
268 validateSchema = validateSchema || ajv.compile(configSchema);
270 if (!validateSchema(config)) {
271 throw new Error(`ESLint configuration in ${source} is invalid:\n${this.formatErrors(validateSchema.errors)}`);
274 if (Object.hasOwnProperty.call(config, "ecmaFeatures")) {
275 emitDeprecationWarning(source, "ESLINT_LEGACY_ECMAFEATURES");
280 * Validates an entire config object.
281 * @param {Object} config The config object to validate.
282 * @param {string} source The name of the configuration source to report in any errors.
283 * @param {function(ruleId:string): Object} [getAdditionalRule] A map from strings to loaded rules.
284 * @param {function(envId:string): Object} [getAdditionalEnv] A map from strings to loaded envs.
287 validate(config, source, getAdditionalRule, getAdditionalEnv) {
288 this.validateConfigSchema(config, source);
289 this.validateRules(config.rules, source, getAdditionalRule);
290 this.validateEnvironment(config.env, source, getAdditionalEnv);
291 this.validateGlobals(config.globals, source);
293 for (const override of config.overrides || []) {
294 this.validateRules(override.rules, source, getAdditionalRule);
295 this.validateEnvironment(override.env, source, getAdditionalEnv);
296 this.validateGlobals(config.globals, source);
301 * Validate config array object.
302 * @param {ConfigArray} configArray The config array to validate.
305 validateConfigArray(configArray) {
306 const getPluginEnv = Map.prototype.get.bind(configArray.pluginEnvironments);
307 const getPluginProcessor = Map.prototype.get.bind(configArray.pluginProcessors);
308 const getPluginRule = Map.prototype.get.bind(configArray.pluginRules);
311 for (const element of configArray) {
312 if (validated.has(element)) {
315 validated.add(element);
317 this.validateEnvironment(element.env, element.name, getPluginEnv);
318 this.validateGlobals(element.globals, element.name);
319 this.validateProcessor(element.processor, element.name, getPluginProcessor);
320 this.validateRules(element.rules, element.name, getPluginRule);