Validation
Vitreous.FluentValidation runs your FluentValidation validators before the handler. A message that fails validation never reaches its handler: the dispatch returns Error.Validation with the failures grouped by property.
dotnet add package Vitreous.FluentValidationdotnet add package FluentValidation.DependencyInjectionExtensionsbuilder.Services.AddVitreousHandlers();builder.Services.AddVitreousValidation();builder.Services.AddValidatorsFromAssemblyContaining<CreateOrderCommandValidator>(); // scoped by defaultAddVitreousValidation() registers the behaviors only. Validators are ordinary FluentValidation validators, registered here by FluentValidation.DependencyInjectionExtensions:
public sealed class CreateOrderCommandValidator : AbstractValidator<CreateOrderCommand>{ public CreateOrderCommandValidator() { RuleFor(c => c.CustomerName).NotEmpty(); RuleFor(c => c.Quantity).GreaterThan(0); }}The failures are in Error.Extensions["failures"], an IReadOnlyDictionary<string, string[]>, ready for Results.ValidationProblem:
app.MapPost("/orders", async (CreateOrderCommand command, ISender sender, CancellationToken ct) => (await sender.Send(command, ct)).Match( id => Results.Created($"/orders/{id}", new { id }), error => error.Extensions?.GetValueOrDefault("failures") is IReadOnlyDictionary<string, string[]> failures ? Results.ValidationProblem(failures.ToDictionary()) // .NET 8 takes an IDictionary; .NET 9+ takes failures as is : Results.Problem(error.Message)));{ "title": "One or more validation errors occurred.", "status": 400, "errors": { "CustomerName": ["'Customer Name' must not be empty."], "Quantity": ["'Quantity' must be greater than '0'."] }}How it behaves
Section titled “How it behaves”- What is validated. Commands (including void commands), queries and stream queries. A stream query is validated once, before the stream opens, and
CreateStreamreturns the failure. Notifications are not validated. - Every validator runs. All validators registered for the message run one after another, and every failure is reported. Any failure fails the message, whatever its
Severity, as with FluentValidation’sValidationResult.IsValid. The error has codeVALIDATIONand messageValidation failed., and the failures are keyed by FluentValidation’sPropertyName, e.g.QuantityorLines[0].Sku. - Messages without a validator pass straight through, without an extra async step.
- Lifetimes. The behaviors are transient, so they take scoped validators (the
AddValidatorsFromAssembly*default) from the request’s scope. WithDefaultHandlerLifetime = Singletonthe pipeline is resolved from the root provider, so register the validators as singletons:AddValidatorsFromAssemblyContaining<T>(ServiceLifetime.Singleton). - Order. Behaviors run outermost first. Call
AddVitreousValidation()beforeAddVitreousCaching()orAddVitreousRedisCaching(), so an invalid query never reaches the cache. The DevUI’s tracing behaviors are always outermost, so failed validations still appear in the dashboard.
Vitreous.FluentValidation is not marked AOT-compatible, because FluentValidation itself is not annotated for trimming.