MinApiLib.FluentValidation 11.0.0

dotnet add package MinApiLib.FluentValidation --version 11.0.0
                    
NuGet\Install-Package MinApiLib.FluentValidation -Version 11.0.0
                    
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="MinApiLib.FluentValidation" Version="11.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MinApiLib.FluentValidation" Version="11.0.0" />
                    
Directory.Packages.props
<PackageReference Include="MinApiLib.FluentValidation" />
                    
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 MinApiLib.FluentValidation --version 11.0.0
                    
#r "nuget: MinApiLib.FluentValidation, 11.0.0"
                    
#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 MinApiLib.FluentValidation@11.0.0
                    
#: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=MinApiLib.FluentValidation&version=11.0.0
                    
Install as a Cake Addin
#tool nuget:?package=MinApiLib.FluentValidation&version=11.0.0
                    
Install as a Cake Tool

license version

MinApiLib.FluentValidation

Validate requests with the FluentValidation library, including body, query, route and header values.

Installation

dotnet add package MinApiLib.FluentValidation

Usage

global using FluentValidation;
global using MinApiLib.FluentValidation;

Register the validators of the assembly as transient services. This also registers the filter:

builder.Services.AddValidatorsFromAssembly();
// or
builder.Services.AddValidatorsFromAssembly(typeof(RequestValidator).Assembly);

Write a validator for the request type and call WithValidation() in the endpoint:

public record struct Request(
    [FromBody] RequestBody Body,
    [FromQuery] int Page = 1
);

public class RequestBody
{
    public string Name { get; set; }
}

public class RequestValidator : AbstractValidator<Request>
{
    public RequestValidator()
    {
        RuleFor(x => x.Body.Name).NotEmpty().Length(3, 100);
        RuleFor(x => x.Page).GreaterThanOrEqualTo(1);
    }
}

public record CreateThing() : PostHandlerAsync<Request>("/things")
{
    protected override RouteHandlerBuilder Configure(RouteHandlerBuilder builder)
        => builder
                .Produces(StatusCodes.Status201Created)
                .WithName("CreateThing")
                .WithValidation();

    protected override async Task<IResult> HandleAsync(Request request, CancellationToken cancellationToken)
    {
        await Task.Delay(10, cancellationToken);
        return Results.Created("/things/1", request.Body);
    }
}

An invalid request gets a 400 with the errors per property, and the handler doesn't run. WithValidation() also declares that 400 in the endpoint metadata.

What gets validated

Call Validates If there is no validator
WithValidation() on an endpoint The first handler parameter with a registered IValidator<T> The endpoint isn't validated, and a warning is logged
WithValidation<T>() on an endpoint The handler parameter of type T The endpoint isn't validated, and a warning is logged
WithValidation() on a group In each endpoint, the first parameter with a validator The endpoint isn't validated
  • Calling WithValidation() on an endpoint without a validator is obsolete, and it will throw in a future version. The warning is logged when the endpoint is built, on the first request at the latest. Check your logs for it, and cover each validation with a test.
  • The validator must target the type the handler receives. With [AsParameters] handlers, that's Request: an AbstractValidator<RequestBody> isn't associated with them.
  • If the filter isn't registered, WithValidation() throws an InvalidOperationException, with or without a validator.
  • The group overload doesn't declare the 400 in the metadata, because it applies to endpoints that may not have a validator.
  • Validators receive the request's CancellationToken.

Manual registration

To register validators one by one, add the filter and your validators:

builder.Services.AddValidationFilter();
builder.Services.AddSingleton<IValidator<Request>, RequestValidator>();

AddValidationFilter accepts the lifetime of the filter, which is Singleton by default. Validators are resolved from the request scope, so they can be scoped.

If the project references FluentValidation.DependencyInjectionExtensions, its AddValidatorsFromAssembly has the same name as the one in this package. Use only one of them per file, or call the method through its class.

Upgrading

AddValidation() is obsolete: use AddValidationFilter(). ASP.NET Core 10 has its own AddValidation(), and calls to it were ambiguous. So on net10.0, the obsolete method requires the lifetime, as in AddValidation(ServiceLifetime.Singleton), and AddValidation() without arguments is the one from ASP.NET Core, which doesn't register this filter.

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 is compatible.  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.  net11.0 is compatible. 
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
11.0.0 100 11/10/2026
10.0.1 1,184 9/14/2026
10.0.0 3,488 11/19/2025
9.0.0 3,644 11/13/2024
8.0.1 2,021 8/30/2024