Skip to content

Latest commit

 

History

History
95 lines (71 loc) · 2.55 KB

File metadata and controls

95 lines (71 loc) · 2.55 KB

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);