|
1 |
| -import { IsNotEmpty, Length, Min, validate } from 'class-validator'; |
| 1 | +import { |
| 2 | + IsEmail, |
| 3 | + IsNotEmpty, |
| 4 | + Length, |
| 5 | + Min, |
| 6 | + MinLength, |
| 7 | + validate, |
| 8 | + ValidateNested, |
| 9 | +} from 'class-validator'; |
2 | 10 | import { stripIndents } from 'common-tags';
|
3 | 11 | import expect from 'expect';
|
4 | 12 |
|
5 |
| -import { classValidatorFlatFormatter } from '.'; |
| 13 | +import { validationErrorsAsArray, validationErrorsAsString } from '.'; |
6 | 14 | import { ValidationError } from './validation-error';
|
7 | 15 |
|
8 |
| -describe('classValidatorFlatFormatter', () => { |
9 |
| - it('built in to string', async () => { |
10 |
| - class User { |
11 |
| - @Length(3, 30) name!: string; |
12 |
| - @IsNotEmpty() @Min(18) age!: number; |
13 |
| - } |
14 |
| - const user = new User(); |
15 |
| - const result = await validate(user); |
16 |
| - expect(result).toBeTruthy(); |
17 |
| - }); |
| 16 | +it('built in to string', async () => { |
| 17 | + class User { |
| 18 | + @Length(3, 30) name!: string; |
| 19 | + @IsNotEmpty() @Min(18) age!: number; |
| 20 | + } |
| 21 | + const user = new User(); |
| 22 | + const errors = await validate(user); |
| 23 | + expect(errors).toBeTruthy(); |
| 24 | +}); |
18 | 25 |
|
19 |
| - it('single error object', () => { |
20 |
| - const error: ValidationError = { |
21 |
| - value: 'aa@mai1l1', |
22 |
| - property: 'email', |
23 |
| - children: [], |
24 |
| - constraints: { |
25 |
| - isEmail: 'email must be an email', |
26 |
| - } as Record<string, string>, |
27 |
| - }; |
28 |
| - expect(classValidatorFlatFormatter([error])).toEqual(stripIndents` |
29 |
| - email: email must be an email (isEmail). |
30 |
| - `); |
31 |
| - }); |
| 26 | +it('single error object', async () => { |
| 27 | + class User { |
| 28 | + @IsEmail() email = ''; |
| 29 | + } |
| 30 | + |
| 31 | + const user = new User(); |
| 32 | + const errors = await validate(user); |
| 33 | + |
| 34 | + expect(validationErrorsAsString(errors)).toEqual( |
| 35 | + 'email: email must be an email (isEmail).', |
| 36 | + ); |
| 37 | +}); |
| 38 | + |
| 39 | +it('fault tollerance', () => { |
| 40 | + const errors: Partial<ValidationError>[] = [{}]; |
| 41 | + expect(validationErrorsAsString(errors as ValidationError[])).toEqual(''); |
| 42 | + expect(validationErrorsAsString(undefined as any)).toEqual(''); |
| 43 | + expect(validationErrorsAsString(null as any)).toEqual(''); |
| 44 | +}); |
| 45 | + |
| 46 | +it('several errors with single constraint', async () => { |
| 47 | + class User { |
| 48 | + @MinLength(3) name = ''; |
| 49 | + @IsNotEmpty() password = ''; |
| 50 | + } |
32 | 51 |
|
33 |
| - it('several errors with single constraint', () => { |
34 |
| - const errors: Partial<ValidationError>[] = [ |
35 |
| - { |
36 |
| - value: '', |
37 |
| - property: 'name', |
38 |
| - children: [], |
39 |
| - constraints: { |
40 |
| - minLength: 'name must be longer than or equal to 3 characters', |
41 |
| - }, |
42 |
| - }, |
43 |
| - { |
44 |
| - value: '', |
45 |
| - property: 'password', |
46 |
| - children: [], |
47 |
| - constraints: { |
48 |
| - isNotEmpty: 'password should not be empty', |
49 |
| - }, |
50 |
| - }, |
51 |
| - ]; |
52 |
| - expect(classValidatorFlatFormatter(errors as ValidationError[])) |
53 |
| - .toEqual(stripIndents` |
| 52 | + const user = new User(); |
| 53 | + const errors = await validate(user); |
| 54 | + |
| 55 | + expect(validationErrorsAsString(errors)).toEqual(stripIndents` |
54 | 56 | name: name must be longer than or equal to 3 characters (minLength),
|
55 | 57 | password: password should not be empty (isNotEmpty).
|
56 | 58 | `);
|
57 |
| - }); |
| 59 | +}); |
58 | 60 |
|
59 |
| - it('single error with multiple constraints', () => { |
60 |
| - const errors: Partial<ValidationError>[] = [ |
61 |
| - { |
62 |
| - value: '', |
63 |
| - property: 'name', |
64 |
| - children: [], |
65 |
| - constraints: { |
66 |
| - minLength: 'minLength error message', |
67 |
| - isNotEmpty: 'name should not be empty', |
68 |
| - }, |
69 |
| - }, |
70 |
| - ]; |
71 |
| - expect(classValidatorFlatFormatter(errors as ValidationError[])) |
72 |
| - .toEqual(stripIndents` |
73 |
| - name: minLength error message (minLength), |
74 |
| - name: name should not be empty (isNotEmpty). |
75 |
| - `); |
76 |
| - }); |
| 61 | +it('validationErrorsAsArray', async () => { |
| 62 | + class EmailObject { |
| 63 | + @IsEmail() |
| 64 | + value = ''; |
| 65 | + } |
| 66 | + class User { |
| 67 | + @Length(3, 30) name = ''; |
| 68 | + @Min(18) age = 0; |
| 69 | + @ValidateNested() email = new EmailObject(); |
| 70 | + } |
| 71 | + const user = new User(); |
| 72 | + const errors = await validate(user); |
77 | 73 |
|
78 |
| - it('single with nested', () => { |
79 |
| - const errors: Partial<ValidationError>[] = [ |
80 |
| - { |
81 |
| - property: 'user', |
82 |
| - children: [ |
83 |
| - ({ |
84 |
| - property: 'name', |
85 |
| - constraints: { alnum: 'alnum error' }, |
86 |
| - } as unknown) as ValidationError, |
87 |
| - ], |
88 |
| - constraints: { |
89 |
| - isNotEmpty: 'should not be empty', |
90 |
| - }, |
91 |
| - }, |
92 |
| - ]; |
93 |
| - expect(classValidatorFlatFormatter(errors as ValidationError[])) |
94 |
| - .toEqual(stripIndents` |
95 |
| - user: should not be empty (isNotEmpty), |
96 |
| - user.name: alnum error (alnum). |
97 |
| - `); |
98 |
| - }); |
| 74 | + const result = validationErrorsAsArray(errors); |
| 75 | + |
| 76 | + expect(result).toBeInstanceOf(Array); |
| 77 | + expect(result).toHaveLength(3); |
| 78 | + expect(result).toContainEqual('email.value: value must be an email (isEmail)'); |
| 79 | +}); |
| 80 | + |
| 81 | +it('coerce to array', async () => { |
| 82 | + class User { |
| 83 | + @IsEmail() email = ''; |
| 84 | + } |
| 85 | + |
| 86 | + const user = new User(); |
| 87 | + const errors = await validate(user); |
99 | 88 |
|
100 |
| - it('fault tollerance', () => { |
101 |
| - const errors: Partial<ValidationError>[] = [{}]; |
102 |
| - expect(classValidatorFlatFormatter(errors as ValidationError[])).toEqual(''); |
103 |
| - expect(classValidatorFlatFormatter(undefined as any)).toEqual(''); |
104 |
| - expect(classValidatorFlatFormatter(null as any)).toEqual(''); |
105 |
| - }); |
| 89 | + expect(validationErrorsAsArray(errors[0] as ValidationError)).toEqual([ |
| 90 | + 'email: email must be an email (isEmail)', |
| 91 | + ]); |
106 | 92 | });
|
0 commit comments