4 const _ = require("lodash");
6 const ignoredOptions = ["severity", "message"];
9 * Validate a rule's options.
11 * See existing rules for examples.
13 * @param {Result} result - postcss result
14 * @param {string} ruleName
15 * @param {...object} ...optionDescriptions - Each optionDescription can
16 * have the following properties:
17 * - `actual` (required): the actual passed option value or object.
18 * - `possible` (required): a schema representation of what values are
19 * valid for those options. `possible` should be an object if the
20 * options are an object, with corresponding keys; if the options are not an
21 * object, `possible` isn't, either. All `possible` value representations
22 * should be **arrays of either values or functions**. Values are === checked
23 * against `actual`. Functions are fed `actual` as an argument and their
24 * return value is interpreted: truthy = valid, falsy = invalid.
25 * - `optional` (optional): If this is `true`, `actual` can be undefined.
26 * @return {boolean} Whether or not the options are valid (true = valid)
28 module.exports = function(
34 const optionDescriptions = Array.from(arguments).slice(2);
36 optionDescriptions.forEach(optionDescription => {
37 validate(optionDescription, ruleName, complain);
40 function complain(message) {
42 result.warn(message, {
43 stylelintType: "invalidOption"
45 _.set(result, "stylelint.stylelintError", true);
51 function validate(opts, ruleName, complain) {
52 const possible = opts.possible;
53 const actual = opts.actual;
54 const optional = opts.optional;
56 if (actual === null || _.isEqual(actual, [null])) {
60 const nothingPossible =
61 possible === undefined ||
62 (Array.isArray(possible) && possible.length === 0);
64 if (nothingPossible && actual === true) {
68 if (actual === undefined) {
69 if (nothingPossible || optional) {
72 complain(`Expected option value for rule "${ruleName}"`);
74 } else if (nothingPossible) {
77 `Incorrect configuration for rule "${
79 }". Rule should have "possible" values for options validation`
85 complain(`Unexpected option value "${actual}" for rule "${ruleName}"`);
89 // If `possible` is a function ...
90 if (_.isFunction(possible)) {
91 if (!possible(actual)) {
93 `Invalid option "${JSON.stringify(actual)}" for rule ${ruleName}`
99 // If `possible` is an array instead of an object ...
100 if (!_.isPlainObject(possible)) {
101 [].concat(actual).forEach(a => {
102 if (isValid(possible, a)) {
105 complain(`Invalid option value "${a}" for rule "${ruleName}"`);
110 // If possible is an object ...
111 if (!_.isPlainObject(actual)) {
113 `Invalid option value ${JSON.stringify(actual)} for rule "${
115 }": ` + "should be an object"
120 Object.keys(actual).forEach(optionName => {
121 if (ignoredOptions.indexOf(optionName) !== -1) {
125 if (!possible[optionName]) {
126 complain(`Invalid option name "${optionName}" for rule "${ruleName}"`);
130 const actualOptionValue = actual[optionName];
131 [].concat(actualOptionValue).forEach(a => {
132 if (isValid(possible[optionName], a)) {
136 `Invalid value "${a}" for option "${optionName}" of rule "${ruleName}"`
142 function isValid(possible, actual) {
143 const possibleList = [].concat(possible);
144 for (let i = 0, l = possibleList.length; i < l; i++) {
145 const possibility = possibleList[i];
146 if (typeof possibility === "function" && possibility(actual)) {
149 if (actual === possibility) {