Challenge - 5 Problems
ValidationPipe Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when ValidationPipe rejects invalid input?
Consider a NestJS controller using ValidationPipe globally. What is the response behavior when a request body fails validation?
NestJS
import { Controller, Post, Body, UsePipes, ValidationPipe } from '@nestjs/common'; import { IsInt, Min } from 'class-validator'; class AgeDto { @IsInt() @Min(18) age: number; } @Controller('users') export class UserController { @Post('age') @UsePipes(new ValidationPipe()) setAge(@Body() ageDto: AgeDto) { return { message: `Age set to ${ageDto.age}` }; } }
Attempts:
2 left
💡 Hint
Think about what ValidationPipe does when input does not meet the rules.
✗ Incorrect
ValidationPipe automatically checks the input against the rules defined in the DTO. If validation fails, it sends a 400 Bad Request with details.
📝 Syntax
intermediate1:30remaining
Which option correctly enables whitelist in ValidationPipe?
You want ValidationPipe to strip out any properties not defined in the DTO. Which code snippet correctly sets this up?
Attempts:
2 left
💡 Hint
Check the official option name for removing extra properties.
✗ Incorrect
The whitelist option removes properties not in the DTO. Other options are invalid.
❓ state_output
advanced2:00remaining
What is the output when transform is enabled in ValidationPipe?
Given this DTO and controller, what is the type of the parameter inside the method when ValidationPipe has transform enabled?
NestJS
import { Controller, Post, Body, UsePipes, ValidationPipe } from '@nestjs/common'; import { IsString } from 'class-validator'; class NameDto { @IsString() name: string; } @Controller('hello') export class HelloController { @Post('name') @UsePipes(new ValidationPipe({ transform: true })) greet(@Body() nameDto: NameDto) { return typeof nameDto; } }
Attempts:
2 left
💡 Hint
Transform converts plain JSON to class instance.
✗ Incorrect
With transform: true, the input is converted to an instance of the DTO class, so typeof returns "object".
🔧 Debug
advanced2:30remaining
Why does ValidationPipe not validate nested objects by default?
Given this DTO with a nested object, why does ValidationPipe not validate the nested properties without extra setup?
NestJS
import { IsString, ValidateNested } from 'class-validator'; import { Type } from 'class-transformer'; class AddressDto { @IsString() street: string; } class UserDto { @IsString() name: string; @ValidateNested() @Type(() => AddressDto) address: AddressDto; }
Attempts:
2 left
💡 Hint
Check how class-validator handles nested validation.
✗ Incorrect
To validate nested objects, you must use @ValidateNested() and @Type(() => ClassName) decorators on the nested property.
🧠 Conceptual
expert2:00remaining
What is the effect of setting forbidNonWhitelisted to true in ValidationPipe?
If you configure ValidationPipe with
whitelist: true and forbidNonWhitelisted: true, what happens when the request body contains extra properties not in the DTO?Attempts:
2 left
💡 Hint
Think about how forbidNonWhitelisted changes whitelist behavior.
✗ Incorrect
Setting forbidNonWhitelisted: true causes ValidationPipe to reject requests with unknown properties instead of silently removing them.