Skip to content

Commit 9085e1e

Browse files
authored
Merge pull request webpack#3232 from webpack/SpaceK33z-fix-schema-instanceof
Fix validation schema accepting invalid values when `instanceof` is used
2 parents c7de73e + 7add543 commit 9085e1e

5 files changed

Lines changed: 39 additions & 3 deletions

File tree

lib/WebpackOptionsValidationError.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ WebpackOptionsValidationError.formatValidationError = function formatValidationE
7575
}
7676
return dataPath + " should be " + err.params.type + ":\n" +
7777
getSchemaPartText(err.parentSchema);
78+
case "instanceof":
79+
return dataPath + " should be an instance of " + getSchemaPartText(err.parentSchema) + ".";
7880
case "required":
7981
var missingProperty = err.params.missingProperty.replace(/^\./, "");
8082
return dataPath + " misses the property '" + missingProperty + "'.\n" +

lib/validateSchema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var ajv = new Ajv({
88
allErrors: true,
99
verbose: true
1010
});
11+
require('ajv-keywords')(ajv);
1112

1213
function validateSchema(schema, options) {
1314
if(Array.isArray(options)) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"dependencies": {
77
"acorn": "^4.0.3",
88
"ajv": "^4.7.0",
9+
"ajv-keywords": "^1.1.1",
910
"async": "^2.1.2",
1011
"enhanced-resolve": "^2.2.0",
1112
"interpret": "^1.0.0",

schemas/webpackOptionsSchema.json

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,14 @@
7676
},
7777
{
7878
"additionalProperties": {
79-
"type": "string"
79+
"anyOf": [
80+
{
81+
"type": "string"
82+
},
83+
{
84+
"type": "object"
85+
}
86+
]
8087
},
8188
"description": "If an dependency matches exactly a property of the object, the property value is used as dependency.",
8289
"type": "object"
@@ -109,7 +116,14 @@
109116
"type": "boolean"
110117
},
111118
"exprContextRegExp": {
112-
"instanceof": "RegExp"
119+
"anyOf": [
120+
{
121+
"type": "boolean"
122+
},
123+
{
124+
"instanceof": "RegExp"
125+
}
126+
]
113127
},
114128
"exprContextRequest": {
115129
"type": "string"
@@ -153,7 +167,14 @@
153167
"type": "boolean"
154168
},
155169
"unknownContextRegExp": {
156-
"instanceof": "RegExp"
170+
"anyOf": [
171+
{
172+
"type": "boolean"
173+
},
174+
{
175+
"instanceof": "RegExp"
176+
}
177+
]
157178
},
158179
"unknownContextRequest": {
159180
"type": "string"

test/Validation.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ describe("Validation", function() {
3333
" object { <key>: string | [string] } | string | [string]",
3434
" The entry point(s) of the compilation."
3535
]
36+
}, {
37+
name: "invalid instanceof",
38+
config: {
39+
entry: "a",
40+
module: {
41+
wrappedContextRegExp: 1337
42+
}
43+
},
44+
message: [
45+
" - configuration.module.wrappedContextRegExp should be an instance of RegExp.",
46+
]
3647
}, {
3748
name: "multiple errors",
3849
config: {

0 commit comments

Comments
 (0)