Data Validation is a key feature of any Web Application. Validating properly to the UI component helps us in avoiding error prone data and gives the user proper information regarding what they are supposed to enter. In Web Applications, it avoids different security threats like SQL injections and script injections.
We already know that we have different validation frameworks given by .NET framework, we can validate the UI components, using JavSscript, jQuery, in client side.
In ASP.NET MVC, Data Annotation validation is used to validate the page. It is easy to use the validation rules and pass the error message to the UI fields. The problem in it is the validation rules, which are attached to the view model.
In a large ASP.NET MVC Application, we separate the Entity from View Model. The domain entities are related to all the Applications but View Models are specific to an Application. We practically use the fields, which are needed in the Application and not the total domain entity. We use Automapper to map domain entity to view model to make the View Model clean and simple. When we put validation condition in the View Model, it becomes complicated.
If the Application is a large scale enterprise Application, we have messed up the validations and model in a single class. Here, we are violating the design principle separation of the concern and single responsibility principle. This causes a problem with a test driven development promise to the customer and all our code is not covered in the test.
To overcome this type of problem, we have an option, which is a third party Validation framework called Fluent validation. We can separate the validation rules completely from the underlying model. Fluent validation library provides some easy way to unit test the validation rules; it will be helpful, when we need to inject dependencies into our validation rules.
For example, we can read some information from the database or external sources to validate UI fields and show the error message to the users. When there is a need of separating the validation logic from Model property, keep the design rules of single responsibility and separation of concerns. Fluent validations are easy as far as unit testing is concerned and we can inject the dependency or we can make a database call or any Service call to validate a property.
It supports a lot of built-in rules, error messages localization, using object data in the error messages, custom validation methods and conditional validation - applying some rules, if the object data matches a condition, rule sets - apply named set of rules, validation of aggregated objects, collections and property names would be compatible with ASP.NET MVC property names and so on.
It gives the better control validation rules, separates the validation from my View Models and makes Unit testing easier and a clean separation.
Let us create a project and understand in detail about Fluent validation.
Create the EmployeeModel, as shown below-
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace FluentValidationDemo.Models
- {
- public class EmployeeModel
- {
- public int EmployeeId { get; set; }
- public string EmployeeName { get; set; }
-
- public string DepartmentNo { get; set; }
- public string Address { get; set; }
- public int Salary { get; set; }
-
- public string Email { get; set; }
- }
- }
Create a Class named EmployeeValidator and the code for EmployeeValidator, as shown below-
- using FluentValidation;
- using FluentValidationDemo.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace FluentValidationDemo
- {
- public class EmployeeValidator : AbstractValidator<EmployeeModel>
- {
- public EmployeeValidator()
- {
-
- EmployeeEntities e = new EmployeeEntities();
-
-
- RuleFor(x => x.EmployeeId).NotEmpty().WithMessage("EmployeeId is required");
- RuleFor(x => x.EmployeeName).NotEmpty().WithMessage("EmployeeName is required");
-
- EmployeeEntities e = new EmployeeEntities();
- Custom(model =>
- {
- var employeevalidation = e.Employees.FirstOrDefault(emp => emp.EmployeeName == model.EmployeeName);
- if (employeevalidation != null)
- {
- return new FluentValidation.Results.ValidationFailure("EmployeeName", model.EmployeeName+"is a Existing employee.");
- }
-
- return null;
- });
-
- RuleFor(x => x.EmployeeName).Length(10,20).WithMessage("Name should be between 10 and 20 characters");
- RuleFor(x => x.DepartmentNo).NotEmpty().WithMessage("DepartmentNo is not valid");
-
- RuleFor(x => x.Address).NotEmpty().WithMessage("Address is required");
- RuleFor(x => x.Salary).NotEmpty().WithMessage("Salary is not valid");
-
- RuleFor(x => x.Email)
- .Matches(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$").WithMessage("Not a valid email")
- .NotEmpty();
- Custom(model=>
- {
- var employeevalidation=e.Employees.FirstOrDefault(emp=>emp.Email==model.Email);
- if(employeevalidation!=null)
- {
- return new FluentValidation.Results.ValidationFailure("Email", "Email is already in Use Please try another EMail ");
- }
-
- return null;
- });
-
- }
- }
- }
EmployeeValidator class takes EmployeeModel as an input: AbstractValidator<EmployeeModel>. Here, we are applying different validation rules like required fields, length, range, regular expression for EmployeeModel.
-
- RuleFor(x => x.EmployeeId).NotEmpty().WithMessage("The EmployeeId field is required");
-
- RuleFor(x => x.EmployeeName).NotEmpty().WithMessage("EmployeeName is required");
We can validate the data from the database Server and check certain values before the user saves those values. For example, Email or username, which can be a duplicate can be avoided, using a database call in a Fluent validation rules, which are given below-
- EmployeeEntities e = new EmployeeEntities();
- Custom(model =>
- {
- var employeevalidation = e.Employees.FirstOrDefault(emp => emp.EmployeeName == model.EmployeeName);
- if (employeevalidation != null)
- {
- return new FluentValidation.Results.ValidationFailure("EmployeeName", model.EmployeeName + "is a Existing employee.");
- }
- return null;
- });
Here, we are calling EmployeeEntities, which is an entity from a employee table from the database.
Hope you got the information on Fluent validation. Thank you for reading.