Path: redb-route/redb.Route/Validation/ValidateProcessor.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/ValidateProcessor.cs
----
using redb.Route.Abstractions;
namespace redb.Route.Validation;
///
/// Pipeline processor that validates the exchange using an .
///
/// By default, a failed validation sets the ValidationErrors property on the exchange
/// and throws a . If is false,
/// the processor only sets the property without throwing — giving the developer freedom to handle
/// errors downstream via .Choice() or .Filter().
///
///
public sealed class ValidateProcessor : IProcessor
{
/// Well-known property key for validation error messages.
public const string ValidationErrorsProperty = "ValidationErrors";
/// Well-known property key for the boolean validation result.
public const string ValidationResultProperty = "ValidationResult";
///
/// Well-known message-header key receiving warning-severity validation messages.
/// Multiple warnings are appended as a semicolon-joined string.
///
public const string ValidationWarningsHeader = "redb.validation.warnings";
private readonly IMessageValidator _validator;
///
/// Whether to throw on failure.
/// Default is true. Set to false for soft validation that only sets properties.
/// Ignored when is .
///
public bool ThrowOnFailure { get; }
/// Creates a validate processor that wraps the specified validator.
/// Validator to apply.
/// Whether to throw on validation failure (default: true).
public ValidateProcessor(IMessageValidator validator, bool throwOnFailure = true)
{
_validator = validator ?? throw new ArgumentNullException(nameof(validator));
ThrowOnFailure = throwOnFailure;
}
///
public async Task Process(IExchange exchange, CancellationToken ct = default)
{
var result = await _validator.ValidateAsync(exchange).ConfigureAwait(false);
// Always set properties so downstream steps can inspect them
exchange.Properties[ValidationResultProperty] = result.IsValid;
exchange.Properties[ValidationErrorsProperty] = result.IsValid
? null
: string.Join("; ", result.Errors);
if (!result.IsValid)
{
if (result.Severity == ValidationSeverity.Warning)
{
// Soft path: append to warnings header, never throw.
var newMessages = string.Join("; ", result.Errors);
if (exchange.In.Headers.TryGetValue(ValidationWarningsHeader, out var existing) &&
existing is string s && !string.IsNullOrEmpty(s))
{
exchange.In.Headers[ValidationWarningsHeader] = s + "; " + newMessages;
}
else
{
exchange.In.Headers[ValidationWarningsHeader] = newMessages;
}
}
else if (ThrowOnFailure)
{
throw new ValidationException(result.Errors);
}
}
}
}
///
/// Exception thrown when message validation fails and is true.
///
public sealed class ValidationException : Exception
{
/// Individual validation error messages.
public IReadOnlyList Errors { get; }
/// Creates a validation exception with the specified errors.
/// Validation error messages.
public ValidationException(IReadOnlyList errors)
: base($"Validation failed: {string.Join("; ", errors)}")
{
Errors = errors;
}
}