Path: redb-route/redb.Route/Validation/PredicateValidator.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/PredicateValidator.cs ---- using redb.Route.Abstractions; using redb.Route.Predicates; namespace redb.Route.Validation; /// /// Validator that checks an against the exchange. The predicate is /// awaited through when the processor validates /// asynchronously, so a predicate that consults a store or a service does not block. A plain /// delegate is wrapped in a at the boundary. /// public sealed class PredicateValidator : IMessageValidator { private readonly IPredicate _predicate; private readonly Func? _errorFactory; private readonly string _defaultError; /// Creates a validator from a predicate instance and a fixed error message. /// The condition that must hold. /// The error reported when it does not. public PredicateValidator(IPredicate predicate, string errorMessage = "Validation failed") { _predicate = predicate ?? throw new ArgumentNullException(nameof(predicate)); _defaultError = errorMessage; } /// Creates a validator from a predicate instance and an error factory. /// The condition that must hold. /// Builds the error message from the failing exchange. public PredicateValidator(IPredicate predicate, Func errorFactory) { _predicate = predicate ?? throw new ArgumentNullException(nameof(predicate)); _errorFactory = errorFactory ?? throw new ArgumentNullException(nameof(errorFactory)); _defaultError = "Validation failed"; } /// Creates a validator from a delegate and a fixed error message. /// The condition that must hold. /// The error reported when it does not. public PredicateValidator(Func predicate, string errorMessage = "Validation failed") : this(new LambdaPredicate(predicate ?? throw new ArgumentNullException(nameof(predicate))), errorMessage) { } /// Creates a validator from a delegate and an error factory. /// The condition that must hold. /// Builds the error message from the failing exchange. public PredicateValidator(Func predicate, Func errorFactory) : this(new LambdaPredicate(predicate ?? throw new ArgumentNullException(nameof(predicate))), errorFactory) { } /// public ValidationResult Validate(IExchange exchange) { ArgumentNullException.ThrowIfNull(exchange, nameof(exchange)); return ToResult(_predicate.Matches(exchange), exchange); } /// public async Task ValidateAsync(IExchange exchange) { ArgumentNullException.ThrowIfNull(exchange, nameof(exchange)); return ToResult(await _predicate.MatchesAsync(exchange).ConfigureAwait(false), exchange); } private ValidationResult ToResult(bool holds, IExchange exchange) { if (holds) return ValidationResult.Success(); var error = _errorFactory != null ? _errorFactory(exchange) : _defaultError; return ValidationResult.Failure(error); } }