0
0
NestJSframework~20 mins

Whitelist and transform options in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the effect of enabling whitelist in NestJS ValidationPipe?

Consider a NestJS controller using ValidationPipe with whitelist: true. What happens to extra properties sent in the request body that are not defined in the DTO?

NestJS
import { Controller, Post, Body, ValidationPipe } from '@nestjs/common';
import { IsString } from 'class-validator';

class CreateUserDto {
  @IsString()
  name: string;
}

@Controller('users')
export class UsersController {
  @Post()
  create(
    @Body(new ValidationPipe({ whitelist: true })) createUserDto: CreateUserDto
  ) {
    return createUserDto;
  }
}
AExtra properties are removed from the request body before validation and controller logic.
BExtra properties cause the validation to fail with an error.
CExtra properties are kept and passed to the controller as-is.
DExtra properties are logged but still passed to the controller.
Attempts:
2 left
💡 Hint

Think about what whitelist means in the context of filtering properties.

state_output
intermediate
2:00remaining
What is the output of this NestJS controller with transform: true enabled?

Given the following DTO and controller, what will be the type of age inside the create method?

NestJS
import { Controller, Post, Body, ValidationPipe } from '@nestjs/common';
import { IsInt } from 'class-validator';

class CreateUserDto {
  @IsInt()
  age: number;
}

@Controller('users')
export class UsersController {
  @Post()
  create(
    @Body(new ValidationPipe({ transform: true })) createUserDto: CreateUserDto
  ) {
    return typeof createUserDto.age;
  }
}
A"string" because request body values are strings by default.
B"undefined" because age is not set correctly.
C"number" because transform converts input to the DTO types.
D"object" because DTO instances are plain objects.
Attempts:
2 left
💡 Hint

Consider what transform: true does to incoming data types.

📝 Syntax
advanced
2:00remaining
Which option correctly enables both whitelist and transform globally in a NestJS app?

Choose the correct way to apply ValidationPipe globally with whitelist and transform enabled.

NestJS
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  // Apply global pipe here
  await app.listen(3000);
}
bootstrap();
Aapp.useGlobalPipes(new ValidationPipe({ whitelist: 'true', transform: 'true' }));
Bapp.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));
Capp.useGlobalPipes(new ValidationPipe[whitelist: true, transform: true]);
Dapp.useGlobalPipes(ValidationPipe({ whitelist: true, transform: true }));
Attempts:
2 left
💡 Hint

Remember how to instantiate classes and pass options as an object.

🔧 Debug
advanced
2:00remaining
Why does this NestJS ValidationPipe not remove extra properties despite whitelist: true?

Examine the code below. The controller expects whitelist: true to remove extra properties, but they remain. What is the cause?

NestJS
import { Controller, Post, Body, ValidationPipe } from '@nestjs/common';
import { IsString } from 'class-validator';

class CreateUserDto {
  @IsString()
  username: string;
}

@Controller('users')
export class UsersController {
  @Post()
  create(
    @Body(new ValidationPipe({ whitelist: true, skipMissingProperties: true })) createUserDto: CreateUserDto
  ) {
    return createUserDto;
  }
}
AThe <code>skipMissingProperties</code> option disables <code>whitelist</code> filtering.
BThe DTO class is missing <code>@Type()</code> decorator to enable transformation.
CThe <code>ValidationPipe</code> must be applied globally to work properly.
DThe extra properties are not removed because <code>forbidNonWhitelisted</code> is not set.
Attempts:
2 left
💡 Hint

Check how skipMissingProperties affects validation behavior.

🧠 Conceptual
expert
3:00remaining
What is the combined effect of whitelist: true and forbidNonWhitelisted: true in NestJS ValidationPipe?

When both whitelist and forbidNonWhitelisted are set to true, what happens if the request body contains extra properties not in the DTO?

AThe request proceeds but the extra properties are converted to null.
BThe extra properties are removed silently and the request proceeds.
CThe extra properties are kept but logged as warnings.
DThe request fails validation and an error is thrown for extra properties.
Attempts:
2 left
💡 Hint

Consider what forbidNonWhitelisted adds on top of whitelist.