1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 | /** @typedef {import("ajv").default} Ajv */
|
---|
8 | /** @typedef {import("ajv").SchemaValidateFunction} SchemaValidateFunction */
|
---|
9 | /** @typedef {import("ajv").AnySchemaObject} AnySchemaObject */
|
---|
10 | /** @typedef {import("../validate").SchemaUtilErrorObject} SchemaUtilErrorObject */
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * @param {string} message
|
---|
14 | * @param {object} schema
|
---|
15 | * @param {string} data
|
---|
16 | * @returns {SchemaUtilErrorObject}
|
---|
17 | */
|
---|
18 | function errorMessage(message, schema, data) {
|
---|
19 | return {
|
---|
20 | // @ts-ignore
|
---|
21 | // eslint-disable-next-line no-undefined
|
---|
22 | dataPath: undefined,
|
---|
23 | // @ts-ignore
|
---|
24 | // eslint-disable-next-line no-undefined
|
---|
25 | schemaPath: undefined,
|
---|
26 | keyword: "absolutePath",
|
---|
27 | params: {
|
---|
28 | absolutePath: data
|
---|
29 | },
|
---|
30 | message,
|
---|
31 | parentSchema: schema
|
---|
32 | };
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * @param {boolean} shouldBeAbsolute
|
---|
37 | * @param {object} schema
|
---|
38 | * @param {string} data
|
---|
39 | * @returns {SchemaUtilErrorObject}
|
---|
40 | */
|
---|
41 | function getErrorFor(shouldBeAbsolute, schema, data) {
|
---|
42 | 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(data)} is an absolute path!`;
|
---|
43 | return errorMessage(message, schema, data);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | *
|
---|
48 | * @param {Ajv} ajv
|
---|
49 | * @returns {Ajv}
|
---|
50 | */
|
---|
51 | function addAbsolutePathKeyword(ajv) {
|
---|
52 | ajv.addKeyword({
|
---|
53 | keyword: "absolutePath",
|
---|
54 | type: "string",
|
---|
55 | errors: true,
|
---|
56 | /**
|
---|
57 | * @param {boolean} schema
|
---|
58 | * @param {AnySchemaObject} parentSchema
|
---|
59 | * @returns {SchemaValidateFunction}
|
---|
60 | */
|
---|
61 | compile(schema, parentSchema) {
|
---|
62 | /** @type {SchemaValidateFunction} */
|
---|
63 | const callback = data => {
|
---|
64 | let passes = true;
|
---|
65 | const isExclamationMarkPresent = data.includes("!");
|
---|
66 | if (isExclamationMarkPresent) {
|
---|
67 | callback.errors = [errorMessage(`The provided value ${JSON.stringify(data)} contains exclamation mark (!) which is not allowed because it's reserved for loader syntax.`, parentSchema, data)];
|
---|
68 | passes = false;
|
---|
69 | }
|
---|
70 |
|
---|
71 | // ?:[A-Za-z]:\\ - Windows absolute path
|
---|
72 | // \\\\ - Windows network absolute path
|
---|
73 | // \/ - Unix-like OS absolute path
|
---|
74 | const isCorrectAbsolutePath = schema === /^(?:[A-Za-z]:(\\|\/)|\\\\|\/)/.test(data);
|
---|
75 | if (!isCorrectAbsolutePath) {
|
---|
76 | callback.errors = [getErrorFor(schema, parentSchema, data)];
|
---|
77 | passes = false;
|
---|
78 | }
|
---|
79 | return passes;
|
---|
80 | };
|
---|
81 | callback.errors = [];
|
---|
82 | return callback;
|
---|
83 | }
|
---|
84 | });
|
---|
85 | return ajv;
|
---|
86 | }
|
---|
87 | var _default = exports.default = addAbsolutePathKeyword; |
---|