Bird
Raised Fist0
NestJSframework~5 mins

Whitelist and transform options in NestJS

Choose your learning style10 modes available
Introduction

Whitelist and transform options help clean and shape data coming into your NestJS app. They make sure only allowed data is kept and convert data to the right types automatically.

When you want to accept only specific fields from user input to keep your app safe.
When you want to automatically convert strings to numbers or dates in incoming requests.
When you want to avoid unexpected or extra data that your app does not need.
When you want to simplify your code by letting NestJS handle data conversion.
When you want to protect your app from malicious or wrong data formats.
Syntax
NestJS
import { ValidationPipe } from '@nestjs/common';

app.useGlobalPipes(new ValidationPipe({
  whitelist: true,
  transform: true,
}));

whitelist: Removes any properties that are not in your DTO (Data Transfer Object).

transform: Automatically converts input data to the types defined in your DTO classes.

Examples
This removes any extra fields not defined in your DTO but does not convert types.
NestJS
new ValidationPipe({ whitelist: true })
This converts input data types but keeps all fields, even extra ones.
NestJS
new ValidationPipe({ transform: true })
This removes extra fields and converts data types automatically.
NestJS
new ValidationPipe({ whitelist: true, transform: true })
This disables both features, so input is accepted as-is.
NestJS
new ValidationPipe({ whitelist: false, transform: false })
Sample Program

This NestJS app has a POST /users endpoint. It uses ValidationPipe with whitelist and transform options on the body. It only accepts 'name' and 'age' fields. It converts 'age' to a number automatically.

If extra fields are sent, they are removed. If 'age' is sent as a string like '25', it becomes the number 25.

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

class CreateUserDto {
  @IsString()
  name: string;

  @IsInt()
  age: number;
}

@Controller('users')
class UserController {
  @Post()
  createUser(@Body(new ValidationPipe({ whitelist: true, transform: true })) createUserDto: CreateUserDto) {
    return createUserDto;
  }
}

@Module({
  controllers: [UserController],
})
class AppModule {}

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}

bootstrap();
OutputSuccess
Important Notes

Time complexity: Validation and transformation run in linear time relative to input size.

Space complexity: Uses extra memory proportional to input size for creating new objects.

Common mistake: Forgetting to enable whitelist allows unwanted fields to pass through.

Use whitelist to keep data clean and transform to avoid manual type conversions.

Summary

Whitelist removes unwanted fields from input data.

Transform converts input data to the types defined in your DTO.

Using both together keeps your app safe and your data clean.

Practice

(1/5)
1. What does the whitelist option do when enabled in NestJS validation pipe?
easy
A. It disables validation for unknown properties.
B. It removes any properties not defined in the DTO from the input data.
C. It transforms input data types to match the DTO automatically.
D. It logs all incoming requests for debugging.

Solution

  1. Step 1: Understand the whitelist option purpose

    The whitelist option removes any properties from the input that are not explicitly defined in the DTO (Data Transfer Object).
  2. Step 2: Compare with other options

    Transform changes types, logging is unrelated, and disabling validation is not whitelist's role.
  3. Final Answer:

    It removes any properties not defined in the DTO from the input data. -> Option B
  4. Quick Check:

    Whitelist = remove extra fields [OK]
Hint: Whitelist = remove unwanted fields from input [OK]
Common Mistakes:
  • Confusing whitelist with transform option
  • Thinking whitelist disables validation
  • Assuming whitelist logs data
2. Which is the correct way to enable both whitelist and transform options in a global validation pipe in NestJS?
easy
A. app.useGlobalPipes(new ValidationPipe({ whitelist: false, transform: true }));
B. app.useGlobalPipes(new ValidationPipe(whitelist: true, transform: true));
C. app.useGlobalPipes(new ValidationPipe({ whitelist: 'true', transform: 'true' }));
D. app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));

Solution

  1. Step 1: Check the syntax for ValidationPipe options

    Options must be passed as an object with keys and boolean values inside curly braces.
  2. Step 2: Validate option values

    Both whitelist and transform must be boolean true, not strings or missing braces.
  3. Final Answer:

    app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true })); -> Option D
  4. Quick Check:

    Correct object syntax with booleans [OK]
Hint: Use object with booleans inside braces for options [OK]
Common Mistakes:
  • Missing curly braces around options
  • Using strings instead of booleans
  • Setting whitelist to false by mistake
3. Given this DTO and input, what will be the output after validation with whitelist: true and transform: true?
class UserDto {
  name: string;
  age: number;
}

const input = { name: 'Alice', age: '30', extra: 'remove me' };
medium
A. { name: 'Alice', age: 30 }
B. { name: 'Alice', age: '30', extra: 'remove me' }
C. { name: 'Alice', age: 30, extra: 'remove me' }
D. Validation error due to extra property

Solution

  1. Step 1: Apply whitelist option

    The extra property 'extra' is removed because whitelist is true.
  2. Step 2: Apply transform option

    The age string '30' is converted to number 30 as per DTO type.
  3. Final Answer:

    { name: 'Alice', age: 30 } -> Option A
  4. Quick Check:

    Whitelist removes extra, transform converts types [OK]
Hint: Whitelist removes extras; transform converts types [OK]
Common Mistakes:
  • Expecting extra fields to remain
  • Not realizing transform converts string to number
  • Thinking validation fails on extra fields with whitelist
4. You set whitelist: true but forgot transform: true in your ValidationPipe. What issue might you face when receiving input with number fields as strings?
medium
A. Input number fields remain strings, causing type errors later.
B. Extra fields are not removed from input.
C. ValidationPipe throws a syntax error.
D. Input is automatically converted to correct types.

Solution

  1. Step 1: Understand whitelist without transform

    Whitelist removes extra fields but does not convert types.
  2. Step 2: Consequence of missing transform

    Number fields sent as strings remain strings, which can cause errors in code expecting numbers.
  3. Final Answer:

    Input number fields remain strings, causing type errors later. -> Option A
  4. Quick Check:

    Missing transform = no type conversion [OK]
Hint: Whitelist only removes fields; transform converts types [OK]
Common Mistakes:
  • Assuming whitelist converts types
  • Expecting syntax errors from missing transform
  • Thinking extra fields remain with whitelist true
5. You want to accept a nested object in your DTO and ensure unwanted fields are removed and types are converted. Which ValidationPipe options setup correctly handles nested objects with whitelist and transform?
hard
A. new ValidationPipe({ whitelist: true, transform: true, forbidNonWhitelisted: true })
B. new ValidationPipe({ whitelist: false, transform: true, transformOptions: { enableImplicitConversion: false } })
C. new ValidationPipe({ whitelist: true, transform: true, transformOptions: { enableImplicitConversion: true } })
D. new ValidationPipe({ whitelist: true, transform: false, forbidNonWhitelisted: true })

Solution

  1. Step 1: Recognize nested object handling needs transform options

    For nested objects, enabling implicit conversion helps transform nested types correctly.
  2. Step 2: Check options for whitelist and transform

    Whitelist true removes unwanted fields; transform true with transformOptions enables nested conversion.
  3. Step 3: Compare other options

    Options with forbidNonWhitelisted: true throw errors on extras instead of removing; others disable whitelist or transform or lack transformOptions.
  4. Final Answer:

    new ValidationPipe({ whitelist: true, transform: true, transformOptions: { enableImplicitConversion: true } }) -> Option C
  5. Quick Check:

    whitelist + transform + enableImplicitConversion [OK]
Hint: Use transformOptions with enableImplicitConversion for nested objects [OK]
Common Mistakes:
  • Ignoring transformOptions for nested conversion
  • Disabling transform but expecting type conversion
  • Not using whitelist to remove extra nested fields
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard