-
Notifications
You must be signed in to change notification settings - Fork 820
/
Copy pathsync-validation.spec.ts
70 lines (61 loc) · 2.43 KB
/
sync-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
import { Validator } from '../../src/validation/Validator';
import { ValidationArguments } from '../../src/validation/ValidationArguments';
import { registerDecorator } from '../../src/register-decorator';
import { ValidationOptions } from '../../src/decorator/ValidationOptions';
import { ValidatorConstraint, Validate, IsNotEmpty } from '../../src/decorator/decorators';
import { ValidatorConstraintInterface } from '../../src/validation/ValidatorConstraintInterface';
const validator = new Validator();
describe('sync validation should ignore async validation constraints', () => {
@ValidatorConstraint({ name: 'isShortenThan', async: true })
class IsShortenThanConstraint implements ValidatorConstraintInterface {
validate(value: any, args: ValidationArguments): Promise<boolean> {
return Promise.resolve(false);
}
}
function IsLonger(property: string, validationOptions?: ValidationOptions) {
return function (object: object, propertyName: string): void {
registerDecorator({
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
constraints: [property],
async: true,
name: 'isLonger',
validator: {
validate(value: any, args: ValidationArguments): Promise<boolean> {
return Promise.resolve(false);
},
},
});
};
}
class SecondClass {
@IsLonger('lastName')
firstName: string;
@Validate(IsShortenThanConstraint)
lastName: string;
@IsNotEmpty({ message: 'name should not be empty' })
name: string;
@IsNotEmpty()
alwaysWithValue: string = 'this field always has a value';
}
it('should ignore async validations and validate only sync validation types', () => {
expect.assertions(1);
const model = new SecondClass();
model.firstName = 'such validation may lead';
model.firstName = 'to recursion';
model.name = 'Umed';
const errors = validator.validateSync(model);
expect(errors.length).toEqual(0);
});
it('should ignore async validations and validate only sync validation types', () => {
expect.assertions(2);
const model = new SecondClass();
model.firstName = 'such validation may lead';
model.firstName = 'to recursion';
model.name = '';
const errors = validator.validateSync(model);
expect(errors.length).toEqual(1);
expect(errors[0].constraints).toEqual({ isNotEmpty: 'name should not be empty' });
});
});