ValidationUtils.js 1.7 KB

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