0
0
NestJSframework~20 mins

ValidationPipe setup in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ValidationPipe Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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}` };
  }
}
AThe server ignores validation and processes the request normally.
BThe server responds with HTTP 400 and a detailed error message about validation failure.
CThe server crashes with an unhandled exception.
DThe server responds with HTTP 500 Internal Server Error.
Attempts:
2 left
💡 Hint
Think about what ValidationPipe does when input does not meet the rules.
📝 Syntax
intermediate
1: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?
Anew ValidationPipe({ whitelist: true })
Bnew ValidationPipe({ removeExtra: true })
Cnew ValidationPipe({ stripUnknown: true })
Dnew ValidationPipe({ filter: true })
Attempts:
2 left
💡 Hint
Check the official option name for removing extra properties.
state_output
advanced
2: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;
  }
}
A"undefined"
B"string"
C"number"
D"object"
Attempts:
2 left
💡 Hint
Transform converts plain JSON to class instance.
🔧 Debug
advanced
2: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;
}
ABecause nested objects are ignored unless transform is false.
BBecause ValidationPipe only validates primitive types by default.
CBecause ValidationPipe requires the nested property to be decorated with @ValidateNested and Type() decorators.
DBecause ValidationPipe does not support nested validation at all.
Attempts:
2 left
💡 Hint
Check how class-validator handles nested validation.
🧠 Conceptual
expert
2: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?
AThe request is rejected with HTTP 400 and an error listing the extra properties.
BThe extra properties are silently removed and the request proceeds.
CThe server logs a warning but processes the request normally.
DThe extra properties are added to the DTO instance.
Attempts:
2 left
💡 Hint
Think about how forbidNonWhitelisted changes whitelist behavior.