Path: redb-route/redb.Route.Validation.Adapters/FluentValidationMessageValidator.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Validation.Adapters/FluentValidationMessageValidator.cs
----
using FluentValidation;
using redb.Route.Abstractions;
namespace redb.Route.Validation.Adapters;
///
/// Bridges FluentValidation's
/// to redb.Route's .
///
/// The exchange body is cast to before validation. A null body or
/// a body of incompatible type yields a single error result; the supplied severity governs
/// how reacts.
///
///
/// The expected body type.
public sealed class FluentValidationMessageValidator : IMessageValidator
{
private readonly IValidator _validator;
private readonly ValidationSeverity _severity;
///
/// Creates a FluentValidation-backed message validator.
///
/// FluentValidation validator instance.
/// Severity for failures (default ).
public FluentValidationMessageValidator(IValidator validator,
ValidationSeverity severity = ValidationSeverity.Error)
{
_validator = validator ?? throw new ArgumentNullException(nameof(validator));
_severity = severity;
}
///
public ValidationResult Validate(IExchange exchange)
{
ArgumentNullException.ThrowIfNull(exchange);
var body = exchange.In.Body;
if (body is null)
return ValidationResult.Failure(new[] { "Message body is null." }, _severity);
if (body is not T typed)
return ValidationResult.Failure(
new[] { $"Body of type '{body.GetType().FullName}' is not assignable to '{typeof(T).FullName}'." },
_severity);
var result = _validator.Validate(typed);
if (result.IsValid)
return ValidationResult.Success();
var errors = result.Errors
.Select(e => string.IsNullOrEmpty(e.PropertyName) ? e.ErrorMessage : $"{e.PropertyName}: {e.ErrorMessage}")
.ToArray();
return ValidationResult.Failure(errors, _severity);
}
}