-
Notifications
You must be signed in to change notification settings - Fork 819
/
Copy pathpromise-validation.spec.ts
155 lines (129 loc) · 5.04 KB
/
promise-validation.spec.ts
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { Contains, IsDefined, MinLength, ValidateNested, ValidatePromise } from '../../src/decorator/decorators';
import { Validator } from '../../src/validation/Validator';
import { ValidationTypes } from '../../src/validation/ValidationTypes';
const validator = new Validator();
describe('promise validation', () => {
it('should not validate missing nested objects', () => {
expect.assertions(4);
class MySubClass {
@MinLength(5)
name: string;
}
class MyClass {
@Contains('hello')
title: string;
@ValidatePromise()
@ValidateNested()
@IsDefined()
mySubClass: Promise<MySubClass>;
}
const model: any = new MyClass();
model.title = 'helo';
return validator.validate(model).then(errors => {
expect(errors[1].target).toEqual(model);
expect(errors[1].value).toBeUndefined();
expect(errors[1].property).toEqual('mySubClass');
expect(errors[1].constraints).toEqual({ isDefined: 'mySubClass should not be null or undefined' });
});
});
it('should validate nested objects', () => {
expect.assertions(24);
class MySubClass {
@MinLength(5)
name: string;
}
class MyClass {
@Contains('hello')
title: string;
@ValidatePromise()
@ValidateNested()
mySubClass: Promise<MySubClass>;
@ValidatePromise()
@ValidateNested()
mySubClasses: Promise<MySubClass[]>;
}
const model = new MyClass();
model.title = 'helo world';
const mySubClass = new MySubClass();
mySubClass.name = 'my';
model.mySubClass = Promise.resolve(mySubClass);
const mySubClasses = [new MySubClass(), new MySubClass()];
mySubClasses[0].name = 'my';
mySubClasses[1].name = 'not-short';
model.mySubClasses = Promise.resolve(mySubClasses);
return validator.validate(model).then(errors => {
return Promise.all([model.mySubClass, model.mySubClasses]).then(([modelMySubClass, modelMySubClasses]) => {
expect(errors.length).toEqual(3);
expect(errors[0].target).toEqual(model);
expect(errors[0].property).toEqual('title');
expect(errors[0].constraints).toEqual({ contains: 'title must contain a hello string' });
expect(errors[0].value).toEqual('helo world');
expect(errors[1].target).toEqual(model);
expect(errors[1].property).toEqual('mySubClass');
expect(errors[1].value).toEqual(modelMySubClass);
expect(errors[1].constraints).toBeUndefined();
const subError1 = errors[1].children[0];
expect(subError1.target).toEqual(modelMySubClass);
expect(subError1.property).toEqual('name');
expect(subError1.constraints).toEqual({ minLength: 'name must be longer than or equal to 5 characters' });
expect(subError1.value).toEqual('my');
expect(errors[2].target).toEqual(model);
expect(errors[2].property).toEqual('mySubClasses');
expect(errors[2].value).toEqual(modelMySubClasses);
expect(errors[2].constraints).toBeUndefined();
const subError2 = errors[2].children[0];
expect(subError2.target).toEqual(modelMySubClasses);
expect(subError2.value).toEqual(modelMySubClasses[0]);
expect(subError2.property).toEqual('0');
const subSubError = subError2.children[0];
expect(subSubError.target).toEqual(modelMySubClasses[0]);
expect(subSubError.property).toEqual('name');
expect(subSubError.constraints).toEqual({ minLength: 'name must be longer than or equal to 5 characters' });
expect(subSubError.value).toEqual('my');
});
});
});
it('should validate when nested is not object', () => {
expect.assertions(4);
class MySubClass {
@MinLength(5)
name: string;
}
class MyClass {
@ValidatePromise()
@ValidateNested()
mySubClass: MySubClass;
}
const model = new MyClass();
model.mySubClass = 'invalidnested object' as any;
return validator.validate(model).then(errors => {
expect(errors[0].target).toEqual(model);
expect(errors[0].property).toEqual('mySubClass');
expect(errors[0].children.length).toEqual(0);
expect(errors[0].constraints).toEqual({
[ValidationTypes.NESTED_VALIDATION]: 'nested property mySubClass must be either object or array',
});
});
});
it('should validate array promise', () => {
expect.assertions(5);
class MyClass {
@ValidatePromise()
@MinLength(2)
arrProperty: Promise<string[]>;
}
const model = new MyClass();
model.arrProperty = Promise.resolve(['one']);
return validator.validate(model).then(errors => {
return Promise.all([model.arrProperty]).then(([modelArrProperty]) => {
expect(errors.length).toEqual(1);
expect(errors[0].target).toEqual(model);
expect(errors[0].property).toEqual('arrProperty');
expect(errors[0].constraints).toEqual({
minLength: 'arrProperty must be longer than or equal to 2 characters',
});
expect(errors[0].value).toEqual(modelArrProperty);
});
});
});
});