UNPKG

1.36 kBJavaScriptView Raw
1"use strict";
2
3const errorMessage = (schema, data, message) => ({
4 keyword: "absolutePath",
5 params: { absolutePath: data },
6 message: message,
7 parentSchema: schema
8});
9
10const 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
20module.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 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 )} contains exclamation mark (!) which is not allowed because it's reserved for loader syntax.`
39 )
40 ];
41 passes = false;
42 }
43
44 if (!isCorrectAbsoluteOrRelativePath) {
45 callback.errors = [getErrorFor(expected, data, schema)];
46 passes = false;
47 }
48
49 return passes;
50 }
51 callback.errors = [];
52
53 return callback;
54 }
55 });