You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB

4 years ago
  1. "use strict";
  2. const errorMessage = (schema, data, message) => ({
  3. keyword: "absolutePath",
  4. params: { absolutePath: data },
  5. message: message,
  6. parentSchema: schema
  7. });
  8. const getErrorFor = (shouldBeAbsolute, data, schema) => {
  9. const message = shouldBeAbsolute
  10. ? `The provided value ${JSON.stringify(data)} is not an absolute path!`
  11. : `A relative path is expected. However, the provided value ${JSON.stringify(
  12. data
  13. )} is an absolute path!`;
  14. return errorMessage(schema, data, message);
  15. };
  16. module.exports = ajv =>
  17. ajv.addKeyword("absolutePath", {
  18. errors: true,
  19. type: "string",
  20. compile(expected, schema) {
  21. function callback(data) {
  22. let passes = true;
  23. const isExclamationMarkPresent = data.includes("!");
  24. const isCorrectAbsoluteOrRelativePath =
  25. expected === /^(?:[A-Za-z]:\\|\/)/.test(data);
  26. if (isExclamationMarkPresent) {
  27. callback.errors = [
  28. errorMessage(
  29. schema,
  30. data,
  31. `The provided value ${JSON.stringify(
  32. data
  33. )} contains exclamation mark (!) which is not allowed because it's reserved for loader syntax.`
  34. )
  35. ];
  36. passes = false;
  37. }
  38. if (!isCorrectAbsoluteOrRelativePath) {
  39. callback.errors = [getErrorFor(expected, data, schema)];
  40. passes = false;
  41. }
  42. return passes;
  43. }
  44. callback.errors = [];
  45. return callback;
  46. }
  47. });