0
0
NestJSframework~10 mins

class-validator decorators in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - class-validator decorators
Define Class with Properties
Add Validation Decorators
Create Instance of Class
Call validate() Function
Check Validation Results
Proceed
This flow shows how you define a class with decorators, create an instance, validate it, and handle results.
Execution Sample
NestJS
import { IsString, IsInt, Min } from 'class-validator';

class User {
  @IsString()
  name: string;

  @IsInt()
  @Min(0)
  age: number;
}
Defines a User class with validation rules on name and age properties.
Execution Table
StepActionPropertyDecorator CheckValidation Result
1Create User instancenameN/AN/A
2Create User instanceageN/AN/A
3Validate 'name'@IsString()Check if value is stringPass if string, fail otherwise
4Validate 'age'@IsInt()Check if value is integerPass if integer, fail otherwise
5Validate 'age'@Min(0)Check if value >= 0Pass if >=0, fail otherwise
6Collect errorsAll propertiesAggregate failed validationsEmpty if all pass, list errors if any fail
7Return validation resultAll propertiesN/AValid or Invalid with errors
💡 Validation ends after checking all decorated properties and collecting errors.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
user.nameundefined"Alice""Alice""Alice""Alice" (valid)
user.ageundefined25252525 (valid)
validationErrors[][][][][] (no errors)
Key Moments - 3 Insights
Why does validation fail if 'age' is a string like '25'?
Because @IsInt() requires a number type, not a string. See execution_table step 4 where the integer check fails.
What happens if 'name' is missing or empty?
If 'name' is undefined or not a string, @IsString() fails validation as shown in execution_table step 3.
Can multiple decorators be used on one property?
Yes, like @IsInt() and @Min(0) on 'age'. Both checks run sequentially (steps 4 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what validation happens at step 5?
ACheck if 'name' is a string
BCheck if 'age' is an integer
CCheck if 'age' is at least 0
DCollect all validation errors
💡 Hint
Refer to execution_table row with Step 5 describing @Min(0) check.
According to variable_tracker, what is the value of 'user.name' after step 3?
Aundefined
B"Alice"
C25
D[]
💡 Hint
Check variable_tracker row for 'user.name' after Step 3.
If 'user.age' was set to -5, which step would fail validation?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look at execution_table step 5 where @Min(0) checks the minimum value.
Concept Snapshot
class-validator decorators add rules to class properties.
Use @IsString(), @IsInt(), @Min() etc. above properties.
Call validate() to check if values follow rules.
Validation returns errors or success.
Multiple decorators can combine checks on one property.
Full Transcript
This visual execution shows how class-validator decorators work in NestJS. First, you define a class with properties and add decorators like @IsString() or @IsInt() to set validation rules. When you create an instance and call validate(), each property is checked step-by-step. For example, 'name' must be a string, and 'age' must be an integer and at least zero. The execution table traces these checks and their results. The variable tracker shows how values change and validation errors accumulate. Key moments clarify common confusions like type mismatches or multiple decorators. The quiz tests understanding of validation steps and variable states. This helps beginners see exactly how validation runs and what happens when rules pass or fail.