Performance: ValidationPipe in depth
ValidationPipe affects the request processing speed by adding validation logic before controller handlers run, impacting interaction responsiveness.
app.useGlobalPipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true }));app.useGlobalPipes(new ValidationPipe({ whitelist: false, forbidNonWhitelisted: false }));| Pattern | CPU Usage | Event Loop Blocking | Validation Depth | Verdict |
|---|---|---|---|---|
| ValidationPipe with whitelist and transform enabled | Low | Minimal | Controlled | [OK] Good |
| ValidationPipe with default settings and no whitelist | High | Moderate | Deep | [X] Bad |
ValidationPipe in a NestJS application?ValidationPipe in NestJS?transform: true option enables automatic conversion of input data types to expected types.whitelist removes unwanted properties, forbidNonWhitelisted throws error on extra props, skipMissingProperties skips validation for missing props.class UserDto {
@IsInt()
age: number;
}
// In controller
@Post()
create(@Body(new ValidationPipe({ transform: true })) user: UserDto) {
return typeof user.age;
}typeof user.age returns "number".@Post()
create(@Body(new ValidationPipe({ whitelist: true })) user: UserDto) {
return user;
}
// UserDto has properties: name (string), age (number)forbidNonWhitelisted: true must be set.Pass the test and this pattern moves unknown