Skip to content

Commit 0e84a27

Browse files
test: update group tests to pass validation with forbidUnknownValues enabled
1 parent 607ef61 commit 0e84a27

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

test/functional/validation-options.spec.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
ValidatorConstraint,
1010
IsOptional,
1111
IsNotEmpty,
12+
Allow,
1213
} from '../../src/decorator/decorators';
1314
import { Validator } from '../../src/validation/Validator';
1415
import {
@@ -937,36 +938,45 @@ describe('groups', () => {
937938
});
938939

939940
describe('strictGroups', function () {
940-
class MyClass {
941-
@Contains('hello', {
942-
groups: ['A'],
943-
})
941+
class MyPayload {
942+
/**
943+
* Since forbidUnknownValues defaults to true, we must add a property to
944+
* register the class in the metadata storage. Otherwise the unknown value check
945+
* would take priority (first check) and exit without running the grouping logic.
946+
*
947+
* To solve this we register this property with always: true, so at least a single
948+
* metadata is returned for each validation preventing the unknown value was passed error.
949+
*/
950+
@IsOptional({ always: true })
951+
propertyToRegisterClass: string;
952+
953+
@Contains('hello', { groups: ['A'] })
944954
title: string;
945955
}
946956

947-
const model1 = new MyClass();
957+
const instance = new MyPayload();
948958

949959
it('should ignore decorators with groups if validating without groups', function () {
950-
return validator.validate(model1, { strictGroups: true }).then(errors => {
960+
return validator.validate(instance, { strictGroups: true }).then(errors => {
951961
expect(errors).toHaveLength(0);
952962
});
953963
});
954964

955965
it('should ignore decorators with groups if validating with empty groups array', function () {
956-
return validator.validate(model1, { strictGroups: true, groups: [] }).then(errors => {
966+
return validator.validate(instance, { strictGroups: true, groups: [] }).then(errors => {
957967
expect(errors).toHaveLength(0);
958968
});
959969
});
960970

961971
it('should include decorators with groups if validating with matching groups', function () {
962-
return validator.validate(model1, { strictGroups: true, groups: ['A'] }).then(errors => {
972+
return validator.validate(instance, { strictGroups: true, groups: ['A'] }).then(errors => {
963973
expect(errors).toHaveLength(1);
964974
expectTitleContains(errors[0]);
965975
});
966976
});
967977

968978
it('should not include decorators with groups if validating with different groups', function () {
969-
return validator.validate(model1, { strictGroups: true, groups: ['B'] }).then(errors => {
979+
return validator.validate(instance, { strictGroups: true, groups: ['B'] }).then(errors => {
970980
expect(errors).toHaveLength(0);
971981
});
972982
});

test/functional/whitelist-validation.spec.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Allow, IsDefined, Min } from '../../src/decorator/decorators';
1+
import { Allow, IsDefined, IsOptional, Min } from '../../src/decorator/decorators';
22
import { Validator } from '../../src/validation/Validator';
33
import { ValidationTypes } from '../../src';
44

@@ -46,18 +46,30 @@ describe('whitelist validation', () => {
4646
});
4747

4848
it('should throw an error when forbidNonWhitelisted flag is set', () => {
49-
class MyClass {}
49+
class MyPayload {
50+
/**
51+
* Since forbidUnknownValues defaults to true, we must add a property to
52+
* register the class in the metadata storage. Otherwise the unknown value check
53+
* would take priority (first check) and exit without running the whitelist logic.
54+
*/
55+
@IsOptional()
56+
propertyToRegisterClass: string;
5057

51-
const model: any = new MyClass();
58+
nonDecorated: string;
5259

53-
model.unallowedProperty = 'non-whitelisted';
60+
constructor(nonDecorated: string) {
61+
this.nonDecorated = nonDecorated;
62+
}
63+
}
64+
65+
const instance = new MyPayload('non-whitelisted');
5466

55-
return validator.validate(model, { whitelist: true, forbidNonWhitelisted: true }).then(errors => {
67+
return validator.validate(instance, { whitelist: true, forbidNonWhitelisted: true }).then(errors => {
5668
expect(errors.length).toEqual(1);
57-
expect(errors[0].target).toEqual(model);
58-
expect(errors[0].property).toEqual('unallowedProperty');
69+
expect(errors[0].target).toEqual(instance);
70+
expect(errors[0].property).toEqual('nonDecorated');
5971
expect(errors[0].constraints).toHaveProperty(ValidationTypes.WHITELIST);
60-
expect(() => errors[0].toString()).not.toThrowError();
72+
expect(() => errors[0].toString()).not.toThrow();
6173
});
6274
});
6375
});

0 commit comments

Comments
 (0)