Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ajv.absolutePath.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. });