ValidationPipe automatically checks incoming data against defined rules to ensure it is valid before processing. It helps catch errors early and keeps the app safe.
Click to reveal answer
beginner
How do you apply ValidationPipe globally in a NestJS application?
Use app.useGlobalPipes(new ValidationPipe()) inside the main.ts file after creating the app instance.
Click to reveal answer
beginner
Which package must be installed to use ValidationPipe with decorators like @IsString()?
You need to install class-validator and class-transformer packages to use ValidationPipe with decorators.
Click to reveal answer
intermediate
What does the 'whitelist' option do in ValidationPipe?
It removes any properties from the input object that are not defined in the DTO, helping to keep data clean and safe.
Click to reveal answer
intermediate
How can you enable automatic transformation of input data types in ValidationPipe?
Set the 'transform' option to true in ValidationPipe. This converts input strings to the expected types defined in DTOs.
Click to reveal answer
Where do you typically set up ValidationPipe globally in a NestJS app?
AInside each controller method
BIn main.ts using app.useGlobalPipes()
CIn the service class constructor
DIn the module imports array
✗ Incorrect
Global pipes like ValidationPipe are set up in main.ts with app.useGlobalPipes() to apply validation app-wide.
Which decorator is used to mark a class property as a string for validation?
A@IsString()
B@IsBoolean()
C@IsNumber()
D@IsDate()
✗ Incorrect
@IsString() marks a property to be validated as a string using class-validator.
What happens if ValidationPipe receives invalid data?
AIt throws a BadRequestException
BIt silently ignores the error
CIt logs a warning but continues
DIt converts data to default values
✗ Incorrect
ValidationPipe throws a BadRequestException when validation fails, stopping the request.
What does setting 'forbidNonWhitelisted' to true do in ValidationPipe?
AIgnores validation rules
BRemoves extra properties silently
CAllows extra properties without error
DThrows an error if extra properties exist
✗ Incorrect
'forbidNonWhitelisted: true' causes ValidationPipe to throw an error if unknown properties are present.
Which packages are required to use ValidationPipe decorators?
Aexpress and body-parser
Btypeorm and graphql
Cclass-validator and class-transformer
Drxjs and reflect-metadata
✗ Incorrect
class-validator and class-transformer provide decorators and transformation support for ValidationPipe.
Explain how to set up ValidationPipe globally in a NestJS app and why it is useful.
Think about where the app starts and how to apply validation everywhere.
You got /4 concepts.
Describe the purpose of the 'whitelist' and 'transform' options in ValidationPipe.
Consider how these options clean and convert data.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using ValidationPipe in a NestJS application?
easy
A. To serve static files like images and CSS
B. To handle database connections automatically
C. To check and clean incoming data to ensure it is safe and correct
D. To manage user authentication and authorization
Solution
Step 1: Understand ValidationPipe role
ValidationPipe is designed to validate and sanitize incoming data in NestJS, ensuring the data meets expected rules.
Step 2: Compare with other options
Other options like database handling, authentication, or serving static files are unrelated to ValidationPipe's purpose.
Final Answer:
To check and clean incoming data to ensure it is safe and correct -> Option C
Quick Check:
ValidationPipe purpose = data validation and cleaning [OK]
Hint: ValidationPipe is all about checking incoming data [OK]
Common Mistakes:
Confusing ValidationPipe with authentication features
C. useGlobalPipes should be called inside a controller
D. ValidationPipe cannot be used globally
Solution
Step 1: Check ValidationPipe instantiation
ValidationPipe is a class and must be instantiated with the new keyword.
Step 2: Validate other parts
The transform option is valid, and useGlobalPipes is correctly used in main.ts.
Final Answer:
Missing 'new' keyword before ValidationPipe -> Option A
Quick Check:
Instantiate ValidationPipe with new [OK]
Hint: Always use 'new' with ValidationPipe class [OK]
Common Mistakes:
Omitting 'new' keyword when creating ValidationPipe
Thinking transform option is invalid
Trying to set global pipes inside controllers
5. You want to automatically convert incoming JSON strings to their expected types in your DTOs. Which ValidationPipe option should you enable globally to achieve this?
hard
A. forbidNonWhitelisted: true
B. whitelist: true
C. disableErrorMessages: true
D. transform: true
Solution
Step 1: Understand transform option
Setting transform: true tells ValidationPipe to convert incoming payloads to the types defined in DTO classes automatically.
Step 2: Check other options
whitelist removes extra properties, forbidNonWhitelisted throws errors on extra props, and disableErrorMessages hides validation errors; none convert types.
Final Answer:
transform: true -> Option D
Quick Check:
transform true = auto type conversion [OK]
Hint: Use transform true to convert types automatically [OK]
Common Mistakes:
Confusing whitelist with type conversion
Thinking forbidNonWhitelisted converts types
Believing disableErrorMessages affects data types
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.