Tolitech.Application.Validation
1.0.0-preview.6
dotnet add package Tolitech.Application.Validation --version 1.0.0-preview.6
NuGet\Install-Package Tolitech.Application.Validation -Version 1.0.0-preview.6
<PackageReference Include="Tolitech.Application.Validation" Version="1.0.0-preview.6" />
<PackageVersion Include="Tolitech.Application.Validation" Version="1.0.0-preview.6" />
<PackageReference Include="Tolitech.Application.Validation" />
paket add Tolitech.Application.Validation --version 1.0.0-preview.6
#r "nuget: Tolitech.Application.Validation, 1.0.0-preview.6"
#:package Tolitech.Application.Validation@1.0.0-preview.6
#addin nuget:?package=Tolitech.Application.Validation&version=1.0.0-preview.6&prerelease
#tool nuget:?package=Tolitech.Application.Validation&version=1.0.0-preview.6&prerelease
Tolitech.Application.Validation
Overview
Tolitech.Application.Validation is a .NET library that provides a pipeline behavior for MediatR, enabling automatic validation of requests using FluentValidation before they reach their handlers. It helps enforce business rules, improve code quality, and maintain a clean architecture.
Features
- ValidationBehavior: Pipeline behavior for MediatR that validates requests using registered FluentValidation validators.
- Error Aggregation: Collects validation errors and returns them in a standardized result format.
- Integration with Tolitech.Results: Returns validation errors using the Result type for consistency.
- Localization Support: Error messages can be localized using resource files.
- Extensibility: Easily add custom validators for any request type.
Main Class
ValidationBehavior<TRequest, TResponse>
Validates requests before passing them to the next handler.
public sealed class ValidationBehavior<TRequest, TResponse>(IEnumerable<IValidator<TRequest>> validators)
: IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
where TResponse : Result
Example Usage
1. Define a Request and Validator
public class CreateOrderCommand : IRequest<Result>
{
public string ProductId { get; set; }
public int Quantity { get; set; }
}
public class CreateOrderCommandValidator : AbstractValidator<CreateOrderCommand>
{
public CreateOrderCommandValidator()
{
RuleFor(x => x.ProductId).NotEmpty();
RuleFor(x => x.Quantity).GreaterThan(0);
}
}
2. Register Validators and Pipeline Behavior
services.AddValidatorsFromAssemblyContaining<CreateOrderCommandValidator>();
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
3. Using MediatR
var result = await mediator.Send(new CreateOrderCommand { ProductId = "A1", Quantity = 0 });
if (!result.IsValid)
{
// Handle validation errors
foreach (var error in result.Errors)
{
Console.WriteLine($"{error.Key}: {error.Message}");
}
}
Integration Steps
- Reference Tolitech.Application.Validation in your project.
- Create FluentValidation validators for your requests.
- Register validators and ValidationBehavior in your DI container.
- Use MediatR to send requests; validation will be automatic.
Summary
Tolitech.Application.Validation automates request validation in MediatR pipelines, improving reliability and maintainability in .NET applications.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
-
net9.0
- FluentValidation (>= 12.0.0)
- MediatR (>= 12.5.0)
- Tolitech.Results (>= 1.0.0-preview.5)
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.0.0-preview.6 | 431 | 7/21/2025 |
1.0.0-preview.5 | 115 | 7/7/2025 |
1.0.0-preview.4 | 117 | 7/3/2025 |
1.0.0-preview.3 | 121 | 7/3/2025 |
1.0.0-preview.2 | 96 | 12/13/2024 |
1.0.0-preview.1 | 71 | 12/11/2024 |