0
0
NestJSframework~20 mins

ValidationPipe in depth in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ValidationPipe Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when ValidationPipe is used globally with transform enabled?

Consider a NestJS app where ValidationPipe is applied globally with transform: true. What is the output type of a controller method receiving a DTO?

NestJS
app.useGlobalPipes(new ValidationPipe({ transform: true }));

@Post()
create(@Body() createDto: CreateDto) {
  return typeof createDto;
}
A"undefined"
B"object"
C"number"
D"string"
Attempts:
2 left
💡 Hint

Think about what transform: true does to the incoming data.

📝 Syntax
intermediate
2:00remaining
Which option correctly enables whitelist and forbids non-whitelisted properties?

Choose the correct way to configure ValidationPipe to remove properties not in the DTO and throw an error if extra properties are present.

Anew ValidationPipe({ whitelist: true, forbidNonWhitelisted: true })
Bnew ValidationPipe({ whitelist: false, forbidNonWhitelisted: false })
Cnew ValidationPipe({ whitelist: true, forbidNonWhitelisted: false })
Dnew ValidationPipe({ whitelist: false, forbidNonWhitelisted: true })
Attempts:
2 left
💡 Hint

Whitelist removes extra properties. ForbidNonWhitelisted throws error on extra properties.

🔧 Debug
advanced
2:00remaining
Why does ValidationPipe not validate nested objects by default?

Given a DTO with a nested object property, why does ValidationPipe skip validating the nested object?

Example DTO:

class AddressDto {
  @IsString()
  street: string;
}

class UserDto {
  @ValidateNested()
  @Type(() => AddressDto)
  address: AddressDto;
}
ABecause ValidationPipe needs <code>transform: true</code> to convert nested objects before validation
BBecause <code>ValidateNested</code> and <code>Type</code> decorators are missing or misused
CBecause ValidationPipe requires <code>enableImplicitConversion</code> to validate nested objects
DBecause ValidationPipe does not support nested validation at all
Attempts:
2 left
💡 Hint

Think about how nested objects are converted before validation.

state_output
advanced
2:00remaining
What error message is returned when forbidNonWhitelisted is true and extra properties exist?

When ValidationPipe is configured with forbidNonWhitelisted: true and the request body contains extra properties not in the DTO, what is the typical error message returned?

A"Extra properties detected: extra"
B"Validation failed: extra is not allowed"
C"Unexpected property found: extra"
D"property 'extra' should not exist"
Attempts:
2 left
💡 Hint

Look for the exact wording from class-validator errors.

🧠 Conceptual
expert
3:00remaining
How does ValidationPipe interact with custom validators and async validation?

When using custom async validators in DTOs, how does ValidationPipe handle the validation process?

AValidationPipe runs async validators but does not wait for their completion, causing possible false positives
BValidationPipe ignores async validators and only runs synchronous ones
CValidationPipe waits for all async validators to complete before returning the result
DValidationPipe requires a special flag to enable async validation
Attempts:
2 left
💡 Hint

Think about how async validation integrates with the validation lifecycle.