ValidationUtils.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ValidationUtils = exports.constraintToString = void 0;
  4. /**
  5. * Convert the constraint to a string to be shown in an error
  6. */
  7. function constraintToString(constraint) {
  8. if (Array.isArray(constraint)) {
  9. return constraint.join(', ');
  10. }
  11. if (typeof constraint === 'symbol') {
  12. constraint = constraint.description;
  13. }
  14. return `${constraint}`;
  15. }
  16. exports.constraintToString = constraintToString;
  17. class ValidationUtils {
  18. static replaceMessageSpecialTokens(message, validationArguments) {
  19. let messageString;
  20. if (message instanceof Function) {
  21. messageString = message(validationArguments);
  22. }
  23. else if (typeof message === 'string') {
  24. messageString = message;
  25. }
  26. if (messageString && Array.isArray(validationArguments.constraints)) {
  27. validationArguments.constraints.forEach((constraint, index) => {
  28. messageString = messageString.replace(new RegExp(`\\$constraint${index + 1}`, 'g'), constraintToString(constraint));
  29. });
  30. }
  31. if (messageString &&
  32. validationArguments.value !== undefined &&
  33. validationArguments.value !== null &&
  34. typeof validationArguments.value === 'string')
  35. messageString = messageString.replace(/\$value/g, validationArguments.value);
  36. if (messageString)
  37. messageString = messageString.replace(/\$property/g, validationArguments.property);
  38. if (messageString)
  39. messageString = messageString.replace(/\$target/g, validationArguments.targetName);
  40. return messageString;
  41. }
  42. }
  43. exports.ValidationUtils = ValidationUtils;
  44. //# sourceMappingURL=ValidationUtils.js.map