forked from typestack/class-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator-options.spec.ts
47 lines (40 loc) · 1.82 KB
/
validator-options.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
import { IsNotEmpty } from '../../src/decorator/decorators';
import { Validator } from '../../src/validation/Validator';
const validator = new Validator();
describe('validator options', () => {
it('should not return target in validation error if validationError: { target: false } is set', () => {
class MyClass {
@IsNotEmpty()
title: string = '';
isActive: boolean;
}
const model = new MyClass();
model.title = '';
return validator
.validate(model, { skipMissingProperties: true, validationError: { target: false } })
.then(errors => {
expect(errors.length).toEqual(1);
expect(errors[0].target).toBeUndefined();
expect(errors[0].property).toEqual('title');
expect(errors[0].constraints).toEqual({ isNotEmpty: 'title should not be empty' });
expect(errors[0].value).toEqual('');
});
});
test.each([[{ forbidUnknownValues: true }], [{}]])('should returns error on unknown objects with %s', options => {
const anonymousObject = { badKey: 'This should not pass.' };
return validator.validate(anonymousObject, { ...options }).then(errors => {
expect(errors.length).toEqual(1);
expect(errors[0].target).toEqual(anonymousObject);
expect(errors[0].property).toEqual(undefined);
expect(errors[0].value).toEqual(undefined);
expect(errors[0].children).toBeInstanceOf(Array);
expect(errors[0].constraints).toEqual({ unknownValue: 'an unknown value was passed to the validate function' });
});
});
it('should return no error on unknown objects if forbidUnknownValues is false', function () {
const anonymousObject = { badKey: 'This should not pass.' };
return validator.validate(anonymousObject, { forbidUnknownValues: false }).then(errors => {
expect(errors.length).toEqual(0);
});
});
});