Skip to content

Commit fc64303

Browse files
author
Pedro Costa
committed
test(nested): add whitelist forbidNonWhitelisted array ValidateNested regression test
1 parent 221502c commit fc64303

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { IsInt, IsOptional, IsString, ValidateNested } from '../../src/decorator/decorators';
2+
import { Validator } from '../../src/validation/Validator';
3+
import { ValidationTypes } from '../../src/validation/ValidationTypes';
4+
5+
const validator = new Validator();
6+
7+
describe('nested validation with whitelist on arrays', () => {
8+
it('should not flag decorated child properties as non-whitelisted when using ValidateNested on array', () => {
9+
class VehicleDump {
10+
@IsString()
11+
vin: string;
12+
13+
@IsInt()
14+
@IsOptional()
15+
year?: number;
16+
17+
@IsString()
18+
@IsOptional()
19+
make?: string;
20+
}
21+
22+
class Parent {
23+
@ValidateNested({ each: true })
24+
vehicleDumps: VehicleDump[];
25+
}
26+
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+
});
50+
51+

0 commit comments

Comments
 (0)