SharpGrip.FluentValidation.AutoValidation.Endpoints
1.2.0
Prefix Reserved
See the version list below for details.
dotnet add package SharpGrip.FluentValidation.AutoValidation.Endpoints --version 1.2.0
NuGet\Install-Package SharpGrip.FluentValidation.AutoValidation.Endpoints -Version 1.2.0
<PackageReference Include="SharpGrip.FluentValidation.AutoValidation.Endpoints" Version="1.2.0" />
paket add SharpGrip.FluentValidation.AutoValidation.Endpoints --version 1.2.0
#r "nuget: SharpGrip.FluentValidation.AutoValidation.Endpoints, 1.2.0"
// Install SharpGrip.FluentValidation.AutoValidation.Endpoints as a Cake Addin #addin nuget:?package=SharpGrip.FluentValidation.AutoValidation.Endpoints&version=1.2.0 // Install SharpGrip.FluentValidation.AutoValidation.Endpoints as a Cake Tool #tool nuget:?package=SharpGrip.FluentValidation.AutoValidation.Endpoints&version=1.2.0
SharpGrip FluentValidation AutoValidation
Builds
Introduction
SharpGrip FluentValidation AutoValidation is an extension of the FluentValidation (v10+) library enabling automatic asynchronous validation in MVC controllers and minimal APIs (endpoints). The library FluentValidation.AspNetCore is no longer being maintained and is unsupported. As a result, support for automatic validation provided by this library is no longer available. This library re-introduces this functionality for MVC controllers and introduces automatic validation for minimal APIs (endpoints). It enables developers to easily implement automatic validation in their projects.
Installation
Register your validators with the Microsoft DI service container, for instructions on setting that up please see https://docs.fluentvalidation.net/en/latest/di.html.
MVC controllers
For MVC controllers reference NuGet package SharpGrip.FluentValidation.AutoValidation.Mvc
(https://www.nuget.org/packages/SharpGrip.FluentValidation.AutoValidation.Mvc).
using SharpGrip.FluentValidation.AutoValidation.Mvc.Extensions;
builder.Services.AddFluentValidationAutoValidation();
Minimal APIs (endpoints)
For minimal APIs (endpoints) reference NuGet package SharpGrip.FluentValidation.AutoValidation.Endpoints
(https://www.nuget.org/packages/SharpGrip.FluentValidation.AutoValidation.Endpoints).
Enabling minimal API (endpoint) automatic validation can be done on both route groups and routes.
using SharpGrip.FluentValidation.AutoValidation.Endpoints.Extensions;
builder.Services.AddFluentValidationAutoValidation();
var app = builder.Build();
var endpointGroup = app.MapGroup("/some-group").AddFluentValidationAutoValidation();
endpointGroup.MapPost("/", (SomeModel someModel) => $"Hello {someModel.Name}");
app.MapPost("/", (SomeOtherModel someOtherModel) => $"Hello again {someOtherModel.Name}").AddFluentValidationAutoValidation();
Configuration
MVC controllers
Property | Default value | Description |
---|---|---|
DisableBuiltInModelValidation | false |
Disables the built-in .NET model (data annotations) validation. |
ValidationStrategy | ValidationStrategy.All |
Configures the validation strategy. Validation strategy ValidationStrategy.All enables asynchronous automatic validation on all controllers inheriting from ControllerBase . Validation strategy ValidationStrategy.Annotations enables asynchronous automatic validation on controllers inheriting from ControllerBase decorated (class or method) with a FluentValidationAutoValidationAttribute attribute. |
EnableBodyBindingSourceAutomaticValidation | true |
Enables asynchronous automatic validation for parameters bound from the BindingSource.Body binding source (typically parameters decorated with the [FormBody] attribute). |
EnableFormBindingSourceAutomaticValidation | false |
Enables asynchronous automatic validation for parameters bound from the BindingSource.Form binding source (typically parameters decorated with the [FormForm] attribute). |
EnableQueryBindingSourceAutomaticValidation | true |
Enables asynchronous automatic validation for parameters bound from the BindingSource.Query binding source (typically parameters decorated with the [FormQuery] attribute). |
using SharpGrip.FluentValidation.AutoValidation.Mvc.Extensions;
builder.Services.AddFluentValidationAutoValidation(configuration =>
{
// Disable the built-in .NET model (data annotations) validation.
configuration.DisableBuiltInModelValidation = true;
// Only validate controllers decorated with the `FluentValidationAutoValidation` attribute.
configuration.ValidationStrategy = ValidationStrategy.Annotation;
// Enable validation for parameters bound from the `BindingSource.Body` binding source.
configuration.EnableBodyBindingSourceAutomaticValidation = true;
// Enable validation for parameters bound from the `BindingSource.Form` binding source.
configuration.EnableFormBindingSourceAutomaticValidation = true;
// Enable validation for parameters bound from the `BindingSource.Query` binding source.
configuration.EnableQueryBindingSourceAutomaticValidation = true;
// Replace the default result factory with a custom implementation.
configuration.OverrideDefaultResultFactoryWith<CustomResultFactory>();
});
public class CustomResultFactory : IFluentValidationAutoValidationResultFactory
{
public IActionResult CreateActionResult(ActionExecutingContext context, ValidationProblemDetails? validationProblemDetails)
{
return new BadRequestObjectResult(new {Title = "Validation errors", ValidationErrors = validationProblemDetails?.Errors});
}
}
Minimal APIs (endpoints)
using SharpGrip.FluentValidation.AutoValidation.Endpoints.Extensions;
builder.Services.AddFluentValidationAutoValidation(configuration =>
{
// Replace the default result factory with a custom implementation.
configuration.OverrideDefaultResultFactoryWith<CustomResultFactory>();
});
public class CustomResultFactory : IFluentValidationAutoValidationResultFactory
{
public IResult CreateResult(EndpointFilterInvocationContext context, ValidationResult validationResult)
{
var validationProblemErrors = validationResult.ToValidationProblemErrors();
return Results.ValidationProblem(validationProblemErrors, "Some details text.", "Some instance text.", (int) HttpStatusCode.BadRequest, "Some title.");
}
}
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
-
net8.0
NuGet packages (4)
Showing the top 4 NuGet packages that depend on SharpGrip.FluentValidation.AutoValidation.Endpoints:
Package | Downloads |
---|---|
Fluxera.Extensions.Hosting.Modules.AspNetCore.HttpApi
A module that enables HTTP APIs for ASP.NET Core. |
|
GlavLib.App
Package Description |
|
Senlin.Mo
Modular monolith |
|
Pivotte
A tool to help you declare API endpoints for both clients and servers. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.4.0 | 145,370 | 12/17/2023 |
1.3.1 | 10,521 | 10/19/2023 |
1.3.0 | 3,817 | 10/9/2023 |
1.2.0 | 1,054 | 9/22/2023 |
1.1.0 | 665 | 9/6/2023 |
1.0.1 | 232 | 8/21/2023 |
1.0.0 | 464 | 8/19/2023 |
1.0.0-beta3 | 128 | 8/17/2023 |
1.0.0-beta2 | 143 | 8/16/2023 |
1.0.0-beta1 | 138 | 8/16/2023 |