Skip to content

Commit

Permalink
Merge pull request #6754 from byzyk/fix/6742
Browse files Browse the repository at this point in the history
fix: validation error when ! is in the path
  • Loading branch information
sokra committed Mar 21, 2018
2 parents 0d3063e + f99f96d commit 04d8188
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
41 changes: 32 additions & 9 deletions schemas/ajv.absolutePath.js
@@ -1,32 +1,55 @@
"use strict";

const errorMessage = (schema, data, message) => ({
keyword: "absolutePath",
params: { absolutePath: data },
message: message,
parentSchema: schema
});

const getErrorFor = (shouldBeAbsolute, data, schema) => {
const message = shouldBeAbsolute
? `The provided value ${JSON.stringify(data)} is not an absolute path!`
: `A relative path is expected. However the provided value ${JSON.stringify(
: `A relative path is expected. However, the provided value ${JSON.stringify(
data
)} is an absolute path!`;

return {
keyword: "absolutePath",
params: { absolutePath: data },
message: message,
parentSchema: schema
};
return errorMessage(schema, data, message);
};

module.exports = ajv =>
ajv.addKeyword("absolutePath", {
errors: true,
type: "string",
compile(expected, schema) {
function callback(data) {
const passes = expected === /^(?:[A-Za-z]:\\|\/)/.test(data);
if (!passes) {
let passes = true;
const isExclamationMarkPresent = data.includes("!");
const isCorrectAbsoluteOrRelativePath =
expected === /^(?:[A-Za-z]:\\|\/)/.test(data);

if (isExclamationMarkPresent) {
callback.errors = [
errorMessage(
schema,
data,
`The provided value ${JSON.stringify(
data
)} contans exclamation mark (!) which is not allowed because it's reserved for loader syntax.`
)
];
passes = false;
}

if (!isCorrectAbsoluteOrRelativePath) {
callback.errors = [getErrorFor(expected, data, schema)];
passes = false;
}

return passes;
}
callback.errors = [];

return callback;
}
});
16 changes: 15 additions & 1 deletion test/Validation.test.js
Expand Up @@ -231,6 +231,20 @@ describe("Validation", () => {
" * configuration.devtool should be false"
]
},
{
name: "! in path",
config: {
entry: "foo.js",
output: {
path: "/somepath/!test",
filename: "bar"
}
},
message: [
' - configuration.output.path: The provided value "/somepath/!test" contans exclamation mark (!) which is not allowed because it\'s reserved for loader syntax.',
" -> The output directory as **absolute path** (required)."
]
},
{
name: "relative path",
config: {
Expand All @@ -240,7 +254,7 @@ describe("Validation", () => {
}
},
message: [
' - configuration.output.filename: A relative path is expected. However the provided value "/bar" is an absolute path!',
' - configuration.output.filename: A relative path is expected. However, the provided value "/bar" is an absolute path!',
" -> 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.",
" Please use output.path to specify absolute path and output.filename for the file name."
]
Expand Down

0 comments on commit 04d8188

Please sign in to comment.