What is the purpose of @IsString() decorator in class-validator?
The @IsString() decorator checks if the value of a property is a string. It helps ensure the data type is correct before processing.
Click to reveal answer
beginner
How does @IsNotEmpty() decorator help in validation?
The @IsNotEmpty() decorator ensures that a property is not empty. It rejects empty strings, null, or undefined values to guarantee required data is present.
Click to reveal answer
beginner
What does @IsEmail() decorator validate?
The @IsEmail() decorator checks if the property contains a valid email address format. It helps catch invalid emails early.
Click to reveal answer
intermediate
Explain the use of @MinLength(5) decorator.
The @MinLength(5) decorator ensures the string property has at least 5 characters. It helps enforce minimum length rules like passwords or usernames.
Click to reveal answer
intermediate
What is the role of @ValidateNested() decorator?
The @ValidateNested() decorator validates nested objects inside a class. It tells class-validator to check the inner object's decorators too.
Click to reveal answer
Which decorator checks if a property is a number?
A@IsBoolean()
B@IsString()
C@IsNumber()
D@IsEmail()
✗ Incorrect
The @IsNumber() decorator validates that the property is a number.
What does @IsOptional() do in class-validator?
AMakes a property required
BSkips validation if the property is missing
CChecks if a property is a string
DValidates nested objects
✗ Incorrect
The @IsOptional() decorator skips validation if the property is not present.
Which decorator would you use to ensure a string is not empty?
A@IsNotEmpty()
B@IsEmail()
C@MinLength(1)
D@IsString()
✗ Incorrect
@IsNotEmpty() ensures the property is not empty, rejecting empty strings.
How do you validate that a property is an array?
A@IsArray()
B@IsObject()
C@IsString()
D@IsNumber()
✗ Incorrect
@IsArray() checks if the property is an array.
What is the use of @ValidateNested()?
AMakes a property optional
BChecks if a property is a number
CEnsures a string is an email
DValidates nested objects inside a class
✗ Incorrect
@ValidateNested() tells class-validator to validate nested object properties.
Describe how you would use class-validator decorators to ensure a user signup form has a valid email, a password with minimum length 8, and an optional nickname.
Think about which decorators check format, length, and optional fields.
You got /3 concepts.
Explain why @ValidateNested() is important when validating objects that contain other objects.
Consider how validation works on nested data structures.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of @IsEmail() decorator in class-validator when used in a NestJS DTO class?
easy
A. To convert the property value to lowercase
B. To check if the property value is a valid email address format
C. To make the property optional
D. To trim whitespace from the property value
Solution
Step 1: Understand the decorator's role
The @IsEmail() decorator validates that the decorated property contains a valid email format string.
Step 2: Compare options with decorator behavior
Only To check if the property value is a valid email address format matches the validation purpose of @IsEmail(). Other options describe unrelated behaviors.
Final Answer:
To check if the property value is a valid email address format -> Option B
Quick Check:
@IsEmail() validates email format [OK]
Hint: Remember: @IsEmail() checks email format only [OK]
Common Mistakes:
Thinking @IsEmail() modifies the value instead of validating
Confusing @IsEmail() with @IsOptional() or @Transform decorators
2. Which of the following is the correct way to use @Length() decorator to require a string property to be between 5 and 10 characters in NestJS?
easy
A. @Length(5, 10)
B. @Length(10, 5)
C. @Length(min=5, max=10)
D. @Length({min:5, max:10})
Solution
Step 1: Recall the syntax of @Length decorator
The @Length() decorator takes two numeric arguments: minimum length first, then maximum length.
Step 2: Match options with correct syntax
@Length(5, 10) correctly uses @Length(5, 10). Options A, B, and D use incorrect argument order or syntax.
Final Answer:
@Length(5, 10) -> Option A
Quick Check:
@Length(min, max) = @Length(5, 10) [OK]
Hint: @Length(min, max) uses two numbers, min first [OK]
Common Mistakes:
Swapping min and max values
Using object syntax instead of positional arguments
Using named parameters which are invalid
3. Given this DTO class, what will be the validation result for username = 'abc' and age = 17?
Since price is string, validation will fail because @IsInt() requires a number. The type should be number.
Final Answer:
price should be a number type, not string -> Option A
Quick Check:
Type mismatch between property and decorators = error [OK]
Hint: Match property type with decorator expectations [OK]
Common Mistakes:
Using string type with number validators
Assuming decorators convert types automatically
Ignoring TypeScript type declarations
5. You want to validate a DTO property tags which is an array of strings, each string must be at least 3 characters. Which combination of class-validator decorators correctly enforces this?
hard
A. @IsString()\n@Length(3, 50)\n@IsArray()
B. @IsArray()\n@IsString()\n@Length(3, 50)
C. @IsArray()\n@ArrayMinSize(1)\n@IsString({ each: true })\n@Length(3, 50, { each: true })
D. @IsArray()\n@ArrayMaxSize(1)\n@IsString({ each: false })
Solution
Step 1: Validate the property is an array
Use @IsArray() to ensure tags is an array type.
Step 2: Validate each element is a string with length constraints
Use @IsString({ each: true }) and @Length(3, 50, { each: true }) to apply string and length validation to every item in the array.