Challenge - 5 Problems
NestJS DTO Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when a NestJS DTO class is used for validation?
Consider this DTO class in NestJS using class-validator decorators. What will happen if the input object misses the
email property?import { IsString, IsEmail } from 'class-validator';
export class UserDto {
@IsString()
name: string;
@IsEmail()
email: string;
}Attempts:
2 left
💡 Hint
Think about what happens when a required decorated property is missing in the input.
✗ Incorrect
The
@IsEmail() decorator requires the email property to be present and valid. Missing it causes validation to fail.📝 Syntax
intermediate2:00remaining
Which DTO class syntax is correct for optional properties in NestJS?
You want to create a DTO class where the
age property is optional. Which code snippet correctly defines this?Attempts:
2 left
💡 Hint
Optional properties need a special decorator to skip validation if missing.
✗ Incorrect
The
@IsOptional() decorator tells the validator to ignore the property if it is not present. The property must also be marked optional with ?.🔧 Debug
advanced2:00remaining
Why does this DTO class cause a runtime error in NestJS?
Look at this DTO class code. Why does it cause a runtime error when used in a controller?
export class ProductDto {
name: string;
price: number;
constructor() {
this.name = '';
}
}Attempts:
2 left
💡 Hint
Think about how NestJS creates instances of DTO classes and how constructors affect this.
✗ Incorrect
NestJS uses plain objects and class-transformer to create DTO instances. Having a constructor can interfere with this process and cause runtime errors.
❓ state_output
advanced2:00remaining
What is the value of the
isActive property after validation?Given this DTO class and input object, what will be the value of
isActive after validation?import { IsBoolean, IsOptional } from 'class-validator';
export class StatusDto {
@IsBoolean()
@IsOptional()
isActive?: boolean = true;
}
const input = {};Attempts:
2 left
💡 Hint
Default values in DTO classes do not apply automatically during validation.
✗ Incorrect
class-validator does not assign default values during validation. The property remains undefined if missing in input.
🧠 Conceptual
expert2:00remaining
Which statement best describes the role of DTO classes in NestJS?
Choose the most accurate description of what DTO classes do in NestJS applications.
Attempts:
2 left
💡 Hint
Think about how data flows from outside requests into your app.
✗ Incorrect
DTO classes specify how input data should look and help validate and transform it before use. They do not store data or contain business logic.