Table of Contents

Class FluentValidationMessageValidatorAdapter<TMessage>

Namespace
Trellis.Mediator.FluentValidation
Assembly
Trellis.Mediator.FluentValidation.dll

Adapts FluentValidation FluentValidation.IValidator<T> implementations into the Trellis Mediator validation stage. Resolves every IValidator<TMessage> registered in DI, runs them, and returns an aggregated Error.InvalidInput failure when any validator reports errors.

public sealed class FluentValidationMessageValidatorAdapter<TMessage> : IMessageValidator<TMessage> where TMessage : IMessage

Type Parameters

TMessage

The message type the contained validators target.

Inheritance
FluentValidationMessageValidatorAdapter<TMessage>
Implements
Inherited Members
Extension Methods

Remarks

Registered as an open-generic IMessageValidator<TMessage> by AddTrellisFluentValidation(IServiceCollection). When no FluentValidation.IValidator<T> is registered for a given TMessage, the injected sequence is empty and the adapter returns success without allocating violations.

FluentValidation property names that include member chains (Address.PostCode) or indexers (Items[0].Sku) are translated to camelCase RFC 6901 JSON Pointers (/address/postCode, /items/0/sku) via ToJsonPointer(string?) so they round-trip correctly through InputPointer. The normalizer lives in the sister Trellis.FluentValidation package so that domain-layer FluentValidation conversion helpers can share the same RFC 6901 implementation without a Mediator dependency.

Constructors

FluentValidationMessageValidatorAdapter(IEnumerable<IValidator<TMessage>>)

public FluentValidationMessageValidatorAdapter(IEnumerable<IValidator<TMessage>> validators)

Parameters

validators IEnumerable<IValidator<TMessage>>

The injected sequence of FluentValidation.IValidator<T> implementations to run against each message. Typically resolved from DI as IEnumerable<IValidator<TMessage>>.

Exceptions

ArgumentNullException

Thrown when validators is null.

FluentValidationMessageValidatorAdapter(IEnumerable<IValidator<TMessage>>, IOptions<ValidationArgsOptions>)

Initializes a new instance of FluentValidationMessageValidatorAdapter<TMessage> with an application-configured validation-arg allowlist.

public FluentValidationMessageValidatorAdapter(IEnumerable<IValidator<TMessage>> validators, IOptions<ValidationArgsOptions> argsOptions)

Parameters

validators IEnumerable<IValidator<TMessage>>

The injected sequence of FluentValidation.IValidator<T> implementations to run against each message.

argsOptions IOptions<ValidationArgsOptions>

The application's widening of the default arg allowlist. Supplied by the options system, so the pipeline adapter and the standalone ToResult helpers project identically.

Exceptions

ArgumentNullException

Thrown when validators or argsOptions is null.

Methods

ValidateAsync(TMessage, CancellationToken)

Validates the message asynchronously.

public 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.InvalidInput so the pipeline can aggregate them across multiple validators.