Cookbook
FluentValidation Integration
In this example we will show how to integrate Immediate.Handlers and Immediate.Apis with FluentValidation, a popular validation library for .NET that can be used as an alternative for Immediate.Validations. This example is particularly useful for projects that already contain a significant amount of FluentValidation code but want to make use of the other ImmediatePlatform libraries.
The example is structured in a tutorial form that will teach you how to add FluentValidation to an exisiting Todo Web API, similar to our Web API cookbook example. The example can be adapted to any other type of application that allows for intercepting exceptions (e.g. Blazor apps, Discord bots and so on).
If you prefer to skip the tutorial and just view the source code for this example, it is hosted on GitHub and can be viewed at any time.
Tutorial
For the purposes of this tutorial, we will assume that you already have a Web API set up with the following endpoint:
Add FluentValidation packages
Add the validation behavior
We will need to add a validation behavior that will run within the Immediate.Handlers pipeline and validate each incoming request (query/command) and throw a
ValidationException
on validation fail that we will later be able to intercept and handle.📝 Note: Although the following example throws a
ValidationException
, it should not be overly difficult to adapt it to use aResult<T>
type from a results library of your choice. As a helpful pointer, to get started you would need to constrain the TResponse to anIResult
.Register the behavior
To register the behavior, either create an
AssemblyAttributes.cs
file at the root of your project if it does not already exist, with the following contents, or add just the ValidationBehavior to your existingAssemblyAttributes.cs
like so:If you don't already have behaviors registered in your
Program.cs
, add the following:Add a validator to the endpoint
Now that we have the behavior infrastructure in place, we can add a FluentValidator validator to our
CreateTodo
endpoint.Register FluentValidation validators
We have to register FluentValidation validators with DI so that they can be injected into the behavior when it runs. We can do this by adding the following to
Program.cs
:This will automatically add all FluentValidation validators contained within the same assembly as
CreateTodo.CommandValidator
using assembly scanning. If yourProgram
class sits within the same assembly as your validators you can also useProgram
as the generic type here.Handle validation failures
The result of a validation behavior running is that when a parameter fails one or more validations, a ValidationException is thrown, which can be handled via ProblemDetails or any other infrastructure mechanism. In the following example we will use
ProblemDetails
as we are in the context of a Web API.Add the following to your
Program.cs
:
Now whenever you make an invalid request to any of the endpoints, a ProblemDetails
detailing all the validation errors will be returned, alongside a 400 Bad Request
status code.