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