Allows functionality from the FluentValidation libraries to used in a GreenPipes (MassTransit) pipeline. This means that any messages (e.g. commands and queries) that pass through your pipeline will be validated if a validator exists for that message type, otherwise they won't.
- Install the Nuget package
FluentValidationForMassTransit
- In your Startup.cs file, in your
ConfigureServices
method, add FluentValidation and register your validators as per the FluentValidation documentation:
services.AddControllers()
.AddFluentValidation(configuration => configuration
.RegisterValidatorsFromAssemblyContaining<SomeValidator>());
- Decide what you would like to happen when a message fails validation. Make a
ValidationFailurePipe
to handle those messages. YourValidationFailurePipe
must implementFluentValidationForMassTransit.IValidationFailurePipe
(an interface included in this package). It can optionally inherit fromFluentValidationForMassTransit.ValidationFailurePipeBase
(a base class included in this package). Here is an example of aValidationFailurePipe
that passes the dictionary of validation errors back to the caller, but you can code whatever functionality you like. In most cases you'll want to be callingcontext.InnerContext.RespondAsync
. The context'sInnerContext
is theConsumeContext
of the message that was validated.
public class ValidationFailurePipe<TMessage> : ValidationFailurePipeBase<TMessage>
where TMessage : class
{
public async override Task Send(ValidationFailureContext<TMessage> context)
{
var validationProblems = context.ValidationProblems;
await context.InnerContext.RespondAsync(validationProblems);
}
}
- Register your
ValidationFailurePipe
inStartup.ConfigureServices
with a transient lifetime.
services.AddTransient(
typeof(IValidationFailurePipe<>),
typeof(ValidationFailurePipe<>));
- In
Startup.ConfigureServices
, when you callAddMassTransit
, you will then specify your transport mode on theIServiceCollectionBusConfigurator
such asUsingInMemory
orUsingRabbitMQ
etc. Through that, you can use the fluent API to get an instance ofIReceiveEndpointConfigurator
. That is where you can specify (using the extension method in this package)UseFluentValidationForMassTransit
. Pass as an arguemnt your instance ofIBusRegistrationContext
. An example is below:
services.AddMassTransit(services =>
{
// Add consumers
// Add request clients
services.UsingRabbitMq((registrationContext, factoryConfigurator) =>
{
factoryConfigurator.ReceiveEndpoint(AssemblyName, endpointConfigurator =>
{
endpointConfigurator.UseFluentValidationForMassTransit(registrationContext);
// Configure consumers
});
});
});
- From now on, if the FluentValidation library finds a validator that matches the message type, it will use it to validate the message. If a message passes validation, it will be sent to the next handler in the pipeline. If it fails, it will be passed to your ValidationFailurePipe to be handled. Below is an example of a validator:
public class SomeValidator : AbstractValidator<ISomeCommand>
{
public SomeValidator()
{
RuleFor(x => x.OrderId).NotEmpty().WithMessage(command =>
$"'{command.OrderId}' is not a valid identifier.");
RuleFor(x => x.Items).NotEmpty().WithMessage(command =>
$"Order { command.OrderId} has no items.");
}
}