-
Notifications
You must be signed in to change notification settings - Fork 820
/
Copy pathinherited-validation.spec.ts
37 lines (32 loc) · 1.22 KB
/
inherited-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
import { Contains, MinLength } from '../../src/decorator/decorators';
import { Validator } from '../../src/validation/Validator';
const validator = new Validator();
describe('inherited validation', () => {
it('should validate inherited properties', () => {
expect.assertions(9);
class MyClass {
@Contains('hello')
title: string;
}
class MySubClass extends MyClass {
@MinLength(5)
name: string;
}
const model = new MySubClass();
model.title = 'helo world';
model.name = 'my';
return validator.validate(model).then(errors => {
expect(errors.length).toEqual(2);
// subclass own props are validated first
expect(errors[0].target).toEqual(model);
expect(errors[0].property).toEqual('name');
expect(errors[0].constraints).toEqual({ minLength: 'name must be longer than or equal to 5 characters' });
expect(errors[0].value).toEqual('my');
// parent props are validated afterwards
expect(errors[1].target).toEqual(model);
expect(errors[1].property).toEqual('title');
expect(errors[1].constraints).toEqual({ contains: 'title must contain a hello string' });
expect(errors[1].value).toEqual('helo world');
});
});
});