0
0
NestJSframework~10 mins

class-validator decorators 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 validate that the 'email' property is a valid email address.

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

class UserDto {
  @[1]()
  email: string;
}
Drag options to blanks, or click blank then click option'
AIsEmail
BIsString
CIsNotEmpty
DIsNumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using @IsString instead of @IsEmail
Forgetting to import the decorator
Using @IsNotEmpty which only checks presence, not format
2fill in blank
medium

Complete the code to ensure the 'age' property is a number and at least 18.

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

class UserDto {
  @IsNumber()
  @[1](18)
  age: number;
}
Drag options to blanks, or click blank then click option'
AMax
BIsPositive
CMin
DIsInt
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Max instead of @Min
Using @IsInt which only checks integer, not minimum value
Not combining with @IsNumber
3fill in blank
hard

Fix the error in the code to validate that 'username' is a non-empty string.

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

class UserDto {
  @IsString()
  @[1]()
  username: string;
}
Drag options to blanks, or click blank then click option'
AIsNotEmpty
BIsEmail
CIsNumber
DMinLength
Attempts:
3 left
💡 Hint
Common Mistakes
Using @MinLength without specifying length
Using @IsEmail which is for emails
Using @IsNumber on a string
4fill in blank
hard

Fill both blanks to validate that 'password' is a string with minimum length 8.

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

class UserDto {
  @[1]()
  @[2](8)
  password: string;
}
Drag options to blanks, or click blank then click option'
AIsString
BMinLength
CIsNotEmpty
DMaxLength
Attempts:
3 left
💡 Hint
Common Mistakes
Using @MaxLength instead of @MinLength
Forgetting to check type with @IsString
Using @IsNotEmpty which does not check length
5fill in blank
hard

Fill all three blanks to create a validation that 'tags' is an optional array of strings with each string not empty.

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

class UserDto {
  @[1]()
  @[2]({ each: true })
  @[3]({ each: true })
  tags?: string[];
}
Drag options to blanks, or click blank then click option'
AIsOptional
BIsString
CIsNotEmpty
DIsArray
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting @IsOptional and causing validation errors when property is missing
Not using { each: true } and validating the whole array instead of items
Using @IsArray but missing item validations