Fluent Validation in Asp.net Web API

Fluent Validation in Asp.net Web API

Introduction

Fluent validation is a validation library for .Net applications that allows developers to implement validation rules.

In this article, we'll explore how to use Fluent Validation with Asp.net Web API to validate HTTP POST requests.

Install FluentValidation Nuget Package

Install the .Net library from the Nuget package manager or from the dotnet CLI.

Tools →Nuget Package Manager → Manage Nuget Packages For Solution dotnet add package FluentValidation.AspNetCore

Create a Model Class

This model class acts as a representation of the data structure in the database. Our model class --> Transaction.js

public class Transaction
    {
        public Guid Id { get; set; } = Guid.NewGuid();  
        public string TransactionName { get; set; }

        public string VendorName { get; set; } 

        public int TransactionCost { get; set; } 

        public int TransactionCostCharges { get; set; }
    }

The Validator Class

public class TransactionValidator:AbstractValidator<Transaction>
 {
     public TransactionValidator()
     {
       RuleFor(exp => exp.TransactionName).NotEmpty().MinimumLength(5).MaximumLength(20);
            RuleFor(exp => exp.TransactionName).Must(exp => !exp.Any(char.IsDigit)).WithMessage("Name should not Contain any Numbers");

     }
 }

Fluent Validation provides a base class AbstractValidator that provides a set of methods and properties that implements the validation rules that can be inherited and extended.

public class CreatedClass:AbstractValidator<T>

<T> is the model class created above that we are consuming.

public class CreatedClass:AbstractValidator  and public class TransactionValidator:AbstractValidator

Example 1:

RuleFor(exp => exp.TransactionName).NotEmpty().MinimumLength(5).MaximumLength(20);

RuleFor is one of the methods that implements validation rules for a particular property. In this Example the RuleFor validates the TransactionName Property it should not be Empty, with a length between 5 and 20.

Example 2:

RuleFor(exp => exp.TransactionName).Must(exp => !exp.Any(char.IsDigit)).WithMessage("Name should not Contain any Numbers");

The TransactioName should not contain Numbers and the custom message --> WithMessage("Name should not Contain any Numbers") is shown.

There are many Expressions you could choose from refer to the following fluent validation documentation https://docs.fluentvalidation.net/en/latest/index.html

Configure the Validator Class

Register the TransactionValidator in the program.cs class to make it available for dependency injection in other classes.

Dependency injection -->

A software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies

builder.Services.AddScoped<TransactionValidator>()

The Controller

Create a new Controller TransactionController.

public class TransactionController : ControllerBase
 {
        public Transactiondb Transactiondb { get; }
        public TransactionValidator Validations { get; }

        public TransactionController(Transactiondb transactiondb ,TransactionValidator T_validations)
        {
            Transactiondb = transactiondb;
            Validations = T_validations;
        }
}

The TransactionController Extends the ControllerBase class. There are two properties the Transactiondb class and the TransactionValidator class In the constructor of TransactionController both Transactiondb and TransactionValidator are registered or injected.

Add the HTTP Post method_

    [HttpPost]
 public async Task<IActionResult> AddNewTransaction([FromBody] Transaction transaction)
       {

            var validationResult = await Validations.ValidateAsync(transaction);
            if (!validationResult.IsValid) 
            {
                return   BadRequest(validationResult.Errors);
            }

            var newTransaction = new Transaction
            {
                Id = Guid.NewGuid(),
                TransactionName = transaction.TransactionName,
                TransactionCost = transaction.TransactionCost,
                VendorName = transaction.VendorName,    
                TransactionCostCharges= transaction.TransactionCostCharges, 
            };

            await Transactiondb.AddAsync(newTransaction);
            await Transactiondb.SaveChangesAsync();
           return Ok("Transaction added Successfully");;

       }

When the client adds a new transaction the validate asyncmethod from the TransactionValidator performs the validations created on the transaction added.

The validationResult holds the results. If the validationResult is invalid method returns a BadRequest response with the error message in the response body.

If the validation is successful the transaction is added to the database and a Okresponse to the client with a message Transaction added Successfully.

Testing With Postman

Example 1: Let's Clear the TransactionName

HTTP Post Image with Transaction Name empty and an bad request is thrown

A 400 Bad request error Code is thrown with Error Messages :Transaction Name' must not be empty and The length of 'Transaction Name' must be at least 5 characters. You entered 0 characters shown.

Example 2: Lets Add a TransactionName with Numbers.

HTTP Post Transaction Name with numbers  and a bad request is thrown  showing the custom Error message

Error Message : Name should not Contain any Numbers

Valid data:

Http Post with Vaklid data with message Transaction has been added successfully

The 200 OK status code meaning the Transaction has been added to the database.

Conclusion

Validation is very important in API development It helps to ensure that data is valid and secure.FluentValidation has made API validation easy to implement making it a very powerful validation tool.