0
0
NestJSframework~20 mins

Nested DTO validation in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested DTO Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when validating nested DTOs with missing required fields?

Consider the following NestJS DTOs with validation decorators. What will happen if the nested object is missing a required field?

NestJS
import { IsString, ValidateNested } from 'class-validator';
import { Type } from 'class-transformer';

class AddressDto {
  @IsString()
  street: string;

  @IsString()
  city: string;
}

class UserDto {
  @IsString()
  name: string;

  @ValidateNested()
  @Type(() => AddressDto)
  address: AddressDto;
}

// Input object:
const input = {
  name: 'Alice',
  address: {
    city: 'Wonderland'
  }
};

// What happens when validating input against UserDto?
AValidation fails because 'street' is missing in the nested AddressDto.
BValidation passes because only the top-level fields are checked.
CValidation fails because 'name' is missing, even though it is present.
DValidation passes because nested validation decorators are ignored by default.
Attempts:
2 left
💡 Hint

Think about how @ValidateNested() and @Type() work together to validate nested objects.

📝 Syntax
intermediate
2:00remaining
Which option correctly applies nested DTO validation in NestJS?

Given two DTO classes, which code snippet correctly applies nested validation so that the nested object is validated?

NestJS
class ChildDto {
  @IsString()
  childProp: string;
}

class ParentDto {
  @IsString()
  parentProp: string;

  // What decorators should be here for nested validation?
  nested: ChildDto;
}
A
@ValidateIf(o => o.nested)
@IsString()
nested: ChildDto;
B
@IsObject()
@ValidateNested({ each: true })
nested: ChildDto;
C
@ValidateNested()
@Type(() => ChildDto)
nested: ChildDto;
D
@IsDefined()
@Type(() => ChildDto)
nested: ChildDto;
Attempts:
2 left
💡 Hint

Remember that @ValidateNested() triggers validation of nested objects and @Type() transforms plain objects to class instances.

🔧 Debug
advanced
2:00remaining
Why does nested DTO validation not trigger for an array of nested objects?

Given this DTO, why does validation not run on the nested array items?

NestJS
class ItemDto {
  @IsString()
  name: string;
}

class ContainerDto {
  @ValidateNested({ each: true })
  @Type(() => ItemDto)
  items: ItemDto[];
}

// Input:
const input = { items: [{ name: 'Item1' }, { name: 123 }] };

// Validation is run on ContainerDto with input. Why does the invalid second item not cause an error?
ABecause @ValidateNested({ each: true }) is ignored on arrays.
BBecause the input array items are not transformed to ItemDto instances before validation.
CBecause @Type(() => ItemDto) only works on single objects, not arrays.
DBecause the validation pipe is missing in the controller.
Attempts:
2 left
💡 Hint

Think about how class-transformer transforms arrays of objects.

state_output
advanced
2:00remaining
What is the validation error output for deeply nested DTOs with missing fields?

Given these nested DTOs, what validation error message will be produced if the deepest nested field is missing?

NestJS
class Level3Dto {
  @IsString()
  detail: string;
}

class Level2Dto {
  @ValidateNested()
  @Type(() => Level3Dto)
  level3: Level3Dto;
}

class Level1Dto {
  @ValidateNested()
  @Type(() => Level2Dto)
  level2: Level2Dto;
}

const input = { level2: { level3: {} } };

// After validating input against Level1Dto, what error appears?
A[{"property":"level3","constraints":{"isString":"detail must be a string"}}]
BNo validation errors because nested validation stops at level2.
C[{"property":"level2","constraints":{"isDefined":"level2 should not be null or undefined"}}]
D[{"property":"level2","children":[{"property":"level3","children":[{"property":"detail","constraints":{"isString":"detail must be a string"}}]}]}]
Attempts:
2 left
💡 Hint

Consider how validation errors are nested and reported for nested DTOs.

🧠 Conceptual
expert
3:00remaining
Why is the @Type decorator essential for nested DTO validation in NestJS?

In NestJS, why must you use the @Type(() => NestedDto) decorator alongside @ValidateNested() for nested DTO validation?

ABecause @Type transforms plain objects into class instances, enabling class-validator to run validation decorators on nested DTOs.
BBecause @Type automatically validates nested DTOs without needing @ValidateNested().
CBecause @Type disables validation on nested DTOs to improve performance.
DBecause @Type is required to serialize nested DTOs to JSON after validation.
Attempts:
2 left
💡 Hint

Think about how class-transformer and class-validator work together in NestJS.