Skip to content

Commit

Permalink
fix(configure): validate rules and checks properties
Browse files Browse the repository at this point in the history
  • Loading branch information
straker committed Jun 16, 2020
1 parent 86ada3f commit 8c91ead
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/core/public/configure.js
Expand Up @@ -44,14 +44,40 @@ function configureChecksRulesAndBranding(spec) {
}

if (spec.checks) {
if (!Array.isArray(spec.checks)) {
throw new TypeError('Checks property must be an array');
}

spec.checks.forEach(function(check) {
if (!check.id) {
throw new TypeError(
// eslint-disable-next-line max-len
`Configured check ${JSON.stringify(
check
)} is invalid. Checks must be an object with at least an id property`
);
}

audit.addCheck(check);
});
}

const modifiedRules = [];
if (spec.rules) {
if (!Array.isArray(spec.rules)) {
throw new TypeError('Rules property must be an array');
}

spec.rules.forEach(function(rule) {
if (!rule.id) {
throw new TypeError(
// eslint-disable-next-line max-len
`Configured rule ${JSON.stringify(
rule
)} is invalid. Rules must be an object with at least an id property`
);
}

modifiedRules.push(rule.id);
audit.addRule(rule);
});
Expand Down
48 changes: 48 additions & 0 deletions test/core/public/configure.js
Expand Up @@ -56,6 +56,30 @@ describe('axe.configure', function() {
assert.deepEqual(axe._audit.data.rules.bob.joe, 'joe');
});

it('should throw error if rules property is invalid', function() {
assert.throws(function() {
axe.configure({ rules: 'hello' }),
TypeError,
/^Rules property must be an array/;
});
});

it('should throw error if rule is invalid', function() {
assert.throws(function() {
axe.configure({ rules: ['hello'] }),
TypeError,
/Configured rule "hello" is invalid/;
});
});

it('should throw error if rule does not have an id', function() {
assert.throws(function() {
axe.configure({ rules: [{ foo: 'bar' }] }),
TypeError,
/Configured rule "{foo:\"bar\"}" is invalid/;
});
});

it('should call setBranding when passed options', function() {
axe._load({});
axe.configure({
Expand Down Expand Up @@ -158,6 +182,30 @@ describe('axe.configure', function() {
assert.equal(axe._audit.data.checks.bob.joe, 'joe');
});

it('should throw error if checks property is invalid', function() {
assert.throws(function() {
axe.configure({ checks: 'hello' }),
TypeError,
/^Checks property must be an array/;
});
});

it('should throw error if check is invalid', function() {
assert.throws(function() {
axe.configure({ checks: ['hello'] }),
TypeError,
/Configured check "hello" is invalid/;
});
});

it('should throw error if check does not have an id', function() {
assert.throws(function() {
axe.configure({ checks: [{ foo: 'bar' }] }),
TypeError,
/Configured check "{foo:\"bar\"}" is invalid/;
});
});

it('should allow for the overwriting of checks', function() {
axe._load({
data: {
Expand Down

0 comments on commit 8c91ead

Please sign in to comment.