forked from typestack/class-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation-error.spec.ts
More file actions
90 lines (80 loc) · 4.05 KB
/
Copy pathvalidation-error.spec.ts
File metadata and controls
90 lines (80 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { IsString, IsUrl, IsOptional, ValidateNested, MinLength } from '../../src/decorator/decorators';
import { Validator } from '../../src/validation/Validator';
const validator = new Validator();
/**
* TODO: needs to split these test into
* - testing basic toString
* - testing nested objects
* - testing arrays
* - testing color codes?
*/
describe('ValidationError', () => {
it('should correctly log error message without ANSI escape codes', async () => {
class NestedClass {
@IsString()
public name: string;
@IsUrl()
public url: string;
@IsOptional()
@ValidateNested()
public insideNested: NestedClass;
constructor(url: string, name: any, insideNested?: NestedClass) {
this.url = url;
this.name = name;
this.insideNested = insideNested;
}
}
class RootClass {
@IsString()
@MinLength(15)
public title: string;
@ValidateNested()
public nestedObj: NestedClass;
@ValidateNested({ each: true })
public nestedArr: NestedClass[];
constructor() {
this.title = 5 as any;
this.nestedObj = new NestedClass('invalid-url', 5, new NestedClass('invalid-url', 5));
this.nestedArr = [new NestedClass('invalid-url', 5), new NestedClass('invalid-url', 5)];
}
}
const validationErrors = await validator.validate(new RootClass());
expect(validationErrors).toHaveLength(3);
expect(validationErrors[0].toString()).toEqual(
'An instance of RootClass has failed the validation:\n' +
' - property title has failed the following constraints: minLength, isString \n'
);
expect(validationErrors[1].toString()).toEqual(
'An instance of RootClass has failed the validation:\n' +
' - property nestedObj.name has failed the following constraints: isString \n' +
' - property nestedObj.url has failed the following constraints: isUrl \n' +
' - property nestedObj.insideNested.name has failed the following constraints: isString \n' +
' - property nestedObj.insideNested.url has failed the following constraints: isUrl \n'
);
expect(validationErrors[2].toString()).toEqual(
'An instance of RootClass has failed the validation:\n' +
' - property nestedArr[0].name has failed the following constraints: isString \n' +
' - property nestedArr[0].url has failed the following constraints: isUrl \n' +
' - property nestedArr[1].name has failed the following constraints: isString \n' +
' - property nestedArr[1].url has failed the following constraints: isUrl \n'
);
expect(validationErrors[0].toString(undefined, undefined, undefined, true)).toEqual(
'An instance of RootClass has failed the validation:\n' +
' - property title has failed the following constraints: title must be longer than or equal to 15 characters, title must be a string \n'
);
expect(validationErrors[1].toString(undefined, undefined, undefined, true)).toEqual(
'An instance of RootClass has failed the validation:\n' +
' - property nestedObj.name has failed the following constraints: name must be a string \n' +
' - property nestedObj.url has failed the following constraints: url must be a URL address \n' +
' - property nestedObj.insideNested.name has failed the following constraints: name must be a string \n' +
' - property nestedObj.insideNested.url has failed the following constraints: url must be a URL address \n'
);
expect(validationErrors[2].toString(undefined, undefined, undefined, true)).toEqual(
'An instance of RootClass has failed the validation:\n' +
' - property nestedArr[0].name has failed the following constraints: name must be a string \n' +
' - property nestedArr[0].url has failed the following constraints: url must be a URL address \n' +
' - property nestedArr[1].name has failed the following constraints: name must be a string \n' +
' - property nestedArr[1].url has failed the following constraints: url must be a URL address \n'
);
});
});