Thank you , 8000 awesome people
I am thrilled to announce I’ve hit 8K subscribers on my newsletter, it was not possible without your love and support. Thanks a million to each one of you.
As a token of appreciation, I’ve launched my YouTube Channel. Stay tuned for more valuable content and don’t forget to Subscribe , 800+ people have already subscribed it.
In today’s newsletter we are going to discuss:
Why we need validation
How to validate
Introduction of library
How to implement it
GitHub Code [FREE]
Why we need validation
Data validation is vital in development to maintain data integrity. By verifying information for accuracy (e.g., age not being 99999), we ensure our databases store only valid data, enhancing overall system reliability.
How to validate
We can use data annotations for validations, write all custom code of validation or use some third party library which can do the job.
Using data annotations can make our code bloated, writing all custom validation stuff could be time taking, that is where Fluent Validation comes into party.
Introduction of Fluent Validation
FluentValidation is a .NET library (with 116M downloads) for building strongly-typed validation rules. It supports multiple platforms (.NET Standard 2.0, .NET Core 3.1, .NET 5,6,7)
How to use FluentValidation
1/ Install following Nuget Package by using Nuget Package Manager or command
install-package FluentValidation.AspNetCore
2/ Create your validator by inheriting from AbstracyValidator
generic class and pass it your DTO/Request class
3/ Add rules for your properties by using built in methods.
4/ To use this validator first we need to tell our .NET API about it. Add following line in your Program.cs
These two lines will search all validators in our assembly and register them, although we have few more ways as well to register it , you can read them here
5/ To use our validator inject IValidator<T>
in constructor of desired class and we are good to go.
6/ Call validate method and pass it your DTO and then by accessing its IsValid
property we can verify if it was proper data or not.
This was just basic overview of how to use this library in .NET Core, it comes with a variety of other features :
- Localization
- Define rules
- Async validation
- Apply set of rules
- Set severity levels
- Custom error codes
- Validate specific properties
- Built in and custom validators
You can read in detail about them at Official Docs I am big fan of their documentation , really simple and easy to understand.
GitHub Code of Demo
You can get code of Fluent Validation with implementation at this GitHub Repository
Whenever you’re ready, there are 4 ways I can help you
Promote yourself to 8000+ subscribers by Sponsoring my Newsletter
Become a Patron and get access to 140+ .NET Questions and Answers
Get a FREE eBook from Gumroad that contains 30 .NET Tips (2900+ downloads)
Subscribe my YouTube Channel where I would be publishing .NET stuff.
Special Offers 📢
Ultimate ASP.NET Core Web API Second Edition - Premium Package
10% off with discount code: 9s6nuez