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.
Whitelist and transform options in 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.
new ValidationPipe({ whitelist: true })new ValidationPipe({ transform: true })new ValidationPipe({ whitelist: true, transform: true })new ValidationPipe({ whitelist: false, transform: false })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.
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();
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.
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
whitelist option do when enabled in NestJS validation pipe?Solution
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).Step 2: Compare with other options
Transform changes types, logging is unrelated, and disabling validation is not whitelist's role.Final Answer:
It removes any properties not defined in the DTO from the input data. -> Option BQuick Check:
Whitelist = remove extra fields [OK]
- Confusing whitelist with transform option
- Thinking whitelist disables validation
- Assuming whitelist logs data
whitelist and transform options in a global validation pipe in NestJS?Solution
Step 1: Check the syntax for ValidationPipe options
Options must be passed as an object with keys and boolean values inside curly braces.Step 2: Validate option values
Both whitelist and transform must be boolean true, not strings or missing braces.Final Answer:
app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true })); -> Option DQuick Check:
Correct object syntax with booleans [OK]
- Missing curly braces around options
- Using strings instead of booleans
- Setting whitelist to false by mistake
whitelist: true and transform: true?
class UserDto {
name: string;
age: number;
}
const input = { name: 'Alice', age: '30', extra: 'remove me' };Solution
Step 1: Apply whitelist option
The extra property 'extra' is removed because whitelist is true.Step 2: Apply transform option
The age string '30' is converted to number 30 as per DTO type.Final Answer:
{ name: 'Alice', age: 30 } -> Option AQuick Check:
Whitelist removes extra, transform converts types [OK]
- Expecting extra fields to remain
- Not realizing transform converts string to number
- Thinking validation fails on extra fields with whitelist
whitelist: true but forgot transform: true in your ValidationPipe. What issue might you face when receiving input with number fields as strings?Solution
Step 1: Understand whitelist without transform
Whitelist removes extra fields but does not convert types.Step 2: Consequence of missing transform
Number fields sent as strings remain strings, which can cause errors in code expecting numbers.Final Answer:
Input number fields remain strings, causing type errors later. -> Option AQuick Check:
Missing transform = no type conversion [OK]
- Assuming whitelist converts types
- Expecting syntax errors from missing transform
- Thinking extra fields remain with whitelist true
Solution
Step 1: Recognize nested object handling needs transform options
For nested objects, enabling implicit conversion helps transform nested types correctly.Step 2: Check options for whitelist and transform
Whitelist true removes unwanted fields; transform true with transformOptions enables nested conversion.Step 3: Compare other options
Options withforbidNonWhitelisted: truethrow errors on extras instead of removing; others disable whitelist or transform or lacktransformOptions.Final Answer:
new ValidationPipe({ whitelist: true, transform: true, transformOptions: { enableImplicitConversion: true } }) -> Option CQuick Check:
whitelist + transform + enableImplicitConversion [OK]
- Ignoring transformOptions for nested conversion
- Disabling transform but expecting type conversion
- Not using whitelist to remove extra nested fields
Ready to prove it?
Pass the test and this pattern moves unknown
