So we had a look at lode ash which is a vanilla javascript package where we needed to install some translations
and we had a look at class transformer which is also working in Manila javascript but also works particularly
well in typescript.
Now let's have a look at class validator.
This now is a package which really utilizes typescript it builds up on the concept of typescript decorators
and it gives us a brand new way of working with them or at least almost brand new.
If you followed through my decorator module in this course you will actually see something here in this
lecture which we kind of built on our own there as well.
Now however with this third party library we don't have to reinvent the wheel from scratch.
We don't have to build it on our own and does library also arguably is a bit more elaborate than our
solution in the decorators module.
I am talking about validation.
This is a package which allows us to add validation rules with the help of some decorators inside of
a class.
And then whenever we instantiate such a class we can actually validated for the rules we set up there
with the help of decorators.
Now to get started we just need to import this package here.
So npm install class dash validator dash dash save.
And thereafter we can start right away.
So let me restart the webpage.
Def server and to use this package we have to add a couple of decorators to the models so to the classes
where we want to validate certain properties.
So in my case here in the product model I will import something from class validator.
Now again if you followed through the decorator module in this course this is a concept which are already
also built from scratch there without this package.
But of course we built it in a very naive and basic version with this package.
We don't have to build it on our own and it's all the more elaborate.
Now from class validator we can import certain decorators for example is not empty or also is no.
We can also import is positive now we can add them to the properties we want to validate.
So here for example on the title we can add is not empty as a decorator like this.
And these are all decorator factories.
So you always have to execute them.
And also here is number and is positive.
Let's execute that now to use decorators here.
You all should go to t is confit Jason File and make sure you turn experimental decorators on they're
there after safety's and now these errors here should all go away.
We also need to restart the deaf server though so that it picks up the new TSA conflict Jason version.
So now we add a d rules D validation rules here.
We're basically saying that title must never be an empty string and the number should be a positive
number.
Now we can create a new product here.
Let's say their new product where we call new product and I pass an empty string for the title and then
minus five of ninety nine for the number.
If I then console lock new prot Get in formation and execute this.
Let's see whether it works.
Play it safe that huh.
It works well we're not entirely there yet with our decorators.
We set up the the rules but these decorators on their own don't do much.
Instead we now also have to import the validated method from class validator.
So from this package we import validate and we have to execute this on a concrete instance of the class
where we added our decorators too.
So in this case here we can run validate and parse the new prod to it and validate will then return
a promise which actually might yield validation errors so we can then add then here if you were in a
async function you could all use a single weight of course.
And here we get back to errors this validate function finds no important.
We always end up and then even if there are errors.
So if there are validation errors we don't get into some catch block.
We always go into the then block.
Now in here we can now check if errors which is an array dot length is greater than zero which means
there are validation errors in this case we can lock validation errors and maybe also output these errors
we found.
So console log errors and otherwise in my case here I will lock the information.
Now of course it depends on the application you're building what you want to do here.
You might want to show an alert to the user in this case here if there are errors you might want to
reset the input whatever you want to do here.
I'll just log something to the console and if we now save that while you see validation errors and then
here and there array of the errors that were found for example the first error is on the pile property
that it should not be empty and the second error is on the price property that it must be a positive
number.
And this is how easily you can add quite powerful validation to your projects with this decorator based
approach related to what we did earlier in the course and the decorators module.
But as I mentioned multiple times more elaborate with a bunch of built in rules and you can learn all
about that of course in the docs of this package and without reinventing the wheel.
So that a class validate or package really is a powerful package you should be aware of if you're building
bigger projects with typescript.