| 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 message
|
|---|
| 14 | * @param {object} schema schema
|
|---|
| 15 | * @param {string} data data
|
|---|
| 16 | * @returns {SchemaUtilErrorObject} error object
|
|---|
| 17 | */
|
|---|
| 18 | function errorMessage(message, schema, data) {
|
|---|
| 19 | return {
|
|---|
| 20 | dataPath: undefined,
|
|---|
| 21 | // @ts-expect-error
|
|---|
| 22 | schemaPath: undefined,
|
|---|
| 23 | keyword: "absolutePath",
|
|---|
| 24 | params: {
|
|---|
| 25 | absolutePath: data
|
|---|
| 26 | },
|
|---|
| 27 | message,
|
|---|
| 28 | parentSchema: schema
|
|---|
| 29 | };
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * @param {boolean} shouldBeAbsolute true when should be absolute path, otherwise false
|
|---|
| 34 | * @param {object} schema schema
|
|---|
| 35 | * @param {string} data data
|
|---|
| 36 | * @returns {SchemaUtilErrorObject} error object
|
|---|
| 37 | */
|
|---|
| 38 | function getErrorFor(shouldBeAbsolute, schema, data) {
|
|---|
| 39 | 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!`;
|
|---|
| 40 | return errorMessage(message, schema, data);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | /**
|
|---|
| 44 | * @param {Ajv} ajv ajv
|
|---|
| 45 | * @returns {Ajv} configured ajv
|
|---|
| 46 | */
|
|---|
| 47 | function addAbsolutePathKeyword(ajv) {
|
|---|
| 48 | ajv.addKeyword({
|
|---|
| 49 | keyword: "absolutePath",
|
|---|
| 50 | type: "string",
|
|---|
| 51 | errors: true,
|
|---|
| 52 | /**
|
|---|
| 53 | * @param {boolean} schema schema
|
|---|
| 54 | * @param {AnySchemaObject} parentSchema parent schema
|
|---|
| 55 | * @returns {SchemaValidateFunction} validate function
|
|---|
| 56 | */
|
|---|
| 57 | compile(schema, parentSchema) {
|
|---|
| 58 | /** @type {SchemaValidateFunction} */
|
|---|
| 59 | const callback = data => {
|
|---|
| 60 | let passes = true;
|
|---|
| 61 | const isExclamationMarkPresent = data.includes("!");
|
|---|
| 62 | if (isExclamationMarkPresent) {
|
|---|
| 63 | 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)];
|
|---|
| 64 | passes = false;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | // ?:[A-Za-z]:\\ - Windows absolute path
|
|---|
| 68 | // \\\\ - Windows network absolute path
|
|---|
| 69 | // \/ - Unix-like OS absolute path
|
|---|
| 70 | const isCorrectAbsolutePath = schema === /^(?:[A-Za-z]:(\\|\/)|\\\\|\/)/.test(data);
|
|---|
| 71 | if (!isCorrectAbsolutePath) {
|
|---|
| 72 | callback.errors = [getErrorFor(schema, parentSchema, data)];
|
|---|
| 73 | passes = false;
|
|---|
| 74 | }
|
|---|
| 75 | return passes;
|
|---|
| 76 | };
|
|---|
| 77 | callback.errors = [];
|
|---|
| 78 | return callback;
|
|---|
| 79 | }
|
|---|
| 80 | });
|
|---|
| 81 | return ajv;
|
|---|
| 82 | }
|
|---|
| 83 | var _default = exports.default = addAbsolutePathKeyword; |
|---|