Using FluentValidation In .NET Core

This article introduced the basic usages of FluentValidation to build validation rules.


Sometimes, there are many validation rules in our methods, such as the name cannot be null, the age must be greater than 18, etc., and as usual, we may write some code for defining these rules. 
  1. static void Main(string[] args)  
  2. {  
  3.     var addCusDto = new AddCustomerDto();  
  4.     AddCustomer(addCusDto);  
  5.     Console.ReadKey();  
  6. }  
  8. static void AddCustomer(AddCustomerDto dto)  
  9. {  
  10.     if (string.IsNullOrWhiteSpace(dto.Name))  
  11.     {  
  12.         Console.WriteLine($"name cannot be null");  
  13.         return;  
  14.     }  
  16.     if (string.IsNullOrWhiteSpace(dto.Phone))  
  17.     {  
  18.         Console.WriteLine($"phone cannot be null");  
  19.         return;  
  20.     }  
  22.     if (dto.Other <= 0)  
  23.     {  
  24.         Console.WriteLine($"other must great than 0");  
  25.         return;  
  26.     }   
  27.     //save code ...  
  28. }  
However, this way seems not fluent. Here, I will introduce a small validation library named FluentValidation that uses a fluent interface and lambda expressions for building the validation rules.
You can follow this link for more information.
Let's take a look at how to use this library. Before the next section, we need to install FluentValidation via NuGet.
Step 1
Creating the validator.
We need to create a class that inherits from AbstractValidator<T>, where T is the type of class that you wish to validate. For the background example, we can create the following validator.
  1. public class AddCustomerDtoValidator : AbstractValidator<AddCustomerDto>  
  2. {  
  3.     public AddCustomerDtoValidator()  
  4.     {  
  5.         RuleFor(x => x.Name).NotEmpty().WithMessage("name cannot be null");  
  6.         RuleFor(x => x.Phone).NotEmpty();  
  7.         RuleFor(x => x.Other).GreaterThan(0).WithMessage("other must be great than 0");  
  8.     }  
  9. }  
What does this validator do?
  1. The Name property is not empty. If this property is empty, the error message will be "name cannot be null".
  2. The Phone property is not empty. If this property is empty, the error message will be the default value.
  3. The Other property must be greater than 0. If this property is not greater than 0, the error message will be the default value.
Step 2
Using the validator.
  1. static void AddCustomerWithFluentValidation(AddCustomerDto dto)  
  2. {  
  3.     var validator = new AddCustomerDtoValidator();  
  4.     var validRes = validator.Validate(dto);  
  5.     if (!validRes.IsValid)  
  6.     {  
  7.         //first error message  
  8.         Console.WriteLine(validRes.Errors.FirstOrDefault());  
  9.         //Console.WriteLine(validRes.Errors.FirstOrDefault().ErrorMessage);  
  10.         ////all error messages  
  11.         //Console.WriteLine(validRes.ToString(","));  
  12.         //Console.WriteLine(string.Join(",", validRes.Errors.Select(x => x.ErrorMessage)));  
  13.         //Console.WriteLine(string.Join(",", validRes.Errors));  
  14.     }  
  16.     //save code ...  
  17. }  
As you can see, we just created an instance of the validator and called the Validatmethod by passing the dto object which we want to validate.
The Validate method returns a ValidationResult object. It contains two properties, one is IsValid that says whether the validation succeeded or not, while the other one is Errors, that contains the details about any validation failures.
The above sample shows you how to get the first error message and all error messages. You can return the message(s) based on your requirement. We only need two steps to use this library.
There are many useful features of FluentValidation, such as chaining validators.
  1. RuleFor(x => x.Name)
  2.       .NotEmpty()
  3.       .NotEqual("catcher")
  4.       .WithMessage("name cannot be null");  
We can combine lots of validators for a property.

This article introduced a new way to build the validation rules.

I hope this will help you!