Using FluentValidation
FluentValidation
gives you full control when creating data validation. Hence, it is very useful for all validation scenarios:
- Let's add the
FluentValidation
package to our application:dotnet add package FluentValidation
This preceding command installs the popular validation library for .NET. The package uses a fluent interface to build strongly typed rules.
- The following command installs the dependency injection extensions for
FluentValidation
:dotnet add package FluentValidation.DependencyInjectionExtensions
Now we can create another behavior, and this will be for validation. The following code validates the requests inside PipeLineBehavior
by using IValidator
, which defines a validator for a specific type, and ValidationContext
, which creates an instance of the new validation context, from the FluentValidation
namespace:
// ValidationBehavior.cs
using FluentValidation; using MediatR; using ValidationException = Travel.Application...