MinApiLib.FluentValidation 9.0.0

dotnet add package MinApiLib.FluentValidation --version 9.0.0                
NuGet\Install-Package MinApiLib.FluentValidation -Version 9.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="9.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MinApiLib.FluentValidation --version 9.0.0                
#r "nuget: MinApiLib.FluentValidation, 9.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.
// Install MinApiLib.FluentValidation as a Cake Addin
#addin nuget:?package=MinApiLib.FluentValidation&version=9.0.0

// Install MinApiLib.FluentValidation as a Cake Tool
#tool nuget:?package=MinApiLib.FluentValidation&version=9.0.0                

license version

MinApiLib.FluentValidation

This package contains extensions to use validation in your endpoints. It uses the FluentValidation library.

Installation

You can install this package using the NuGet package manager:

Install-Package MinApiLib.FluentValidation

Or using the .NET CLI:

dotnet add package MinApiLib.FluentValidation

Usage

To use the validation, you can use the WithValidation extension method:

global using MinApiLib.FluentValidation;

And to create a validatior for your request, you can use the AbstractValidator class from the FluentValidation namespace:

global using FluentValidation;

Now configure the validation filter in your endpoint:

public readonly record struct Request(
    [FromBody] RequestBody Body
);

public readonly record struct RequestBody(
    public string Name,
    public string City,
    public string Country
);

public class RequestValidator : AbstractValidator<Request>
{
    public RequestValidator()
    {
        RuleFor(x => x.Body).NotNull();
        RuleFor(x => x.Body.Name).NotEmpty();
        // ...
    }
}

Then, in your endpoint, use the WithValidation or WithValidation extension method:

public record CreateThing() : PostEndpoint<Request>("things")
{
    protected override RouteHandlerBuilder Configure(RouteHandlerBuilder builder)
        => builder
                .Produces<Response>(StatusCodes.Status201Created)
                .WithName("CreateThings")
                .WithTags("things")
                .WithValidation(); // <------------
                // .WithValidation<Request>(); // <------------ is also valid

    protected override async Task<IResult> OnHandleAsync(Request request, CancellationToken cancellationToken)
    {
        // async stuff
        return Results.Created($"/things/{created.Id}", created);
    }
}

To make the validation work you need to register your validators in the DI container a IValidator<T>:

services.AddValidation();
services.AddSingleton<IValidator<Request>, RequestValidator>();

Or you can use the AddValidatorsFromAssembly extension method to register all validators in the assembly:

services.AddValidatorsFromAssembly();
// or
services.AddValidatorsFromAssembly(typeof(RequestValidator).Assembly);
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. 
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
9.0.0 59 11/13/2024
8.0.1 898 8/30/2024