Table of Contents

Interface IMessageValidator<TMessage>

Namespace
Trellis.Mediator
Assembly
Trellis.Mediator.dll

Contributes a validation step to the unified validation stage of the Trellis Mediator pipeline (ValidationBehavior<TMessage, TResponse>).

public interface IMessageValidator<in TMessage> where TMessage : IMessage

Type Parameters

TMessage

The message type to validate.

Extension Methods

Remarks

Implementations are resolved from DI as IEnumerable<IMessageValidator<TMessage>> by ValidationBehavior<TMessage, TResponse>. Every registered validator runs before the handler executes; their Error.UnprocessableContent failures are aggregated into a single response failure. Any non-Error.UnprocessableContent failure short-circuits the stage immediately.

This is the extensibility point that lets external packages (e.g., Trellis.FluentValidation) plug additional validation sources into the pipeline without taking a dependency on a specific message-side interface or validation library from Trellis.Mediator.

Implementations should return Result.Ok() when validation passes, and Result.Fail(new Error.UnprocessableContent(...)) with field-level violations when it fails. Returning a non-Error.UnprocessableContent failure is allowed but will short-circuit subsequent validators in the same request.

Methods

ValidateAsync(TMessage, CancellationToken)

Validates the message asynchronously.

ValueTask<IResult> ValidateAsync(TMessage message, CancellationToken cancellationToken)

Parameters

message TMessage

The message to validate.

cancellationToken CancellationToken

A token to observe for cancellation.

Returns

ValueTask<IResult>

Result.Ok() on success, or a failure IResult describing the violations. Field-level violations should be wrapped in an Error.UnprocessableContent so the pipeline can aggregate them across multiple validators.