Consider a NestJS app where ValidationPipe is applied globally with transform: true. What is the output type of a controller method receiving a DTO?
app.useGlobalPipes(new ValidationPipe({ transform: true }));
@Post()
create(@Body() createDto: CreateDto) {
return typeof createDto;
}Think about what transform: true does to the incoming data.
With transform: true, the incoming JSON is converted to an instance of the DTO class, so typeof createDto is "object".
Choose the correct way to configure ValidationPipe to remove properties not in the DTO and throw an error if extra properties are present.
Whitelist removes extra properties. ForbidNonWhitelisted throws error on extra properties.
Setting whitelist: true removes extra properties. Setting forbidNonWhitelisted: true makes the pipe throw an error if extra properties are found.
Given a DTO with a nested object property, why does ValidationPipe skip validating the nested object?
Example DTO:
class AddressDto {
@IsString()
street: string;
}
class UserDto {
@ValidateNested()
@Type(() => AddressDto)
address: AddressDto;
}Think about how nested objects are converted before validation.
ValidationPipe requires transform: true to convert nested plain objects into class instances so that nested validation decorators work properly.
When ValidationPipe is configured with forbidNonWhitelisted: true and the request body contains extra properties not in the DTO, what is the typical error message returned?
Look for the exact wording from class-validator errors.
The error message from class-validator when forbidding non-whitelisted properties is typically: "property 'extra' should not exist".
When using custom async validators in DTOs, how does ValidationPipe handle the validation process?
Think about how async validation integrates with the validation lifecycle.
ValidationPipe supports async validators and waits for all promises to resolve before returning validation results.