What does the whitelist option do in NestJS validation?
The whitelist option removes any properties from the input object that are not explicitly defined in the DTO (Data Transfer Object). This helps keep only the allowed data and improves security.
Click to reveal answer
beginner
What is the purpose of the transform option in NestJS validation pipe?
The transform option automatically converts input data types to the types defined in the DTO classes. For example, it can convert a string to a number if the DTO expects a number.
Click to reveal answer
intermediate
How do whitelist and transform options work together in NestJS?
When both options are enabled, NestJS first removes unwanted properties (whitelist) and then converts the remaining data to the correct types (transform). This ensures clean and correctly typed input data.
Click to reveal answer
beginner
How do you enable whitelist and transform options globally in a NestJS app?
You enable them by passing options to the ValidationPipe in the main app file, like this:
What happens if whitelist is false and extra properties are sent in a request?
If whitelist is false, extra properties not defined in the DTO remain in the input object. This can lead to unexpected data being processed or security risks.
Click to reveal answer
What does setting whitelist: true in NestJS ValidationPipe do?
AConverts input data types automatically
BAllows all properties without filtering
CDisables validation
DRemoves properties not in the DTO from the input
✗ Incorrect
The whitelist option removes any properties not defined in the DTO, keeping only allowed data.
What is the effect of transform: true in NestJS ValidationPipe?
AAutomatically converts input types to DTO types
BBlocks all invalid requests
CRemoves extra properties
DLogs validation errors
✗ Incorrect
Transform converts input data types to match the DTO class types automatically.
How do you enable both whitelist and transform globally in NestJS?
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
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 A
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 })