Skip to content

Commit

Permalink
Add validateObjectWithArrayProps as a flexible utility for evaluating…
Browse files Browse the repository at this point in the history
… secondary options
  • Loading branch information
CAYdenberg committed Dec 7, 2018
1 parent ed56990 commit 20ad772
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 36 deletions.
6 changes: 3 additions & 3 deletions lib/rules/unit-blacklist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const mediaParser = require("postcss-media-query-parser").default;
const optionsMatches = require("../../utils/optionsMatches");
const report = require("../../utils/report");
const ruleMessages = require("../../utils/ruleMessages");
const validateObjectWithStringOrRegexArrayProps = require("../../utils/validateObjectWithStringOrRegexArrayProps");
const validateObjectWithArrayProps = require("../../utils/validateObjectWithArrayProps");
const validateOptions = require("../../utils/validateOptions");
const valueParser = require("postcss-value-parser");

Expand Down Expand Up @@ -41,8 +41,8 @@ const rule = function(blacklistInput, options) {
optional: true,
actual: options,
possible: {
ignoreProperties: validateObjectWithStringOrRegexArrayProps,
ignoreMediaFeatureNames: validateObjectWithStringOrRegexArrayProps
ignoreProperties: validateObjectWithArrayProps([_.isString, _.isRegExp]),
ignoreMediaFeatureNames: validateObjectWithArrayProps([_.isString, _.isRegExp]),
}
}
);
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/unit-whitelist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const getUnitFromValueNode = require("../../utils/getUnitFromValueNode");
const optionsMatches = require("../../utils/optionsMatches");
const report = require("../../utils/report");
const ruleMessages = require("../../utils/ruleMessages");
const validateObjectWithStringOrRegexArrayProps = require("../../utils/validateObjectWithStringOrRegexArrayProps");
const validateObjectWithArrayProps = require("../../utils/validateObjectWithArrayProps");
const validateOptions = require("../../utils/validateOptions");
const valueParser = require("postcss-value-parser");

Expand All @@ -32,7 +32,7 @@ const rule = function(whitelistInput, options) {
optional: true,
actual: options,
possible: {
ignoreProperties: validateObjectWithStringOrRegexArrayProps
ignoreProperties: validateObjectWithArrayProps([_.isString, _.isRegExp])
}
}
);
Expand Down
52 changes: 52 additions & 0 deletions lib/utils/__tests__/validateObjectWithArrayProps.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use strict";

const validateObjectWithArrayProps = require("../validateObjectWithArrayProps");

describe('validateObjectWithArrayProps', () => {
it('should return a function', () => {
expect(validateObjectWithArrayProps(x => x)).toBeInstanceOf(Function)
})

describe('returned validator', () => {
const validator = validateObjectWithArrayProps(x => x)

it('should return false if any of the object properties are not an array', () => {
expect(validator({
arrayProp: [1, 2],
nonArrayProp: 3
})).toBeFalsy()
})

it('should return false if any of the object properties array values do not pass the test', () => {
expect(validator({
arrayProp: [1, 2],
nonArrayProp: [0, 3]
})).toBeFalsy()
})

it('should return true otherwise', () => {
expect(validator({
arrayProp: [1, 2],
nonArrayProp: [3, 4]
})).toBeTruthy()
})
})

describe('returned validator with array', () => {
const validator = validateObjectWithArrayProps([x => x > 0, x => x < 0])

it('should accept an array of validators, any of which can return true', () => {
expect(validator({
arrayProp: [1, 2],
nonArrayProp: [-1, 3]
})).toBeTruthy()
})

it('should be false if none of the validators are true for any value', () => {
expect(validator({
arrayProp: [1, 2],
nonArrayProp: [-1, 0]
})).toBeFalsy()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,3 @@ it("validateObjectWithStringArrayProps", () => {
validateObjectWithStringArrayProps({ prop1: ["1"], prop2: ["object", {}] })
).toBeFalsy();
});

it("allows regular expressions if an optional second variable is set to true", () => {
expect(validateObjectWithStringArrayProps({ prop: [/val/, "val"] }, true))
.toBeTruthy();

expect(validateObjectWithStringArrayProps({ prop: [3.14] }, true))
.toBeFalsy();
});
39 changes: 39 additions & 0 deletions lib/utils/validateObjectWithArrayProps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* @flow */
"use strict";

const _ = require("lodash");

/**
* Check whether the variable is an object and all it's properties are arrays of string values:
*
* ignoreProperties = {
* value1: ["item11", "item12", "item13"],
* value2: ["item21", "item22", "item23"],
* value3: ["item31", "item32", "item33"],
* }
*/

module.exports = (
validator /*: Function */
) => (
value /*: Object*/
) /*: boolean*/ => {
if (!_.isPlainObject(value)) {
return false;
}

return Object.keys(value).every(key => {
if (!_.isArray(value[key])) {
return false;
}

// Make sure the array items are strings
return value[key].every(item => {
if (Array.isArray(validator)) {
return validator.some(v => v(item))
}

return validator(item)
});
});
};
25 changes: 2 additions & 23 deletions lib/utils/validateObjectWithStringArrayProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"use strict";

const _ = require("lodash");
const validateObjectWithArrayProps = require('./validateObjectWithArrayProps')

/**
* Check whether the variable is an object and all it's properties are arrays of string values:
Expand All @@ -13,26 +14,4 @@ const _ = require("lodash");
* }
*/

module.exports = function(
value /*: Object*/,
acceptRegex/*: Boolean*/
) /*: boolean*/ {
if (!_.isPlainObject(value)) {
return false;
}

return Object.keys(value).every(key => {
if (!_.isArray(value[key])) {
return false;
}

// Make sure the array items are strings
return value[key].every(item => {
if (acceptRegex && _.isRegExp(item)) {
return true
}

return _.isString(item)
});
});
};
module.exports = validateObjectWithArrayProps(_.isString)

0 comments on commit 20ad772

Please sign in to comment.