Skip to content

Commit 962635e

Browse files
author
Pedro Costa
committed
test(nested): cover whitelist+forbidNonWhitelisted with ValidateNested on arrays (refs #2089)
1 parent fc64303 commit 962635e

File tree

2 files changed

+74
-31
lines changed

2 files changed

+74
-31
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
"prettier:check": "prettier --check \"**/*.{ts,md}\"",
3232
"lint:fix": "eslint --max-warnings 0 --fix --ext .ts src/",
3333
"lint:check": "eslint --max-warnings 0 --ext .ts src/",
34-
"test": "jest --coverage --verbose",
35-
"test:watch": "jest --watch",
34+
"test": "jest --maxWorkers=50% --coverage --verbose",
35+
"test:watch": "jest --maxWorkers=50% --watch",
3636
"test:ci": "jest --runInBand --no-cache --coverage --verbose"
3737
},
3838
"dependencies": {
Lines changed: 72 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,94 @@
1-
import { IsInt, IsOptional, IsString, ValidateNested } from '../../src/decorator/decorators';
1+
import {
2+
IsArray,
3+
IsInt,
4+
IsNotEmpty,
5+
IsOptional,
6+
IsString,
7+
ValidateNested,
8+
ArrayMinSize,
9+
} from '../../src/decorator/decorators';
210
import { Validator } from '../../src/validation/Validator';
311
import { ValidationTypes } from '../../src/validation/ValidationTypes';
12+
import { ValidationError } from '../../src/validation/ValidationError';
413

514
const validator = new Validator();
615

716
describe('nested validation with whitelist on arrays', () => {
817
it('should not flag decorated child properties as non-whitelisted when using ValidateNested on array', () => {
9-
class VehicleDump {
18+
class PolVehicleDump {
1019
@IsString()
20+
@IsNotEmpty()
1121
vin: string;
1222

1323
@IsInt()
1424
@IsOptional()
15-
year?: number;
25+
year: number;
1626

1727
@IsString()
1828
@IsOptional()
1929
make?: string;
30+
31+
@IsString()
32+
@IsOptional()
33+
model?: string;
34+
35+
@IsInt()
36+
@IsOptional()
37+
vehicleTypeCode?: number;
2038
}
2139

22-
class Parent {
23-
@ValidateNested({ each: true })
24-
vehicleDumps: VehicleDump[];
40+
class PolDump {
41+
@IsInt({ always: true })
42+
@IsNotEmpty({ always: true })
43+
versionNum: number;
44+
45+
@IsString({ always: true })
46+
@IsNotEmpty({ always: true })
47+
polNumber: string;
48+
49+
@IsString({ always: true })
50+
@IsNotEmpty({ always: true })
51+
effectiveDate: string;
52+
53+
@IsString({ always: true })
54+
@IsNotEmpty({ always: true })
55+
expirationDate: string;
56+
57+
@IsArray({ always: true })
58+
@ArrayMinSize(0, { always: true })
59+
@IsOptional({ always: true })
60+
@ValidateNested({ always: true, each: true })
61+
polVehicleDumps?: PolVehicleDump[];
2562
}
2663

27-
const parent = new Parent();
28-
parent.vehicleDumps = [
29-
{
30-
vin: 'XXXXXXX',
31-
year: 2005,
32-
make: 'FREIGHTLINER',
33-
},
34-
];
35-
36-
return validator
37-
.validate(parent, { whitelist: true, forbidNonWhitelisted: true })
38-
.then(errors => {
39-
// Should not report whitelist errors for child properties like "vin" or "year"
40-
// i.e., no constraint of type ValidationTypes.WHITELIST anywhere in the tree
41-
const stringify = (e: any): string => JSON.stringify(e);
42-
const flat = (errs: any[]): any[] =>
43-
errs.flatMap(e => [e, ...(e.children ? flat(e.children) : [])]);
44-
const all = flat(errors);
45-
const hasWhitelist = all.some(e => e.constraints && e.constraints[ValidationTypes.WHITELIST]);
46-
expect(hasWhitelist).toBe(false);
47-
});
48-
});
49-
});
64+
class PolDumpReq {
65+
@IsArray({ always: true })
66+
@ArrayMinSize(1, { always: true })
67+
@ValidateNested({ always: true, each: true })
68+
polDumps: PolDump[];
69+
}
5070

71+
const vehicle = new PolVehicleDump();
72+
vehicle.vin = 'XXXXXXX';
73+
vehicle.year = 2005;
74+
vehicle.make = 'FREIGHTLINER';
5175

76+
const dump = new PolDump();
77+
dump.versionNum = 1;
78+
dump.polNumber = '123123';
79+
dump.effectiveDate = '2020-12-10';
80+
dump.expirationDate = '2021-12-10';
81+
dump.polVehicleDumps = [vehicle];
82+
83+
const polDumpReq = new PolDumpReq();
84+
polDumpReq.polDumps = [dump];
85+
86+
return validator.validate(polDumpReq, { whitelist: true, forbidNonWhitelisted: true }).then(errors => {
87+
const flat = (errs: ReadonlyArray<ValidationError>): ValidationError[] =>
88+
errs.flatMap((e: ValidationError) => [e, ...(e.children ? flat(e.children) : [])]);
89+
const all = flat(errors);
90+
const hasWhitelist = all.some(e => e.constraints && e.constraints[ValidationTypes.WHITELIST]);
91+
expect(hasWhitelist).toBe(false);
92+
});
93+
});
94+
});

0 commit comments

Comments
 (0)