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
<PackageReference Include="Vali-Validation.MediatR" Version="1.1.0" />
<PackageVersion Include="Vali-Validation.MediatR" Version="1.1.0" />
<PackageReference Include="Vali-Validation.MediatR" />
paket add Vali-Validation.MediatR --version 1.1.0
#r "nuget: Vali-Validation.MediatR, 1.1.0"
#:package Vali-Validation.MediatR@1.1.0
#addin nuget:?package=Vali-Validation.MediatR&version=1.1.0
#tool nuget:?package=Vali-Validation.MediatR&version=1.1.0
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-Validation3.0.0+ — Severity-awareValidationResult, RuleSets, Localization,ValiValidationOptions.Global, andPropertyValidatorall work transparently through this pipeline behavior.
Installation
dotnet add package Vali-Validation.MediatR
Registration
One-call setup (recommended)
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:
- 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, throws
ValidationExceptionwith 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:
- 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 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. |
-
net10.0
- MediatR (>= 12.4.1)
- Vali-Validation (>= 3.0.0)
-
net7.0
- MediatR (>= 12.4.1)
- Vali-Validation (>= 3.0.0)
-
net8.0
- MediatR (>= 12.4.1)
- Vali-Validation (>= 3.0.0)
-
net9.0
- MediatR (>= 12.4.1)
- Vali-Validation (>= 3.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
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.