Vali-Validation.ValiMediator
1.0.3
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
<PackageReference Include="Vali-Validation.ValiMediator" Version="1.0.3" />
<PackageVersion Include="Vali-Validation.ValiMediator" Version="1.0.3" />
<PackageReference Include="Vali-Validation.ValiMediator" />
paket add Vali-Validation.ValiMediator --version 1.0.3
#r "nuget: Vali-Validation.ValiMediator, 1.0.3"
#:package Vali-Validation.ValiMediator@1.0.3
#addin nuget:?package=Vali-Validation.ValiMediator&version=1.0.3
#tool nuget:?package=Vali-Validation.ValiMediator&version=1.0.3
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
One-call setup (recommended)
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:
- Resolves
IValidator<TRequest>from DI. If not registered, the pipeline continues normally. - Calls
ValidateAsyncon the validator. - If validation passes, calls the next step in the pipeline.
- If validation fails:
- When
TResponseisResult<T>→ returnsResult<T>.Fail(errors, ErrorType.Validation)(no throw). - When
TResponseisResult(void) → returnsResult.Fail(errors, ErrorType.Validation)(no throw). - Otherwise → throws
ValidationException.
- When
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:
- Latin America — MercadoPago
- International — PayPal
License
Contributions
Issues and pull requests are welcome on GitHub.
| Product | Versions 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. |
-
net7.0
- Vali-Mediator (>= 2.0.0)
- Vali-Validation (>= 2.0.2)
-
net8.0
- Vali-Mediator (>= 2.0.0)
- Vali-Validation (>= 2.0.2)
-
net9.0
- Vali-Mediator (>= 2.0.0)
- Vali-Validation (>= 2.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
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.