Skip to content

Commit 04d8188

Browse files
authored
Merge pull request webpack#6754 from byzyk/fix/6742
fix: validation error when ! is in the path
2 parents 0d3063e + f99f96d commit 04d8188

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

schemas/ajv.absolutePath.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,55 @@
11
"use strict";
22

3+
const errorMessage = (schema, data, message) => ({
4+
keyword: "absolutePath",
5+
params: { absolutePath: data },
6+
message: message,
7+
parentSchema: schema
8+
});
9+
310
const getErrorFor = (shouldBeAbsolute, data, schema) => {
411
const message = shouldBeAbsolute
512
? `The provided value ${JSON.stringify(data)} is not an absolute path!`
6-
: `A relative path is expected. However the provided value ${JSON.stringify(
13+
: `A relative path is expected. However, the provided value ${JSON.stringify(
714
data
815
)} is an absolute path!`;
916

10-
return {
11-
keyword: "absolutePath",
12-
params: { absolutePath: data },
13-
message: message,
14-
parentSchema: schema
15-
};
17+
return errorMessage(schema, data, message);
1618
};
19+
1720
module.exports = ajv =>
1821
ajv.addKeyword("absolutePath", {
1922
errors: true,
2023
type: "string",
2124
compile(expected, schema) {
2225
function callback(data) {
23-
const passes = expected === /^(?:[A-Za-z]:\\|\/)/.test(data);
24-
if (!passes) {
26+
let passes = true;
27+
const isExclamationMarkPresent = data.includes("!");
28+
const isCorrectAbsoluteOrRelativePath =
29+
expected === /^(?:[A-Za-z]:\\|\/)/.test(data);
30+
31+
if (isExclamationMarkPresent) {
32+
callback.errors = [
33+
errorMessage(
34+
schema,
35+
data,
36+
`The provided value ${JSON.stringify(
37+
data
38+
)} contans exclamation mark (!) which is not allowed because it's reserved for loader syntax.`
39+
)
40+
];
41+
passes = false;
42+
}
43+
44+
if (!isCorrectAbsoluteOrRelativePath) {
2545
callback.errors = [getErrorFor(expected, data, schema)];
46+
passes = false;
2647
}
48+
2749
return passes;
2850
}
2951
callback.errors = [];
52+
3053
return callback;
3154
}
3255
});

test/Validation.test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,20 @@ describe("Validation", () => {
231231
" * configuration.devtool should be false"
232232
]
233233
},
234+
{
235+
name: "! in path",
236+
config: {
237+
entry: "foo.js",
238+
output: {
239+
path: "/somepath/!test",
240+
filename: "bar"
241+
}
242+
},
243+
message: [
244+
' - configuration.output.path: The provided value "/somepath/!test" contans exclamation mark (!) which is not allowed because it\'s reserved for loader syntax.',
245+
" -> The output directory as **absolute path** (required)."
246+
]
247+
},
234248
{
235249
name: "relative path",
236250
config: {
@@ -240,7 +254,7 @@ describe("Validation", () => {
240254
}
241255
},
242256
message: [
243-
' - configuration.output.filename: A relative path is expected. However the provided value "/bar" is an absolute path!',
257+
' - configuration.output.filename: A relative path is expected. However, the provided value "/bar" is an absolute path!',
244258
" -> Specifies the name of each output file on disk. You must **not** specify an absolute path here! The `output.path` option determines the location on disk the files are written to, filename is used solely for naming the individual files.",
245259
" Please use output.path to specify absolute path and output.filename for the file name."
246260
]

0 commit comments

Comments
 (0)