What is the purpose of the ValidationPipe in NestJS?
ValidationPipe automatically checks incoming data against defined rules to ensure it is correct and safe before it reaches your code.
Click to reveal answer
intermediate
How do you enable automatic transformation of input data types using ValidationPipe?
By setting the option { transform: true } when creating the ValidationPipe, NestJS converts input data to the expected types defined in DTO classes.
Click to reveal answer
beginner
What happens if validation fails when using ValidationPipe?
ValidationPipe throws a detailed error response explaining which fields failed and why, preventing the controller from running with bad data.
Click to reveal answer
intermediate
Explain the difference between whitelist and forbidNonWhitelisted options in ValidationPipe.
Whitelist removes any properties not in the DTO from the input. forbidNonWhitelisted throws an error if extra properties are present, helping catch unwanted data.
Click to reveal answer
beginner
How can you apply ValidationPipe globally in a NestJS application?
Use app.useGlobalPipes(new ValidationPipe()) in the main.ts file to apply validation to all incoming requests automatically.
Click to reveal answer
What option must be set to true to convert input strings to expected types in ValidationPipe?
Atransform
Bwhitelist
CforbidNonWhitelisted
DskipMissingProperties
✗ Incorrect
Setting transform: true enables automatic type conversion of input data.
Which ValidationPipe option removes properties not defined in the DTO?
AforbidNonWhitelisted
Btransform
CstopAtFirstError
Dwhitelist
✗ Incorrect
Whitelist removes extra properties from the input data.
What does ValidationPipe do if input data fails validation?
AThrows an error and stops request processing
BLogs errors but continues
CIgnores errors and passes data
DAutomatically fixes errors
✗ Incorrect
ValidationPipe throws an error to prevent bad data from reaching your code.
How do you apply ValidationPipe to all routes in a NestJS app?
AAdd ValidationPipe to each controller method
BSet validation in DTO classes only
CUse app.useGlobalPipes(new ValidationPipe()) in main.ts
DImport ValidationPipe in every module
✗ Incorrect
Using app.useGlobalPipes applies ValidationPipe globally.
What does forbidNonWhitelisted option do in ValidationPipe?
AAutomatically adds missing properties
BThrows an error if extra properties exist
CRemoves extra properties silently
DIgnores validation errors
✗ Incorrect
forbidNonWhitelisted causes validation to fail if unexpected properties are present.
Describe how ValidationPipe helps keep your NestJS app safe and clean.
Think about how ValidationPipe acts like a gatekeeper for incoming data.
You got /4 concepts.
Explain the difference between using whitelist and forbidNonWhitelisted options in ValidationPipe.
One quietly cleans input, the other loudly complains.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of ValidationPipe in a NestJS application?
easy
A. To check and clean incoming data before it is used
B. To handle database connections automatically
C. To manage user authentication and sessions
D. To serve static files like images and CSS
Solution
Step 1: Understand ValidationPipe role
ValidationPipe is designed to validate and sanitize incoming request data to ensure it meets expected rules.
Step 2: Compare with other options
Options B, C, and D relate to other parts of NestJS or web apps, not data validation.
Final Answer:
To check and clean incoming data before it is used -> Option A
Quick Check:
ValidationPipe purpose = data validation [OK]
Hint: ValidationPipe = data check and clean before use [OK]
Common Mistakes:
Confusing ValidationPipe with authentication or database handling
Thinking ValidationPipe serves static files
Assuming ValidationPipe manages sessions
2. Which of the following is the correct way to enable automatic transformation of input data types using ValidationPipe in NestJS?
easy
A. new ValidationPipe({ forbidNonWhitelisted: true })
B. new ValidationPipe({ whitelist: true })
C. new ValidationPipe({ transform: true })
D. new ValidationPipe({ skipMissingProperties: true })
Solution
Step 1: Identify transform option
The transform: true option enables automatic conversion of input data types to expected types.
Step 2: Understand other options
whitelist removes unwanted properties, forbidNonWhitelisted throws error on extra props, skipMissingProperties skips validation for missing props.
Final Answer:
new ValidationPipe({ transform: true }) -> Option C
Quick Check:
transform option enables type conversion [OK]
Hint: Use transform: true to convert input types automatically [OK]
A. The ValidationPipe should have forbidNonWhitelisted to throw error on extra props
B. The whitelist option removes the extra property silently
C. The ValidationPipe is missing transform: true to convert types
D. The extra property causes a validation error
Solution
Step 1: Understand whitelist behavior
Whitelist removes properties not in DTO silently without error.
Step 2: Check how to throw error on extra props
To throw error on extra properties, forbidNonWhitelisted: true must be set.
Final Answer:
The ValidationPipe should have forbidNonWhitelisted to throw error on extra props -> Option A
Quick Check:
forbidNonWhitelisted throws error on extra props [OK]
Hint: Use forbidNonWhitelisted to reject extra properties with error [OK]
Common Mistakes:
Thinking whitelist throws error by default
Ignoring need for forbidNonWhitelisted option
Confusing whitelist with transform option
5. You want to ensure that your NestJS API only accepts properties defined in your DTO, automatically converts input types, and throws an error if extra properties are sent. Which ValidationPipe configuration achieves this?
hard
A. new ValidationPipe({ whitelist: true, transform: true })
B. new ValidationPipe({ whitelist: true, transform: true, forbidNonWhitelisted: true })
C. new ValidationPipe({ transform: true, skipMissingProperties: true })
D. new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true })
Solution
Step 1: Identify needed options
Whitelist removes extra props, transform converts types, forbidNonWhitelisted throws error on extra props.
Step 2: Match options to requirements
Only new ValidationPipe({ whitelist: true, transform: true, forbidNonWhitelisted: true }) includes all three: whitelist, transform, and forbidNonWhitelisted.
Final Answer:
new ValidationPipe({ whitelist: true, transform: true, forbidNonWhitelisted: true }) -> Option B