|
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'; |
2 | 10 | import { Validator } from '../../src/validation/Validator';
|
3 | 11 | import { ValidationTypes } from '../../src/validation/ValidationTypes';
|
| 12 | +import { ValidationError } from '../../src/validation/ValidationError'; |
4 | 13 |
|
5 | 14 | const validator = new Validator();
|
6 | 15 |
|
7 | 16 | describe('nested validation with whitelist on arrays', () => {
|
8 | 17 | it('should not flag decorated child properties as non-whitelisted when using ValidateNested on array', () => {
|
9 |
| - class VehicleDump { |
| 18 | + class PolVehicleDump { |
10 | 19 | @IsString()
|
| 20 | + @IsNotEmpty() |
11 | 21 | vin: string;
|
12 | 22 |
|
13 | 23 | @IsInt()
|
14 | 24 | @IsOptional()
|
15 |
| - year?: number; |
| 25 | + year: number; |
16 | 26 |
|
17 | 27 | @IsString()
|
18 | 28 | @IsOptional()
|
19 | 29 | make?: string;
|
| 30 | + |
| 31 | + @IsString() |
| 32 | + @IsOptional() |
| 33 | + model?: string; |
| 34 | + |
| 35 | + @IsInt() |
| 36 | + @IsOptional() |
| 37 | + vehicleTypeCode?: number; |
20 | 38 | }
|
21 | 39 |
|
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[]; |
25 | 62 | }
|
26 | 63 |
|
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 | + } |
50 | 70 |
|
| 71 | + const vehicle = new PolVehicleDump(); |
| 72 | + vehicle.vin = 'XXXXXXX'; |
| 73 | + vehicle.year = 2005; |
| 74 | + vehicle.make = 'FREIGHTLINER'; |
51 | 75 |
|
| 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