Skip to content

Commit

Permalink
Breaking: disallow invalid rule defaults in RuleTester (fixes #11473)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Apr 7, 2019
1 parent c021117 commit 30836bb
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/config/config-validator.js
Expand Up @@ -10,12 +10,12 @@
//------------------------------------------------------------------------------

const path = require("path"),
ajv = require("../util/ajv"),
lodash = require("lodash"),
configSchema = require("../../conf/config-schema.js"),
util = require("util"),
ConfigOps = require("./config-ops");

const ajv = require("../util/ajv")();
const ruleValidators = new WeakMap();

//------------------------------------------------------------------------------
Expand Down
15 changes: 14 additions & 1 deletion lib/testers/rule-tester.js
Expand Up @@ -45,12 +45,13 @@ const lodash = require("lodash"),
path = require("path"),
util = require("util"),
validator = require("../config/config-validator"),
ajv = require("../util/ajv"),
Linter = require("../linter"),
Environments = require("../config/environments"),
SourceCodeFixer = require("../util/source-code-fixer"),
interpolate = require("../util/interpolate");

const ajv = require("../util/ajv")({ strictDefaults: true });

//------------------------------------------------------------------------------
// Private Members
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -383,6 +384,18 @@ class RuleTester {

throw new Error([`Schema for rule ${ruleName} is invalid:`, errors]);
}

/*
* `ajv.validateSchema` checks for errors in the structure of the schema (by comparing the schema against a "meta-schema"),
* and it reports those errors individually. However, there are other types of schema errors that only occur when compiling
* the schema (e.g. using invalid defaults in a schema), and only one of these errors can be reported at a time. As a result,
* the schema is compiled here separately from checking for `validateSchema` errors.
*/
try {
ajv.compile(schema);
} catch (err) {
throw new Error(`Schema for rule ${ruleName} is invalid: ${err.message}`);
}
}

validator.validate(config, ruleMap.get.bind(ruleMap), new Environments(), "rule-tester");
Expand Down
27 changes: 15 additions & 12 deletions lib/util/ajv.js
Expand Up @@ -15,17 +15,20 @@ const Ajv = require("ajv"),
// Public Interface
//------------------------------------------------------------------------------

const ajv = new Ajv({
meta: false,
useDefaults: true,
validateSchema: false,
missingRefs: "ignore",
verbose: true,
schemaId: "auto"
});
module.exports = (additionalOptions = {}) => {
const ajv = new Ajv({
meta: false,
useDefaults: true,
validateSchema: false,
missingRefs: "ignore",
verbose: true,
schemaId: "auto",
...additionalOptions
});

ajv.addMetaSchema(metaSchema);
// eslint-disable-next-line no-underscore-dangle
ajv._opts.defaultMeta = metaSchema.id;
ajv.addMetaSchema(metaSchema);
// eslint-disable-next-line no-underscore-dangle
ajv._opts.defaultMeta = metaSchema.id;

module.exports = ajv;
return ajv;
};
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -36,7 +36,7 @@
"bugs": "https://github.com/eslint/eslint/issues/",
"dependencies": {
"@babel/code-frame": "^7.0.0",
"ajv": "^6.9.1",
"ajv": "^6.10.0",
"chalk": "^2.1.0",
"cross-spawn": "^6.0.5",
"debug": "^4.0.1",
Expand Down
5 changes: 3 additions & 2 deletions tests/conf/config-schema.js
Expand Up @@ -9,10 +9,11 @@
// Requirements
//------------------------------------------------------------------------------

const ajv = require("../../lib/util/ajv"),
configSchema = require("../../conf/config-schema.js"),
const configSchema = require("../../conf/config-schema.js"),
assert = require("assert");

const ajv = require("../../lib/util/ajv")();

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
Expand Down
37 changes: 37 additions & 0 deletions tests/lib/testers/rule-tester.js
Expand Up @@ -659,6 +659,43 @@ describe("RuleTester", () => {

});

it("should disallow invalid defaults in rules", () => {
const ruleWithInvalidDefaults = {
meta: {
schema: [
{
oneOf: [
{ enum: ["foo"] },
{
type: "object",
properties: {
foo: {
enum: ["foo", "bar"],
default: "foo"
}
},
additionalProperties: false
}
]
}
]
},
create: () => ({})
};

assert.throws(() => {
ruleTester.run("invalid-defaults", ruleWithInvalidDefaults, {
valid: [
{
code: "foo",
options: [{}]
}
],
invalid: []
});
}, /Schema for rule invalid-defaults is invalid: default is ignored for: data1\.foo/u);
});

it("throw an error when an unknown config option is included", () => {
assert.throws(() => {
ruleTester.run("no-eval", require("../../fixtures/testers/rule-tester/no-eval"), {
Expand Down

0 comments on commit 30836bb

Please sign in to comment.