FluentValidation in swagger web api | Validate the user inputs in Asp.net Core Web API 6.0

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey i'm gonna tell you about how to use the fluent validation in your asp.net core web api project so you can validate here the user inputs so the package which you need to install for your fluent validation is fluentvalidation.asp.com and we have a different type of dependent different type of ways to register the dependency for fluent validation uh we can do it manually as well as automatic if you're doing the manually then you have to manually inject the register without dependencies into by using the add transient method and the eye validator interface where we need to define the validator class and the model which is student for example and in the automatic registering of the validators we can achieve it by using the register validators from assembly containing and register validated from assemblies so let's move to the coding part so we can achieve the fluent validation in asp.net core web api here is our uh uh solution for a baseball card called ibp first let's install the package which is fluent validation dot asp.net core let's install this oh i have already installed it so you can install this into your solution once it is installed then we have to create the validator for our api response object request object i'm creating a new folder for containing all the validators i have a student controller so i'm making a student validator in student validator it has to be inherited with the abstract validator with the class which we want to validate for example i have student class yeah inside student class we i have these properties first name last name email and age so let's make the constructor and inside constructor we can define the rules for our model object which is student and for example we have first name so i can apply for example it must not be empty and next we can define for email it has to be email address so i have defined the two rules one for first name and email for the student object let's create one endpoint in student controller creator update student somebody we have a student it has to be http post display name could be anything yeah here we perform the logic to create or update i'm not creating here so i'm just making an object of it example response object and just returning it name which is our student dot first name and student dot last name and email which is student.email so i'm returning the object okay with ok response our endpoint is ready let's register this fluent validation into our program file so if we have a asp.net core web api project so we can easily register after the ad and after the ad controllers for example add fluent validation it has x some action methods action properties like x dot include implicitly validate all the child properties by default it is false let's make it true and we have different type of ways for example first manually i can register the myval data like example add transient we have to use the ival data class inside i will i will letter of fluent validation inside i validator you have to define the class which is going to be validated and the validator name which is going to be perform the validation so it is done we can register the fluent validator manually so our student class student valuator and the endpoint is ready let's run this so our swagger open let's see whether our api endpoint well it's the input or not let's make it empty and execute so our api is validating the inputs for example first name first name must not be empty and the email address is not a valid email address so fluent validation is working by using the manual registration of weld data let's try the another automatic resistor comment this out and using the automatic register method register the validators x start register validated from assemblies i am welded registering the well data from assembly so i am using the abdomen.com domain dot get assemblies it will get all the assemblies which is derived from abstract validators so it's all done let's run that again so let's check it out again by registering by using the automatic registration empty the first name and try to execute the api yeah again you can see it is working uh now we have used the automatic registration of by using the assemblies let's check out one more thing whether our api is returning the right result or not student email could be student at the rate gmail.com h could be 20. let's execute this so here you can see we get the result return the name and the email name and side name we concatenate the first name and the last name so it's working as expected so if you find this video helpful then please like and subscribe to my channel thank you
Info
Channel: Aone Coding
Views: 4,353
Rating: undefined out of 5
Keywords: FluentValidation, Custom Validators, FluentValidation .NET, Fluent Validation In ASP.NET MVC, Using Fluent Validation in ASP.NET Core, fluent validation in c#, fluent validation example, fluent validation in asp.net core web api, fluent validation in asp.net 6.0, How to Use FluentValidation in ASP.NET Core, ASP.NET Core FluentValidation, validate user input in asp.net core, validate user input in web api, validate user inputs in .net, Fluent validation in swagger
Id: qbn1Xu6C3L0
Channel Id: undefined
Length: 13min 30sec (810 seconds)
Published: Sat Apr 02 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.