Memoria can run a validator over a command before it reaches its handler. If validation fails, the handler is not called and the dispatcher returns a Result whose Error.Details lists every failure.
This guide assumes you have FluentValidation registered — see Configuration: Validation.
Validation is opt-in. Set validateCommand: true on any dispatcher method:
var result = await dispatcher.Send(command, validateCommand: true);
var result = await dispatcher.SendAndPublish(command, validateCommand: true);
For command sequences, use validateCommands: true on SendSequence.
{
"IsSuccess": false,
"Value": null,
"Error": {
"Message": "Validation failed",
"Details": [
"Name must not be empty",
"Age must be greater than 0"
]
}
}
Use FluentValidation as you would in any other project:
public class CreateProductValidator : AbstractValidator<CreateProduct>
{
public CreateProductValidator()
{
RuleFor(c => c.Name).NotEmpty();
}
}
AddMemoriaFluentValidation(typeof(...)) scans the supplied assembly for AbstractValidator<> subclasses and registers them automatically.