FluentValidation Plugin for SlimMessageBus
Please read the Introduction before reading this provider documentation.
Introduction
The SlimMessageBus.Host.FluentValidation
introduces validation on the producer or consumer side by leveraging the FluentValidation library.
The plugin is based on SlimMessageBus.Host.Interceptor
core interfaces and can work with any transport including the memory bus.
See the full sample.
Configuration
Consider the following command, with the validator (using FluentValidation) and command handler:
public record CreateCustomerCommand : IRequest<CreateCustomerCommandResult>
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? Email { get; set; }
public string? Phone { get; set; }
}
public class CreateCustomerCommandValidator : AbstractValidator<CreateCustomerCommand>
{
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);
}
}
public class CreateCustomerCommandHandler : IRequestHandler<CreateCustomerCommand, CreateCustomerCommandResult>
{
public Task<CreateCustomerCommandResult> 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:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSlimMessageBus(mbb => mbb
.WithProviderMemory()
.AutoDeclareFrom(Assembly.GetExecutingAssembly())
.AddAspNet()
.AddFluentValidation(cfg =>
{
// Configure SlimMessageBus.Host.FluentValidation plugin
cfg.AddProducerValidatorsFromAssemblyContaining<CreateCustomerCommandValidator>();
// You can map the validation errors into a custom exception
//cfg.AddValidationErrorsHandler(errors => new ApplicationException("Custom Validation Exception"));
}));
// FluentValidation library - find and register IValidator<T> implementations:
builder.Services.AddValidatorsFromAssemblyContaining<CreateCustomerCommandValidator>();
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):
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:
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<CreateCustomerCommandValidator>();
});
});
For example given an ASP.NET Minimal WebApi, the request can be delegated to SlimMessageBus in memory transport:
// 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 sample.
Consumer side validation
We can also enable validation of the incoming message just before it gets delivered to the respective IConsumer<T>
or IRequestHandler<T, R>
- 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.
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<CreateCustomerCommandValidator>();
});
});
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<T>
validators in the container, - register the respective
ProducerValidationInterceptor<T>
asIProducerInterceptor<T>
for each of the message typeT
that needs to be validated on producer side, - register the respective
ConsumerValidationInterceptor<T>
asIConsumerInterceptor<T>
for each of the message typeT
that needs to be validated on consumer side, - the scope of can be anything that you need (scoped, transient, singleton)