Blame view
node_modules/eslint/lib/config/config-validator.js
4.45 KB
c39994410
|
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
/** * @fileoverview Validates configs. * @author Brandon Mills * @copyright 2015 Brandon Mills */ "use strict"; //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ var rules = require("../rules"), environments = require("../../conf/environments"), schemaValidator = require("is-my-json-valid"); var validators = { rules: Object.create(null) }; //------------------------------------------------------------------------------ // Private //------------------------------------------------------------------------------ /** * Gets a complete options schema for a rule. * @param {string} id The rule's unique name. * @returns {object} JSON Schema for the rule's options. */ function getRuleOptionsSchema(id) { var rule = rules.get(id), schema = rule && rule.schema; if (!schema) { return { "type": "array", "items": [ { "enum": [0, 1, 2] } ], "minItems": 1 }; } // Given a tuple of schemas, insert warning level at the beginning if (Array.isArray(schema)) { return { "type": "array", "items": [ { "enum": [0, 1, 2] } ].concat(schema), "minItems": 1, "maxItems": schema.length + 1 }; } // Given a full schema, leave it alone return schema; } /** * Validates a rule's options against its schema. * @param {string} id The rule's unique name. * @param {array|number} options The given options for the rule. * @param {string} source The name of the configuration source. * @returns {void} */ function validateRuleOptions(id, options, source) { var validateRule = validators.rules[id], message; if (!validateRule) { validateRule = schemaValidator(getRuleOptionsSchema(id), { verbose: true }); validators.rules[id] = validateRule; } if (typeof options === "number") { options = [options]; } validateRule(options); if (validateRule.errors) { message = [ source, ": ", "\tConfiguration for rule \"", id, "\" is invalid: " ]; validateRule.errors.forEach(function(error) { if (error.field === "data[\"0\"]") { // better error for severity message.push( "\tSeverity should be one of the following: 0 = off, 1 = warning, 2 = error (you passed \"", error.value, "\"). "); } else { message.push( "\tValue \"", error.value, "\" ", error.message, ". " ); } }); throw new Error(message.join("")); } } /** * Validates an environment object * @param {object} environment The environment config object to validate. * @param {string} source The location to report with any errors. * @returns {void} */ function validateEnvironment(environment, source) { // not having an environment is ok if (!environment) { return; } if (Array.isArray(environment)) { throw new Error("Environment must not be an array"); } if (typeof environment === "object") { Object.keys(environment).forEach(function(env) { if (!environments[env]) { var message = [ source, ": ", "\tEnvironment key \"", env, "\" is unknown " ]; throw new Error(message.join("")); } }); } else { throw new Error("Environment must be an object"); } } /** * Validates an entire config object. * @param {object} config The config object to validate. * @param {string} source The location to report with any errors. * @returns {void} */ function validate(config, source) { if (typeof config.rules === "object") { Object.keys(config.rules).forEach(function(id) { validateRuleOptions(id, config.rules[id], source); }); } validateEnvironment(config.env, source); } //------------------------------------------------------------------------------ // Public Interface //------------------------------------------------------------------------------ module.exports = { getRuleOptionsSchema: getRuleOptionsSchema, validate: validate, validateRuleOptions: validateRuleOptions }; |