0
0
NestJSframework~10 mins

DTO class creation in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a basic DTO class with a single string property.

NestJS
export class CreateUserDto {
  [1] name: string;
}
Drag options to blanks, or click blank then click option'
Apublic
Bname
Creadonly
Dprivate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'private' or 'public' instead of 'readonly' for DTO properties.
Omitting any keyword and making the property mutable.
2fill in blank
medium

Complete the code to import the class-validator decorator for string validation.

NestJS
import { [1] } from 'class-validator';

export class CreateUserDto {
  readonly name: string;
}
Drag options to blanks, or click blank then click option'
AIsNumber
BIsDate
CIsBoolean
DIsString
Attempts:
3 left
💡 Hint
Common Mistakes
Using decorators for other types like numbers or booleans incorrectly.
Forgetting to import the decorator.
3fill in blank
hard

Fix the error in the DTO property decorator to validate a non-empty string.

NestJS
import { IsString, [1] } from 'class-validator';

export class CreateUserDto {
  @IsString()
  @[1]()
  readonly name: string;
}
Drag options to blanks, or click blank then click option'
AIsNotEmpty
BIsNumber
CIsOptional
DIsBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using IsOptional which allows empty values.
Using decorators for wrong types.
4fill in blank
hard

Fill both blanks to create a DTO class with a numeric age property that must be positive.

NestJS
import { IsPositive, [1] } from 'class-validator';

export class CreateUserDto {
  readonly name: string;
  @[2]()
  @IsPositive()
  readonly age: number;
}
Drag options to blanks, or click blank then click option'
AIsNumber
BIsString
CIsBoolean
DIsDate
Attempts:
3 left
💡 Hint
Common Mistakes
Using string or boolean decorators for numeric properties.
Forgetting to import the correct decorators.
5fill in blank
hard

Fill all three blanks to create a DTO class with a required email string property validated for email format.

NestJS
import { IsString, [1], [2] } from 'class-validator';

export class CreateUserDto {
  @IsString()
  @IsEmail()
  @[3]()
  readonly email: string;
}
Drag options to blanks, or click blank then click option'
AIsEmail
BIsNotEmpty
CIsOptional
DIsBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using IsOptional which allows missing emails.
Using boolean decorators for string properties.