A DTO (Data Transfer Object) is a simple object that defines how data is sent over the network. We use it to validate and type-check incoming data to keep our app safe and organized.
Click to reveal answer
intermediate
How do you enable validation for nested objects inside a DTO in NestJS?
Use the @ValidateNested() decorator on the nested property and add @Type(() => NestedDtoClass) from 'class-transformer' to tell NestJS how to transform and validate the nested object.
Click to reveal answer
intermediate
What decorators are commonly used to validate nested DTO properties in NestJS?
Common decorators are @ValidateNested() to trigger nested validation and @Type(() => ClassName) to transform plain objects into class instances for validation.
Click to reveal answer
intermediate
Why do we need to use @Type(() => ClassName) when validating nested DTOs?
Because validation works on class instances, @Type() converts plain JSON objects into class instances so the validation decorators inside the nested DTO can run properly.
Click to reveal answer
beginner
What happens if you forget to use @ValidateNested() on a nested DTO property?
The nested object will not be validated, so invalid or missing data inside it might pass through without errors, causing potential bugs or security issues.
Click to reveal answer
Which decorator is essential to validate nested DTO properties in NestJS?
A@IsNotEmpty()
B@ValidateNested()
C@IsString()
D@IsNumber()
✗ Incorrect
@ValidateNested() tells NestJS to validate the nested object using its own validation rules.
What is the role of @Type(() => ClassName) in nested DTO validation?
AMarks a property as optional
BChecks if a value is a string
CTransforms plain objects into class instances for validation
DSets a default value
✗ Incorrect
@Type() from class-transformer converts plain JSON into class instances so validation decorators work.
If you have a nested DTO property without @ValidateNested(), what happens?
ANested validation is skipped
BValidation runs normally
CApp crashes
DProperty is ignored completely
✗ Incorrect
Without @ValidateNested(), the nested object's validation rules are not applied.
Which package provides the @ValidateNested() decorator used in NestJS DTO validation?
Aclass-transformer
Brxjs
Cnestjs/common
Dclass-validator
✗ Incorrect
@ValidateNested() comes from class-validator to enable nested validation.
What must you do to validate an array of nested DTOs?
AUse @ValidateNested({ each: true }) and @Type(() => NestedDto)
BUse only @IsArray()
CUse @IsString() on the array
DNo special decorators needed
✗ Incorrect
For arrays, @ValidateNested({ each: true }) validates each item, and @Type() transforms each item.
Explain how to set up nested DTO validation in NestJS step-by-step.
Think about how NestJS knows to check inside nested objects.
You got /4 concepts.
Why is it important to use both @ValidateNested() and @Type() together for nested DTOs?
One decorator triggers validation, the other prepares the data for it.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using @ValidateNested() in a NestJS DTO?
easy
A. To define default values for nested properties
B. To convert nested objects to plain JavaScript objects
C. To skip validation of nested objects
D. To validate properties of nested objects inside the DTO automatically
Solution
Step 1: Understand the role of @ValidateNested()
This decorator tells NestJS to validate nested objects inside a DTO, not just the top-level properties.
Step 2: Compare with other options
@Type() handles transformation, not validation. Skipping validation or setting defaults is unrelated to @ValidateNested().
Final Answer:
To validate properties of nested objects inside the DTO automatically -> Option D
Hint: Missing required nested fields cause validation failure [OK]
Common Mistakes:
Assuming missing nested fields are allowed by default
Ignoring the effect of @ValidateNested()
Confusing missing nested fields with missing top-level fields
4. Consider this DTO code snippet:
class ProfileDto {
@IsString()
bio: string;
}
class UserDto {
@IsString()
username: string;
@ValidateNested()
profile: ProfileDto;
}
Why will validation NOT work correctly for the nested profile property?
medium
A. Because @ValidateNested() should be on ProfileDto class
B. Because @IsString() is missing on profile
C. Because @Type(() => ProfileDto) decorator is missing on profile
D. Because bio property is missing @ValidateNested()
Solution
Step 1: Identify missing decorator for nested validation
For nested validation to work, @Type(() => ProfileDto) must be added to the nested property to transform plain objects into class instances.
Step 2: Understand why other options are incorrect
@IsString() is for string properties, not objects. @ValidateNested() belongs on the nested property, not the nested class. bio is a string, no nested validation needed.
Final Answer:
Because @Type(() => ProfileDto) decorator is missing on profile -> Option C
Quick Check:
Missing @Type() breaks nested validation [OK]
Hint: Always add @Type() with @ValidateNested() for nested DTOs [OK]
Common Mistakes:
Forgetting @Type() on nested properties
Misplacing @ValidateNested() on nested class
Confusing property decorators with class decorators
5. You have a CompanyDto with a nested array of EmployeeDto objects. Which is the correct way to validate this nested array properly?