absolutePath.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. /** @typedef {import("ajv").default} Ajv */
  7. /** @typedef {import("ajv").SchemaValidateFunction} SchemaValidateFunction */
  8. /** @typedef {import("ajv").AnySchemaObject} AnySchemaObject */
  9. /** @typedef {import("../validate").SchemaUtilErrorObject} SchemaUtilErrorObject */
  10. /**
  11. * @param {string} message
  12. * @param {object} schema
  13. * @param {string} data
  14. * @returns {SchemaUtilErrorObject}
  15. */
  16. function errorMessage(message, schema, data) {
  17. return {
  18. // @ts-ignore
  19. // eslint-disable-next-line no-undefined
  20. dataPath: undefined,
  21. // @ts-ignore
  22. // eslint-disable-next-line no-undefined
  23. schemaPath: undefined,
  24. keyword: "absolutePath",
  25. params: {
  26. absolutePath: data
  27. },
  28. message,
  29. parentSchema: schema
  30. };
  31. }
  32. /**
  33. * @param {boolean} shouldBeAbsolute
  34. * @param {object} schema
  35. * @param {string} data
  36. * @returns {SchemaUtilErrorObject}
  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
  45. * @returns {Ajv}
  46. */
  47. function addAbsolutePathKeyword(ajv) {
  48. ajv.addKeyword({
  49. keyword: "absolutePath",
  50. type: "string",
  51. errors: true,
  52. /**
  53. * @param {boolean} schema
  54. * @param {AnySchemaObject} parentSchema
  55. * @returns {SchemaValidateFunction}
  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. } // ?:[A-Za-z]:\\ - Windows absolute path
  66. // \\\\ - Windows network absolute path
  67. // \/ - Unix-like OS absolute path
  68. const isCorrectAbsolutePath = schema === /^(?:[A-Za-z]:(\\|\/)|\\\\|\/)/.test(data);
  69. if (!isCorrectAbsolutePath) {
  70. callback.errors = [getErrorFor(schema, parentSchema, data)];
  71. passes = false;
  72. }
  73. return passes;
  74. };
  75. callback.errors = [];
  76. return callback;
  77. }
  78. });
  79. return ajv;
  80. }
  81. var _default = addAbsolutePathKeyword;
  82. exports.default = _default;