FluentValidationInterceptor 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package FluentValidationInterceptor --version 1.0.1
                    
NuGet\Install-Package FluentValidationInterceptor -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FluentValidationInterceptor" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FluentValidationInterceptor" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="FluentValidationInterceptor" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FluentValidationInterceptor --version 1.0.1
                    
#r "nuget: FluentValidationInterceptor, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FluentValidationInterceptor@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FluentValidationInterceptor&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=FluentValidationInterceptor&version=1.0.1
                    
Install as a Cake Tool

FluentValidationInterceptor

A NuGet package for automating field validation in ASP.NET Core API or MVC applications using FluentValidation. This package simplifies the validation process by automatically intercepting requests and validating them according to rules defined in FluentValidation validators. No manual validation code is required in each controller or action.

Features

  • Automatic validation of models and DTOs configured with FluentValidation
  • Eliminates repetitive validation code in each endpoint method
  • Automatic error responses in JSON format when validation fails
  • Easy configuration with single UseFluentValidationInterceptor and AddFluentValidationInterceptor methods
  • Compatible with ASP.NET Core Web API and MVC

Installation

Install the package via NuGet Package Manager or CLI:

dotnet add package FluentValidationInterceptor

The package will automatically install these required dependencies:

FluentValidation

FluentValidation.DependencyInjectionExtensions

Microsoft.AspNetCore.Http

Microsoft.AspNetCore.Mvc.Core

Quick Start

  1. Configure Services (Program.cs)
var builder = WebApplication.CreateBuilder(args);

// Add services to the container
builder.Services.AddControllers();

// Register RequestValidation (automatically finds all validators)
builder.Services.AddRequestValidation();

var app = builder.Build();

  1. Configure Middleware (Program.cs)

// Configure the HTTP request pipeline ! important
app.UseRouting();

// Use RequestValidation middleware
app.UseRequestValidation();

app.UseAuthorization();
app.MapControllers();
app.Run();

Usage Example

  1. Create a DTO

public class CreateUserDto
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Email { get; set; }
}
  1. Create a Validator
public class CreateUserValidator : AbstractValidator<CreateUserDto>
{
    public CreateUserValidator()
    {
        RuleFor(x => x.Name)
            .NotEmpty().WithMessage("Name is required.")
            .MaximumLength(50).WithMessage("Name cannot exceed 50 characters.");

        RuleFor(x => x.Age)
            .GreaterThan(0).WithMessage("Age must be greater than 0.")
            .InclusiveBetween(18, 99).WithMessage("Age must be between 18 and 99.");

        RuleFor(x => x.Email)
            .NotEmpty().WithMessage("Email is required.")
            .EmailAddress().WithMessage("A valid email address is required.");
    }
}
  1. Create a Controller

[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
    [HttpPost]
    public IActionResult CreateUser([FromBody] CreateUserDto userDto)
    {
        // No manual validation needed - automatically validated by middleware
        return Ok(new { Message = "User created successfully", User = userDto });
    }
}

Automatic Validation Response

When validation fails, the middleware returns a structured JSON response:

{
  "title": "Validation errors occurred",
  "status": 400,
  "detail": "Please correct the specified errors and try again.",
  "instance": "/api/users",
  "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
  "errors": {
    "Name": ["Name is required."],
    "Age": ["Age must be greater than 0.", "Age must be between 18 and 99."],
    "Email": ["A valid email address is required."]
  }
}

Supported HTTP Methods

The middleware automatically validates these HTTP methods:

  • POST
  • PUT
  • PATCH

Benefits

  • Automation: No manual ModelState checks in controllers
  • Clean Code: Eliminates repetitive validation code
  • Consistency: Standardized validation responses across all endpoints
  • Scalability: Easily add new validations by creating new validators
  • Multi-assembly Support: Automatically discovers validators across all - - - referenced projects

Dependencies

  • This package automatically includes:
  • FluentValidation (≥11.9.0)
  • FluentValidation.DependencyInjectionExtensions (≥11.9.0)
  • Microsoft.AspNetCore.App (shared framework)
  • Microsoft.Extensions.DependencyInjection (≥8.0.0)

Troubleshooting

  • Validators Not Found
  • Ensure your validators are in assemblies that are loaded at runtime.
  • The middleware automatically scans all available assemblies.

Validation Not Triggering

  • Ensure DTOs are decorated with [FromBody] attribute
  • Check that HTTP method is POST, PUT, or PATCH
  • Verify validators are properly registered

License

MIT License - see LICENSE file for details.

Support

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 209 9/7/2025
1.0.1 175 9/6/2025
1.0.0 135 9/5/2025

- Changed method names from AddFluentValidationInterceptor to AddRequestValidation
 - Changed method names from UseFluentValidationInterceptor to UseRequestValidation
 - Backward compatible with previous version
 - Improved method naming for better readability