# FluentValidation Plugin for SlimMessageBus Please read the [Introduction](intro.md) before reading this provider documentation. - [Introduction](#introduction) - [Configuration](#configuration) - [Configuring FluentValidation](#configuring-fluentvalidation) - [Custom exception](#custom-exception) - [Producer side validation](#producer-side-validation) - [Consumer side validation](#consumer-side-validation) - [Configuring without MSDI](#configuring-without-msdi) ## Introduction The [`SlimMessageBus.Host.FluentValidation`](https://www.nuget.org/packages/SlimMessageBus.Host.FluentValidation) introduces validation on the producer or consumer side by leveraging the [FluentValidation](https://www.nuget.org/packages/FluentValidation) library. The plugin is based on [`SlimMessageBus.Host.Interceptor`](https://www.nuget.org/packages/SlimMessageBus.Host.Interceptor) core interfaces and can work with any transport including the memory bus. See the [full sample](/src/Samples/Sample.ValidatingWebApi/). ## Configuration Consider the following command, with the validator (using FluentValidation) and command handler: ```cs public record CreateCustomerCommand : IRequest { public string? FirstName { get; set; } public string? LastName { get; set; } public string? Email { get; set; } public string? Phone { get; set; } } ``` ```cs public class CreateCustomerCommandValidator : AbstractValidator { public CreateCustomerCommandValidator() { RuleFor(x => x.FirstName).NotEmpty(); RuleFor(x => x.LastName).NotEmpty(); RuleFor(x => x.Email).NotEmpty(); RuleFor(x => x.Phone).NotEmpty().Length(6).When(x => x.Phone != null); } } ``` ```cs public class CreateCustomerCommandHandler : IRequestHandler { public Task OnHandle(CreateCustomerCommand command, CancellationToken cancellationToken) { return Task.FromResult(new CreateCustomerCommandResult(Guid.NewGuid())); } } ``` ### Configuring FluentValidation Consider an in-process command that is delivered using the memory bus: ```cs var builder = WebApplication.CreateBuilder(args); builder.Services.AddSlimMessageBus(mbb => mbb .WithProviderMemory() .AutoDeclareFrom(Assembly.GetExecutingAssembly()) .AddAspNet() .AddFluentValidation(cfg => { // Configure SlimMessageBus.Host.FluentValidation plugin cfg.AddProducerValidatorsFromAssemblyContaining(); // You can map the validation errors into a custom exception //cfg.AddValidationErrorsHandler(errors => new ApplicationException("Custom Validation Exception")); })); // FluentValidation library - find and register IValidator implementations: builder.Services.AddValidatorsFromAssemblyContaining(); ``` #### Custom exception By default `FluentValidation.ValidationException` exception is raised on the producer and consumer when validation fails. It is possible to configure custom exception (or perhaps to suppress the validation errors): ```cs builder.Services.AddSlimMessageBus(mbb => { mbb.AddFluentValidation(opts => { // SMB FluentValidation setup goes here opts.AddValidationErrorsHandler(errors => new ApplicationException("Custom exception")); }); }); ``` #### Producer side validation The `.AddProducerValidatorsFromAssemblyContaining()` will register an SMB interceptor that will validate the message upon `.Publish()` or `.Send()` - on the producer side before the message even gets deliverd to the underlying transport. Continuing on the example from previous section: ```cs builder.Services.AddSlimMessageBus(mbb => { mbb.AddFluentValidation(opts => { // Register validation interceptors for message (here command) producers inside message bus // Required Package: SlimMessageBus.Host.FluentValidation opts.AddProducerValidatorsFromAssemblyContaining(); }); }); ``` For example given an ASP.NET Minimal WebApi, the request can be delegated to SlimMessageBus in memory transport: ```cs // Using minimal APIs var app = builder.Build(); app.MapPost("/customer", (CreateCustomerCommand command, IMessageBus bus) => bus.Send(command)); await app.RunAsync(); ``` In the situation that the incoming HTTP request where to deliver an invalid command, the request will fail with `FluentValidation.ValidationException: Validation failed` exception. For full example, please see the [Sample.ValidatingWebApi](../src/Samples/Sample.ValidatingWebApi/) sample. #### Consumer side validation We can also enable validation of the incoming message just before it gets delivered to the respective `IConsumer` or `IRequestHandler` - on the consumer side. Such validation would be needed in scenarios when an external system delivers messages onto the transport (Kafka, Azure Service Bus) which we do not trust, and therefore we could enable validation on the consumer end. This will prevent the invalid messages to enter the consumer or handler. ```cs builder.Services.AddSlimMessageBus(mbb => { mbb.AddFluentValidation(opts => { // Register validation interceptors for message (here command) consumers inside message bus // Required Package: SlimMessageBus.Host.FluentValidation opts.AddConsumerValidatorsFromAssemblyContaining(); }); }); ``` In the situation that the message is invalid, the message will fail with `FluentValidation.ValidationException: Validation failed` exception and standard consumer error handling will take place (depending on the underlying transport it might get retried multiple times until it ends up on dead-letter queue). ### Configuring without MSDI If you are using another DI container than Microsoft.Extensions.DependencyInjection, in order for the `SlimMessageBus.Host.FluentValidation` plugin to work, you simply need to: - register the FluentValidator `IValidator` validators in the container, - register the respective `ProducerValidationInterceptor` as `IProducerInterceptor` for each of the message type `T` that needs to be validated on producer side, - register the respective `ConsumerValidationInterceptor` as `IConsumerInterceptor` for each of the message type `T` that needs to be validated on consumer side, - the scope of can be anything that you need (scoped, transient, singleton)