Hi everyone.
In this video I will show you how to validation.
For the "DTO", Data transfer object and let's click on inside the validation topics here.
Firstly we will install 2 packages right here.
Let me copy.
And the first one will be class validator for validation object and transformer.
We will use that later okay let's focus on the class validator.
If I scroll down a little bit right here, now you can see it has a some decorator.
Something like this is email is not empty okay.
This is from the class validator.
And class validator library already provides some built-in validation rules.
Right.
For that reason we can use that.
Let me show you in the createDto here.
I will use @IsNotEmpty()
And I will copy this and pass it inside the description.
Let's see.
Okay, let's see what happens if I use something like this, I will remove the title.
And I send, you can see it's created right.
And that's not what we want.
The reason because we NOT.
Let me see, let me scroll down a little bit.
We NOT you the validation pipe right here.
We must use the Global Pipes.
Okay.
Let me copy this line right here and go to the main.ts
And right here I will pass this and validation pipe from.
nestjs/common, okay
Make sure we use the global pipe.
And let me send again. Now you may see, it already format for me the message right here.
Very cool.
Right.
If I remove the description also and I will send, this title should not be empty or description should
not be empty.
Right?
Very cool.
And in order to read more about the class validator you can search the class validator.
Right here.
Now you can see we have many more validator.
For example a min or max is this is email, Len something like this right.
You can read more about that and try a little bit with that.
I will leave that job for you okay.
And in this course we will use more and more class validator.
Don't worry about that.
And it's also very popular package right.
So that's how we can validation.
I will see you in the next one.