Performance: Nested DTO validation
This affects server response time and initial API processing speed by adding validation overhead.
class AddressDto { @IsString() street: string; @IsString() city: string; } class UserDto { @IsString() name: string; @ValidateNested({ each: true }) @Type(() => AddressDto) @ArrayMaxSize(5) addresses: AddressDto[]; } // Validation pipe with transform: true and whitelist: true enabled
class AddressDto { @IsString() street: string; @IsString() city: string; } class UserDto { @IsString() name: string; @ValidateNested({ each: true }) @Type(() => AddressDto) addresses: AddressDto[]; } // Validation pipe used globally without whitelist or transform options
| Pattern | CPU Usage | Memory Usage | Response Latency | Verdict |
|---|---|---|---|---|
| Deep nested DTO without limits | High | High | Increased by 50-100ms | [X] Bad |
| Nested DTO with array size limits and transform | Medium | Medium | Reduced by 30-50% | [OK] Good |
@ValidateNested() in a NestJS DTO?address.zipCode is missing when validating UserDto?
class AddressDto {
@IsString()
street: string;
@IsString()
zipCode: string;
}
class UserDto {
@IsString()
name: string;
@ValidateNested()
@Type(() => AddressDto)
address: AddressDto;
}class ProfileDto {
@IsString()
bio: string;
}
class UserDto {
@IsString()
username: string;
@ValidateNested()
profile: ProfileDto;
}
Why will validation NOT work correctly for the nested profile property?CompanyDto with a nested array of EmployeeDto objects. Which is the correct way to validate this nested array properly?
class EmployeeDto {
@IsString()
name: string;
@IsInt()
age: number;
}
class CompanyDto {
@IsString()
companyName: string;
@ValidateNested({ each: true })
@Type(() => EmployeeDto)
employees: EmployeeDto[];
}Pass the test and this pattern moves unknown