Fluent validation in .net

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
fluent validation is a third party library for validating your models in net and it is totally free if you already have used data annotations for validation then you must be aware of validations in net so you might be thinking why do we need fluent validation fluent validation helps you to separate validation logic from your models that makes your code clean so if you have a complex validation logic then you want to define it separately rather than making your model unreadable if you have a small scale projects and does not have any complex validation logic then using data annotation makes more sense and you can use the hybrid validation uh little bit of fluent validation and little bit of data anotations it's totally up to you what suits you best none of them is good or bad it depends on your your requirements so use anything with keep in your mind what is your requirements if you find this video helpful then please hit the like button make sure to subscribe this channel to get more videos like this so let's get it started without any delays let me walk you through this project it is a standard net core API project which is built in net 8 so we are using the latest one net 8 and now let's see what is happening here so here we have defined this person model inside the models folder and it have three properties ID name and email okay so after that we have our controller person controller it is its route and here we have just defined this basic logger setting and here we have this post method add person which is taking this object as a parameter and here we have this try catch block we Define some settings of some lines in uh catch block we are logging theor message here and we are returning 5 uh status code with exception message and here we have just written a mage message added and nothing else there is no logic we are just focusing on the validation so there won't be any logic so it's just a simple basic project we have one uh model and we have one controller so I have already done some of this code so that we can save some time now what we have to do just right click on this project fluent value validation demo and uh click on manage new get packages and here click on the browse and just type fluent validation. aspet core so we will have a package okay so this one fluent validation. aspet core uh by Jeremy Skinner so this version is latest one so I will be using it click on install and we will have a popup message then Okay I accept that's it and we have installed this package and we do not need any more package so just close these guys and now we will create a folder which which is validators validators and inside we will create a class person validators so let's define a class here name person validators person validator okay now we have this class and I'm going to put a semicolon here so that we can remove these extra braces we will inherit a class name abstract validator and we will type here person and control dot okay so we are using these two name spaces here first one is fluent validation and another one is uh fluent validation Dem Mo which is our project name and do models okay now we will type here c r press enter it will create a Constructor for us and here we will write rule 4 and inside it let's type pp. name and not empty and one more let's apply one more rule here Max L L okay max length uh let's TR it to 20 okay we will Define another rule here so let's do it for email and one more validation which is email address that's it and let's change it to 50 okay now we have defined these two rules here now open the program. Cs file and here we will do some configuration so right here add prent validation and let's type it V V Dot and register validators from assembly containing uh here here we will pass person that's it and we are good to go okay so we have used these two name spaces here fluent validation. ASP netcore and this one for our model so that's it and now we will open our controller okay so we are not going to do anything here let's run this project and okay so let's let's click on this try out and let's uh just type here null and here null Also let's click on execute button and you can see we have to validation message name must not be empty email must not be empty so what if I just change it back to this and let's type here something like this and let's click on this and let's see the email is not valid email address so we are going to pass uh something like this okay so here you can see we have now a two success status code and here we have this message okay now let's see how we can pass regular Expressions here so just go here in the person class and let's add another property here and let's name it uh password string password and okay now uh here in the person validator we will Define one more rule so just type here rule 4 PP do password and not empty okay and matches and here we will pass our regular expression and we can also pass some custom message with message so here we will write must contain a numeric value okay so let's run this thing so now you can see that we have this password and let's click on execute button must contain a numeric value so what if I type a numeric value here and click on execute and you will see now we have okay not a valid emails so his string at the red numeric do YY okay now we we are fine so this is how we can use fluent validation so if you want to go on some details of fluent validation let's type here fluent validation you will find the official documentation of fluent validation so it is the link fluent validation. net you can go there and you can research more about fluent validation so if you find this video helpful then please hit the like button make sure to subscribe this channel to get more videos like this that's it for now see you next time
Info
Channel: Ravindra Devrani
Views: 613
Rating: undefined out of 5
Keywords:
Id: nmLKTRAbdEY
Channel Id: undefined
Length: 10min 45sec (645 seconds)
Published: Mon Dec 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.