MinimalApis.FluentValidation 1.0.0-beta

This is a prerelease version of MinimalApis.FluentValidation.
There is a newer version of this package available.
See the version list below for details.
dotnet add package MinimalApis.FluentValidation --version 1.0.0-beta                
NuGet\Install-Package MinimalApis.FluentValidation -Version 1.0.0-beta                
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="MinimalApis.FluentValidation" Version="1.0.0-beta" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MinimalApis.FluentValidation --version 1.0.0-beta                
#r "nuget: MinimalApis.FluentValidation, 1.0.0-beta"                
#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 MinimalApis.FluentValidation as a Cake Addin
#addin nuget:?package=MinimalApis.FluentValidation&version=1.0.0-beta&prerelease

// Install MinimalApis.FluentValidation as a Cake Tool
#tool nuget:?package=MinimalApis.FluentValidation&version=1.0.0-beta&prerelease                

MinimalApis.FluentValidation

Build and Test NuGet Nuget

I'm a big fan of how Fluent Validation works, but as I was teaching Minimal APIs - it was tedious to add validation. In .NET 7, Microsoft introduced Endpoint Filters as a good solution.

Since then, I've been using endpoint filters to execute Fluent Validation's Validators. The result is this library. Assuming you've already used Fluent Validation to create your validators, you can simply call Validate to run the validation. For example:

app.MapPost("/test", (SomeModel model) => Results.Created("/test/1", model))
  .Validate<SomeModel>();

The call to Validate is an extension method that works on MapGroup, MapPost, MapPut, and MapDelete. (I don't prevent it's usage on MapGet or MapDelete, but they don't really make sense there.

Essentially, the call to validate will search for a Validator class for the provided type (TModel). If it doesn't find it, it will throw an exception. If it does find the validator, it will find find any parameters that match the provided type and validate it using the validator.

Finally, if there are any validation errors, it returns a ValidationProblem:

Results.ValidationProblem(...);

Note, the validators are called asynchronously.

You can also use the endpoint filter without the extension method if you like:

app.MapPut("/test", (SomeModel model) => Results.Ok(model))
  .AddEndpointFilter<ValidationEndpointFilter<SomeModel>>();

This might be useful if you need to subclass the ValidaitonEndpointFilter.

Please submit any issues and/or pull requests if you have questions or problems.

Product 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. 
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.0 610 4/21/2024
1.0.0-beta 176 1/27/2024

First version.