Vali-Validation.ValiMediator 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package Vali-Validation.ValiMediator --version 1.0.3
                    
NuGet\Install-Package Vali-Validation.ValiMediator -Version 1.0.3
                    
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.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Vali-Validation.ValiMediator" Version="1.0.3" />
                    
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.0.3
                    
#r "nuget: Vali-Validation.ValiMediator, 1.0.3"
                    
#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.0.3
                    
#: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.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Vali-Validation.ValiMediator&version=1.0.3
                    
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.

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."]
}

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 was computed.  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.0.3 — Bug fixes and improvements:
           - Fixed reflection in ValidationBehavior to properly extract ValidationErrors from Result<T> struct.
           - Enhanced validation error extraction in Result conversion (updated BindingFlags for public properties).

           v1.0.2 — Enhanced logging for debugging:
           - Added diagnostic logging in ValidationBehavior to track validation execution.

           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.