Vali-Validation.MediatR 1.1.0

dotnet add package Vali-Validation.MediatR --version 1.1.0
                    
NuGet\Install-Package Vali-Validation.MediatR -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Vali-Validation.MediatR" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Vali-Validation.MediatR" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Vali-Validation.MediatR" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Vali-Validation.MediatR --version 1.1.0
                    
#r "nuget: Vali-Validation.MediatR, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Vali-Validation.MediatR@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Vali-Validation.MediatR&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Vali-Validation.MediatR&version=1.1.0
                    
Install as a Cake Tool

Vali-Validation.MediatR

MediatR pipeline behavior integration for Vali-Validation. Automatically validates IRequest<T> objects before they reach their handler.

  • Targets: net7.0 / net8.0 / net9.0 / net10.0
  • Depends on Vali-Validation 3.0.0+ — Severity-aware ValidationResult, RuleSets, Localization, ValiValidationOptions.Global, and PropertyValidator all work transparently through this pipeline behavior.

Installation

dotnet add package Vali-Validation.MediatR

Registration

builder.Services.AddMediatRWithValidation(cfg =>
{
    cfg.RegisterServicesFromAssembly(typeof(Program).Assembly);
}, typeof(Program).Assembly);

Manual setup

builder.Services.AddMediatR(cfg =>
{
    cfg.RegisterServicesFromAssembly(typeof(Program).Assembly);
});
builder.Services.AddValiValidationBehavior(typeof(Program).Assembly);

How It Works

ValidationBehavior<TRequest, TResponse> is registered as an IPipelineBehavior. Before the handler executes:

  1. Resolves IValidator<TRequest> from DI. If not registered, the pipeline continues normally.
  2. Calls ValidateAsync on the validator.
  3. If validation passes, calls the next step in the pipeline.
  4. If validation fails, throws ValidationException with structured errors (property → messages).
// Handler
public class CreateUserHandler : IRequestHandler<CreateUserCommand, UserDto>
{
    public Task<UserDto> Handle(CreateUserCommand request, CancellationToken ct)
        => ...; // only reached if validation passes
}
// Validator
public class CreateUserCommandValidator : AbstractValidator<CreateUserCommand>
{
    public CreateUserCommandValidator()
    {
        RuleFor(x => x.Name).NotEmpty().MaxLength(100);
        RuleFor(x => x.Email).NotEmpty().Email();
    }
}

Catching Validation Errors

Catch ValidationException from the call site or use ValiValidationMiddleware from Vali-Validation.AspNetCore:

try
{
    var result = await mediator.Send(new CreateUserCommand(...));
}
catch (ValidationException ex)
{
    // ex.ValidationResult.Errors: IReadOnlyDictionary<string, List<string>> — Severity.Error failures only.
    // ex.ValidationResult.Failures: List<ValidationFailure> — every failure, including Warning/Info severities.
}

Tip: Result<T>-based Flow

If you prefer returning errors as values instead of throwing exceptions, use Vali-Mediator.Validation instead. It integrates with Vali-Mediator and returns Result<T>.Fail(ValidationErrors, ErrorType.Validation) when TResponse is Result<T>.


Donations

If Vali-Validation is useful to you, consider supporting its development:


License

Apache License 2.0

Contributions

Issues and pull requests are welcome on GitHub.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 131 10/1/2026
1.0.3 168 4/14/2026
1.0.2 135 4/13/2026
1.0.1 141 3/18/2026
1.0.0 179 3/16/2026

v1.1.0
           - Adds net10.0 to the multi-targeted frameworks (now net7.0/net8.0/net9.0/net10.0).
           - Now depends on Vali-Validation 3.0.0+, which brings Severity-aware ValidationResult,
             RuleSets, block-level When/Unless, InjectValidator, Localization (English/Spanish
             built in), ValiValidationOptions.Global, and PropertyValidator to any validator run
             through this pipeline behavior. No changes to this package's own public API.

           v1.0.0 — Initial release.
           - ValidationBehavior<TRequest, TResponse> implementing MediatR IPipelineBehavior.
           - Validates request via IValidator<TRequest> resolved from DI.
           - Throws ValidationException if validation fails (use Vali-Mediator.Validation for Result-based flow).
           - AddMediatRWithValidation() convenience extension for one-call registration.
           - AddValiValidationBehavior() for manual MediatR setup.