WilderMinds.MetaWeblog 5.1.2
MinimalApis.FluentValidation
To install the package:
dotnet add package MinimalApis.FluentValidation --version 1.0.0-beta
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.
Showing the top 20 packages that depend on WilderMinds.MetaWeblog.
Packages | Downloads |
---|---|
MoongladePure.Web
Nuget package of 'Web' provided by MoongladePure
|
9 |
MoongladePure.Web
Nuget package of 'Web' provided by MoongladePure
|
7 |
MoongladePure.Web
Nuget package of 'Web' provided by MoongladePure
|
6 |
MoongladePure.Web
Nuget package of 'Web' provided by MoongladePure
|
5 |
.NET Standard 2.1
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.0)
- System.Collections (>= 4.3.0)
- System.Reflection.Extensions (>= 4.3.0)
- System.Text.Encodings.Web (>= 8.0.0)
- System.Text.RegularExpressions (>= 4.3.1)
- System.Xml.XDocument (>= 4.3.0)
Version | Downloads | Last updated |
---|---|---|
5.1.3 | 8 | 04/29/2024 |
5.1.2 | 4 | 04/29/2024 |
5.1.1 | 8 | 04/29/2024 |
5.1.0 | 8 | 03/29/2024 |
5.0.0 | 5 | 03/29/2024 |
3.1.1 | 7 | 03/29/2024 |
3.1.0 | 6 | 03/29/2024 |
2.0.1 | 7 | 03/29/2024 |
2.0.0 | 5 | 03/29/2024 |
1.2.3 | 7 | 03/29/2024 |
1.2.2 | 6 | 03/29/2024 |
1.2.1 | 6 | 03/29/2024 |
1.2.0 | 6 | 03/29/2024 |
1.1.0 | 7 | 03/29/2024 |
1.0.2 | 6 | 03/29/2024 |
1.0.1-rc2 | 6 | 03/29/2024 |
1.0.0-rc2 | 7 | 03/29/2024 |
0.4.0-beta | 6 | 03/29/2024 |
0.3.1-beta | 8 | 03/29/2024 |
0.3.0-beta | 6 | 03/29/2024 |
0.2.6-beta | 5 | 03/29/2024 |
0.2.5-beta | 7 | 03/29/2024 |
0.2.4-beta | 6 | 03/29/2024 |
0.2.3 | 6 | 03/29/2024 |
0.2.2 | 8 | 03/29/2024 |
0.2.1-beta | 5 | 03/29/2024 |
0.1.1-beta | 7 | 03/29/2024 |
0.1.0-beta | 6 | 03/29/2024 |
0.0.3-beta | 6 | 03/29/2024 |
0.0.2-beta | 6 | 03/29/2024 |
0.0.1-alpha | 7 | 03/29/2024 |