Fluent Validation Middleware for HTTP
Wolverine.Http has a separate package called WolverineFx.Http.FluentValidation
that provides a simple middleware for using Fluent Validation in your HTTP endpoints.
To get started, install that Nuget reference:
dotnet add package WolverineFx.Http.FluentValidation
Next, let's assume that you have some Fluent Validation validators registered in your application container for the request types of your HTTP endpoints -- and the UseFluentValidation method from the WolverineFx.FluentValidation
package will help find these validators and register them in a way that optimizes this middleware usage.
Next, add this one single line of code to your Wolverine.Http bootstrapping:
opts.UseFluentValidationProblemDetailMiddleware();
as shown in context below in an application shown below:
app.MapWolverineEndpoints(opts =>
{
// This is strictly to test the endpoint policy
opts.ConfigureEndpoints(httpChain =>
{
// The HttpChain model is a configuration time
// model of how the HTTP endpoint handles requests
// This adds metadata for OpenAPI
httpChain.WithMetadata(new CustomMetadata());
});
// more configuration for HTTP...
// Opting into the Fluent Validation middleware from
// Wolverine.Http.FluentValidation
opts.UseFluentValidationProblemDetailMiddleware();
AsParameters Binding
The Fluent Validation middleware can also be used against the [AsParameters]
input of an HTTP endpoint:
public static class ValidatedAsParametersEndpoint
{
[WolverineGet("/asparameters/validated")]
public static string Get([AsParameters] ValidatedQuery query)
{
return $"{query.Name} is {query.Age}";
}
}
public class ValidatedQuery
{
[FromQuery]
public string? Name { get; set; }
public int Age { get; set; }
public class ValidatedQueryValidator : AbstractValidator<ValidatedQuery>
{
public ValidatedQueryValidator()
{
RuleFor(x => x.Name).NotNull();
}
}
}