Discover how to make your API data handling effortless and safe with just a simple setting!
Why Whitelist and transform options in NestJS? - Purpose & Use Cases
Imagine building an API where users send data, and you manually check each field to accept or reject it.
You also manually convert data types like strings to numbers everywhere in your code.
This manual checking and converting is slow, repetitive, and easy to forget.
It leads to bugs, security holes, and messy code that is hard to maintain.
Whitelist and transform options in NestJS automatically allow only expected fields and convert data types for you.
This keeps your code clean, safe, and consistent without extra effort.
if (typeof data.age === 'string') { data.age = parseInt(data.age); } for (const key in data) { if (!['name', 'age'].includes(key)) { delete data[key]; } }
app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));You can trust incoming data is clean and correctly typed, so your app runs smoothly and securely.
When a user submits a form with extra fields or wrong types, NestJS strips unwanted fields and converts types automatically before your code uses the data.
Manual data checks are error-prone and tedious.
Whitelist option removes unexpected fields automatically.
Transform option converts data types for you.
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
