1
|
"use strict";
|
2
|
|
3
|
const errorMessage = (schema, data, message) => ({
|
4
|
keyword: "absolutePath",
|
5
|
params: { absolutePath: data },
|
6
|
message: message,
|
7
|
parentSchema: schema
|
8
|
});
|
9
|
|
10
|
const getErrorFor = (shouldBeAbsolute, data, schema) => {
|
11
|
const message = shouldBeAbsolute
|
12
|
? `The provided value ${JSON.stringify(data)} is not an absolute path!`
|
13
|
: `A relative path is expected. However, the provided value ${JSON.stringify(
|
14
|
data
|
15
|
)} is an absolute path!`;
|
16
|
|
17
|
return errorMessage(schema, data, message);
|
18
|
};
|
19
|
|
20
|
module.exports = ajv =>
|
21
|
ajv.addKeyword("absolutePath", {
|
22
|
errors: true,
|
23
|
type: "string",
|
24
|
compile(expected, schema) {
|
25
|
function callback(data) {
|
26
|
let passes = true;
|
27
|
const isExclamationMarkPresent = data.includes("!");
|
28
|
|
29
|
if (isExclamationMarkPresent) {
|
30
|
callback.errors = [
|
31
|
errorMessage(
|
32
|
schema,
|
33
|
data,
|
34
|
`The provided value ${JSON.stringify(
|
35
|
data
|
36
|
)} contains exclamation mark (!) which is not allowed because it's reserved for loader syntax.`
|
37
|
)
|
38
|
];
|
39
|
passes = false;
|
40
|
}
|
41
|
// ?:[A-Za-z]:\\ - Windows absolute path
|
42
|
// \\\\ - Windows network absolute path
|
43
|
// \/ - Unix-like OS absolute path
|
44
|
const isCorrectAbsolutePath =
|
45
|
expected === /^(?:[A-Za-z]:\\|\\\\|\/)/.test(data);
|
46
|
if (!isCorrectAbsolutePath) {
|
47
|
callback.errors = [getErrorFor(expected, data, schema)];
|
48
|
passes = false;
|
49
|
}
|
50
|
|
51
|
return passes;
|
52
|
}
|
53
|
callback.errors = [];
|
54
|
|
55
|
return callback;
|
56
|
}
|
57
|
});
|