MinApiLib.FluentValidation
8.0.1
See the version list below for details.
dotnet add package MinApiLib.FluentValidation --version 8.0.1
NuGet\Install-Package MinApiLib.FluentValidation -Version 8.0.1
<PackageReference Include="MinApiLib.FluentValidation" Version="8.0.1" />
paket add MinApiLib.FluentValidation --version 8.0.1
#r "nuget: MinApiLib.FluentValidation, 8.0.1"
// Install MinApiLib.FluentValidation as a Cake Addin #addin nuget:?package=MinApiLib.FluentValidation&version=8.0.1 // Install MinApiLib.FluentValidation as a Cake Tool #tool nuget:?package=MinApiLib.FluentValidation&version=8.0.1
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 | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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. |
-
net7.0
- FluentValidation (>= 11.9.2)
-
net8.0
- FluentValidation (>= 11.9.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.