MinimalHelpers.FluentValidation
1.1.7
dotnet add package MinimalHelpers.FluentValidation --version 1.1.7
NuGet\Install-Package MinimalHelpers.FluentValidation -Version 1.1.7
<PackageReference Include="MinimalHelpers.FluentValidation" Version="1.1.7" />
<PackageVersion Include="MinimalHelpers.FluentValidation" Version="1.1.7" />
<PackageReference Include="MinimalHelpers.FluentValidation" />
paket add MinimalHelpers.FluentValidation --version 1.1.7
#r "nuget: MinimalHelpers.FluentValidation, 1.1.7"
#:package MinimalHelpers.FluentValidation@1.1.7
#addin nuget:?package=MinimalHelpers.FluentValidation&version=1.1.7
#tool nuget:?package=MinimalHelpers.FluentValidation&version=1.1.7
Minimal APIs Helpers
A collection of helper libraries for Minimal API projects.
MinimalHelpers.Routing
A library that provides routing helpers for Minimal API projects, enabling automatic endpoint registration using reflection.
Installation
The library is available on NuGet. Search for MinimalHelpers.Routing in the Package Manager GUI or run the following command in the .NET CLI:
dotnet add package MinimalHelpers.Routing
Usage
Create a class to hold your route handler registrations and have it implement the IEndpointRouteHandlerBuilder interface:
public class PeopleEndpoints : MinimalHelpers.Routing.IEndpointRouteHandlerBuilder
{
public static void MapEndpoints(IEndpointRouteBuilder endpoints)
{
endpoints.MapGet("/api/people", GetList);
endpoints.MapGet("/api/people/{id:guid}", Get);
endpoints.MapPost("/api/people", Insert);
endpoints.MapPut("/api/people/{id:guid}", Update);
endpoints.MapDelete("/api/people/{id:guid}", Delete);
}
// ...
}
Call the MapEndpoints() extension method on the WebApplication object in Program.cs before invoking the Run() method:
// using MinimalHelpers.Routing;
app.MapEndpoints();
app.Run();
By default, MapEndpoints() will scan the calling Assembly to search for classes that implement the IEndpointRouteHandlerBuilder interface. If your route handlers are defined in another Assembly, you have two alternatives:
- Use the
MapEndpoints()overload that takes the Assembly to scan as argument - Use the
MapEndpointsFromAssemblyContaining<T>()extension method and specify a type that is contained in the Assembly you want to scan
You can also explicitly decide which types (among those that implement the IEndpointRouteHandlerBuilder interface) you actually want to map, passing a predicate to the MapEndpoints method:
app.MapEndpoints(type =>
{
if (type.Name.StartsWith("Products"))
{
return false;
}
return true;
});
Note These methods rely on Reflection to scan the Assembly and find the classes that implement the
IEndpointRouteHandlerBuilderinterface. This can have a performance impact, especially in large projects. If you have performance issues, consider using the explicit registration method. Moreover, this solution is incompatible with Native AOT.
MinimalHelpers.Routing.Analyzers
A library that provides a Source Generator for automatic endpoints registration in Minimal API projects.
Installation
The library is available on NuGet. Search for MinimalHelpers.Routing in the Package Manager GUI or run the following command in the .NET CLI:
dotnet add package MinimalHelpers.Routing.Analyzers
Usage
Create a class to hold your route handler registrations and have it implement the IEndpointRouteHandlerBuilder interface:
public class PeopleEndpoints : IEndpointRouteHandlerBuilder
{
public static void MapEndpoints(IEndpointRouteBuilder endpoints)
{
endpoints.MapGet("/api/people", GetList);
endpoints.MapGet("/api/people/{id:guid}", Get);
endpoints.MapPost("/api/people", Insert);
endpoints.MapPut("/api/people/{id:guid}", Update);
endpoints.MapDelete("/api/people/{id:guid}", Delete);
}
// ...
}
Note You only need to use the MinimalHelpers.Routing.Analyzers package. With this Source Generator, the
IEndpointRouteHandlerBuilderinterface is auto-generated.
Call the MapEndpoints() extension method on the WebApplication object in Program.cs before the Run() method invocation:
app.MapEndpoints();
app.Run();
Note The
MapEndpointsmethod is generated by the Source Generator.
MinimalHelpers.OpenApi
A library that provides OpenAPI helpers for Minimal API projects.
Installation
The library is available on NuGet. Search for MinimalHelpers.OpenApi in the Package Manager GUI or run the following command in the .NET CLI:
dotnet add package MinimalHelpers.OpenApi
Usage
Extension methods for RouteHandlerBuilder
Often, endpoints have multiple 4xx return values, each producing a ProblemDetails response:
endpoints.MapGet("/api/people/{id:guid}", Get)
.ProducesProblem(StatusCodes.Status400BadRequest)
.ProducesProblem(StatusCodes.Status401Unauthorized)
.ProducesProblem(StatusCodes.Status403Forbidden)
.ProducesProblem(StatusCodes.Status404NotFound);
To avoid multiple calls to ProducesProblem, use the ProducesDefaultProblem extension method provided by the library:
endpoints.MapGet("/api/people/{id:guid}", Get)
.ProducesDefaultProblem(StatusCodes.Status400BadRequest, StatusCodes.Status401Unauthorized,
StatusCodes.Status403Forbidden, StatusCodes.Status404NotFound);
MinimalHelpers.FluentValidation
A library that provides an endpoint filter for Minimal API projects to perform validation using <a href="https://fluentvalidation.net">FluentValidation</a>.
Installation
The library is available on NuGet. Search for MinimalHelpers.FluentValidation in the Package Manager GUI or run the following command in the .NET CLI:
dotnet add package MinimalHelpers.FluentValidation
Usage
Create a class that extends AbstractValidator<TModel> and define the validation rules:
using FluentValidation;
public record class Product(string? Name, string? Description, double? UnitPrice);
public class ProductValidator : AbstractValidator<Product>
{
public ProductValidator()
{
RuleFor(p => p.Name).NotEmpty().MaximumLength(50);
RuleFor(p => p.Description).MaximumLength(500);
RuleFor(p => p.UnitPrice).GreaterThan(0);
}
}
Register validators in the Service Collection:
using FluentValidation;
// Assuming the validators are in the same assembly as the Program class.
builder.Services.AddValidatorsFromAssemblyContaining<Program>();
Use the WithValidation<TModel>() extension method to enable the validation filter:
using MinimalHelpers.FluentValidation;
app.MapPost("/api/products", (Product product) =>
{
// ...
})
.WithValidation<Product>();
If the validation fails, the response will be a 400 Bad Request with a ValidationProblemDetails object containing the validation errors, for example:
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
"title": "One or more validation errors occurred",
"status": 400,
"instance": "/api/products",
"traceId": "00-f4ced0ae470424dd04cbcebe5f232dc5-bbdcc59f310ebfb8-00",
"errors": {
"Name": [
"'Name' cannot be empty."
],
"UnitPrice": [
"'Unit Price' must be greater than '0'."
]
}
}
To customize validation, use the ConfigureValidation extension method:
using MinimalHelpers.Validation;
builder.Services.ConfigureValidation(options =>
{
// If you want to get errors as a list instead of a dictionary.
options.ErrorResponseFormat = ErrorResponseFormat.List;
// The default is "One or more validation errors occurred"
options.ValidationErrorTitleMessageFactory =
(context, errors) => $"There was {errors.Values.Sum(v => v.Length)} error(s)";
});
You can use the ValidationErrorTitleMessageFactory, for example, if you want to localize the title property of the response using a RESX file.
Contributing
The project is constantly evolving. Contributions are welcome. Feel free to file issues and pull requests in the repository, and we'll address them as we can.
| Product | Versions 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. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- FluentValidation (>= 12.1.0)
-
net8.0
- FluentValidation (>= 12.1.0)
-
net9.0
- FluentValidation (>= 12.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on MinimalHelpers.FluentValidation:
| Repository | Stars |
|---|---|
|
marcominerva/SqlDatabaseVectorSearch
A Blazor Web App and Minimal API for performing RAG (Retrieval Augmented Generation) and vector search using the native VECTOR type in Azure SQL Database and Azure OpenAI.
|