Vali-Validation.ValiMediator 1.1.0

dotnet add package Vali-Validation.ValiMediator --version 1.1.0
                    
NuGet\Install-Package Vali-Validation.ValiMediator -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.ValiMediator" 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.ValiMediator" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Vali-Validation.ValiMediator" />
                    
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.ValiMediator --version 1.1.0
                    
#r "nuget: Vali-Validation.ValiMediator, 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.ValiMediator@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.ValiMediator&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Vali-Validation.ValiMediator&version=1.1.0
                    
Install as a Cake Tool

Vali-Validation.ValiMediator

Vali-Mediator pipeline behavior integration for Vali-Validation. Automatically validates IRequest<T> objects before they reach their handler, with first-class support for Result<T>-based flows — no exceptions needed.

  • 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.ValiMediator

Registration

builder.Services.AddValiMediatorWithValidation(
    config => config.RegisterServicesFromAssembly(typeof(Program).Assembly),
    typeof(Program).Assembly);

Manual setup

builder.Services.AddValiMediator(config =>
{
    config.RegisterServicesFromAssembly(typeof(Program).Assembly);
    config.AddValiValidationBehavior();
});
builder.Services.AddValidationsFromAssembly(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:
    • When TResponse is Result<T> → returns Result<T>.Fail(errors, ErrorType.Validation) (no throw).
    • When TResponse is Result (void) → returns Result.Fail(errors, ErrorType.Validation) (no throw).
    • Otherwise → throws ValidationException.

Usage Example

// Command
public record CreateUserCommand(string Name, string Email) : IRequest<Result<UserDto>>;

// Validator
public class CreateUserCommandValidator : AbstractValidator<CreateUserCommand>
{
    public CreateUserCommandValidator()
    {
        RuleFor(x => x.Name).NotEmpty().MaxLength(100);
        RuleFor(x => x.Email).NotEmpty().Email();
    }
}

// Handler — only reached if validation passes
public class CreateUserHandler : IRequestHandler<CreateUserCommand, Result<UserDto>>
{
    public Task<Result<UserDto>> Handle(CreateUserCommand request, CancellationToken ct)
        => Task.FromResult(Result<UserDto>.Ok(new UserDto(request.Name, request.Email)));
}
// Call site — no try/catch needed
var result = await mediator.Send(new CreateUserCommand("", "not-an-email"));

result.Match(
    onSuccess: dto => Console.WriteLine($"Created: {dto.Name}"),
    onFailure: (errors, type) => Console.WriteLine($"Validation failed: {errors}")
);

Validation Errors Response Format

When TResponse is Result<T>, errors are returned as Dictionary<string, List<string>> inside the Result:

{
  "Name": ["'Name' must not be empty."],
  "Email": ["'Email' is not a valid email address."]
}

Severity note: this behavior builds errors from ValidationResult.Errors, which only reflects Severity.Error failures. A rule marked .WithSeverity(Severity.Warning) (or Severity.Info) never triggers a Result.Fail(...) on its own and never appears here.

Tip: Exception-based Flow

If you use MediatR instead of Vali-Mediator, use Vali-Validation.MediatR — it throws ValidationException on failure and integrates with the standard IPipelineBehavior.


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 125 9/29/2026
1.0.3 166 4/14/2026
1.0.2 156 4/13/2026
1.0.1 149 3/18/2026
1.0.0 138 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 of Vali-Validation.ValiMediator.
           - ValidationBehavior<TRequest, TResponse> implementing Vali-Mediator IPipelineBehavior.
           - Validates request via IValidator<TRequest> resolved from DI.
           - When TResponse is Result<T>: returns Result.Fail(errors, ErrorType.Validation) — no throw.
           - When TResponse is any other type: throws ValidationException.
           - AddValiValidationBehavior() extension on ValiMediatorConfiguration.
           - AddValiMediatorWithValidation() convenience extension on IServiceCollection.