Skip to content

Commit f513065

Browse files
committed
fix: throw validation error when exclamation mark was found in path
1 parent a515918 commit f513065

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

schemas/ajv.absolutePath.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
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
};
1719

1820
module.exports = ajv =>
@@ -21,13 +23,32 @@ module.exports = ajv =>
2123
type: "string",
2224
compile(expected, schema) {
2325
function callback(data) {
24-
const passes = expected === /^(?!(?:[A-Za-z]:\\|\/).*!)/.test(data);
25-
if (!passes) {
26-
callback.errors.push(getErrorFor(expected, data, schema));
26+
let passes = true;
27+
const isExclamationMarkPresent = data.indexOf("!") !== -1;
28+
const isAbsolutePath = expected === /^(?:[A-Za-z]:\\|\/)/.test(data);
29+
30+
if (isExclamationMarkPresent) {
31+
callback.errors = [
32+
errorMessage(
33+
schema,
34+
data,
35+
`The provided value ${JSON.stringify(
36+
data
37+
)} contans exclamation mark (!) which is not allowed because it's reserved for loader syntax.`
38+
)
39+
];
40+
passes = false;
41+
}
42+
43+
if (!isAbsolutePath) {
44+
callback.errors = [getErrorFor(expected, data, schema)];
45+
passes = false;
2746
}
47+
2848
return passes;
2949
}
3050
callback.errors = [];
51+
3152
return callback;
3253
}
3354
});

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)