|
1 | 1 | "use strict";
|
2 | 2 |
|
| 3 | +const errorMessage = (schema, data, message) => ({ |
| 4 | + keyword: "absolutePath", |
| 5 | + params: { absolutePath: data }, |
| 6 | + message: message, |
| 7 | + parentSchema: schema |
| 8 | +}); |
| 9 | + |
3 | 10 | const getErrorFor = (shouldBeAbsolute, data, schema) => {
|
4 | 11 | const message = shouldBeAbsolute
|
5 | 12 | ? `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( |
7 | 14 | data
|
8 | 15 | )} is an absolute path!`;
|
9 | 16 |
|
10 |
| - return { |
11 |
| - keyword: "absolutePath", |
12 |
| - params: { absolutePath: data }, |
13 |
| - message: message, |
14 |
| - parentSchema: schema |
15 |
| - }; |
| 17 | + return errorMessage(schema, data, message); |
16 | 18 | };
|
| 19 | + |
17 | 20 | module.exports = ajv =>
|
18 | 21 | ajv.addKeyword("absolutePath", {
|
19 | 22 | errors: true,
|
20 | 23 | type: "string",
|
21 | 24 | compile(expected, schema) {
|
22 | 25 | 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) { |
25 | 45 | callback.errors = [getErrorFor(expected, data, schema)];
|
| 46 | + passes = false; |
26 | 47 | }
|
| 48 | + |
27 | 49 | return passes;
|
28 | 50 | }
|
29 | 51 | callback.errors = [];
|
| 52 | + |
30 | 53 | return callback;
|
31 | 54 | }
|
32 | 55 | });
|
0 commit comments