|
9 | 9 | ValidatorConstraint,
|
10 | 10 | IsOptional,
|
11 | 11 | IsNotEmpty,
|
| 12 | + Allow, |
12 | 13 | } from '../../src/decorator/decorators';
|
13 | 14 | import { Validator } from '../../src/validation/Validator';
|
14 | 15 | import {
|
@@ -937,36 +938,45 @@ describe('groups', () => {
|
937 | 938 | });
|
938 | 939 |
|
939 | 940 | 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'] }) |
944 | 954 | title: string;
|
945 | 955 | }
|
946 | 956 |
|
947 |
| - const model1 = new MyClass(); |
| 957 | + const instance = new MyPayload(); |
948 | 958 |
|
949 | 959 | 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 => { |
951 | 961 | expect(errors).toHaveLength(0);
|
952 | 962 | });
|
953 | 963 | });
|
954 | 964 |
|
955 | 965 | 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 => { |
957 | 967 | expect(errors).toHaveLength(0);
|
958 | 968 | });
|
959 | 969 | });
|
960 | 970 |
|
961 | 971 | 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 => { |
963 | 973 | expect(errors).toHaveLength(1);
|
964 | 974 | expectTitleContains(errors[0]);
|
965 | 975 | });
|
966 | 976 | });
|
967 | 977 |
|
968 | 978 | 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 => { |
970 | 980 | expect(errors).toHaveLength(0);
|
971 | 981 | });
|
972 | 982 | });
|
|
0 commit comments