ValidationErrorsFormatter.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { assert } from 'chai';
  2. import { ValidationError } from 'class-validator';
  3. import { ValidationErrorsFormatter } from '../../../src/options/ValidationErrorsFormatter';
  4. describe('ValidationErrorsFormatter', () => {
  5. describe('format (validationErrors: ValidationError[]): string', () => {
  6. describe('Variant #1: one constraint group with one constraint', () => {
  7. const constraintGroupRegExp: RegExp = /`foo` *errors:/;
  8. const constraintRegExp: RegExp = /(?: *-)+ *constraint *text/;
  9. const validationErrors: ValidationError[] = [{
  10. target: {},
  11. property: 'foo',
  12. value: null,
  13. constraints: {
  14. 'constraint1': '- constraint text'
  15. },
  16. children: []
  17. }];
  18. let validationError: string;
  19. before(() => {
  20. validationError = ValidationErrorsFormatter.format(validationErrors);
  21. });
  22. it('match #1: should return valid validation errors', () => {
  23. assert.match(validationError, constraintGroupRegExp);
  24. });
  25. it('match #2: should return valid validation errors', () => {
  26. assert.match(validationError, constraintRegExp);
  27. });
  28. });
  29. describe('Variant #2: one constraint group with two constraint', () => {
  30. const constraintGroupRegExp: RegExp = /`foo` *errors:/;
  31. const constraintRegExp1: RegExp = /(?: *-)+ constraint *text *#1/;
  32. const constraintRegExp2: RegExp = /(?: *-)+ constraint *text *#2/;
  33. const validationErrors: ValidationError[] = [{
  34. target: {},
  35. property: 'foo',
  36. value: null,
  37. constraints: {
  38. 'constraint1': '- constraint text #1',
  39. 'constraint2': '- constraint text #2'
  40. },
  41. children: []
  42. }];
  43. let validationError: string;
  44. before(() => {
  45. validationError = ValidationErrorsFormatter.format(validationErrors);
  46. });
  47. it('match #1: should return valid validation errors', () => {
  48. assert.match(validationError, constraintGroupRegExp);
  49. });
  50. it('match #2: should return valid validation errors', () => {
  51. assert.match(validationError, constraintRegExp1);
  52. });
  53. it('match #3: should return valid validation errors', () => {
  54. assert.match(validationError, constraintRegExp2);
  55. });
  56. });
  57. describe('Variant #3: two constraint groups', () => {
  58. const regExpMatch: string = `` +
  59. `\`foo\` *errors:\\n` +
  60. `(?: *-)+ *constraint *group *#1 *text\\n+` +
  61. `\`bar\` *errors:\\n` +
  62. `(?: *-)+ *constraint *group *#2 *text\\n+` +
  63. ``;
  64. const regExp: RegExp = new RegExp(regExpMatch);
  65. const validationErrors: ValidationError[] = [{
  66. target: {},
  67. property: 'foo',
  68. value: null,
  69. constraints: {
  70. 'constraint': '- constraint group #1 text'
  71. },
  72. children: []
  73. }, {
  74. target: {},
  75. property: 'bar',
  76. value: null,
  77. constraints: {
  78. 'constraint': '- constraint group #2 text'
  79. },
  80. children: []
  81. }];
  82. let validationError: string;
  83. before(() => {
  84. validationError = ValidationErrorsFormatter.format(validationErrors);
  85. });
  86. it('should return valid validation errors', () => {
  87. assert.match(validationError, regExp);
  88. });
  89. });
  90. });
  91. });