mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
Adds validation mediator pipeline
This commit is contained in:
1 parent
0ae5588bbe
commit
5dc40db307
2 files changed
+68
No files matched your search
@@ -0,0 +1,56 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Mediator;
|
||||
using ErrorOr;
|
||||
using FluentValidation;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Behaviors;
|
||||
|
||||
/// <summary>
|
||||
/// Behavior pipeline for the register command
|
||||
/// </summary>
|
||||
/// <typeparam name="TRequest">The type representing a request. It should implement <see cref="IRequest{TResponse}"/>.</typeparam>
|
||||
/// <typeparam name="TResponse">The type representing a response. It should implement <see cref="IErrorOr"/> interface.</typeparam>
|
||||
public class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>
|
||||
where TResponse : IErrorOr
|
||||
{
|
||||
#region ================================================================== FIELD MEMBERS ================================================================================
|
||||
private readonly IValidator<TRequest>? validator;
|
||||
#endregion
|
||||
|
||||
#region ====================================================================== CTOR =====================================================================================
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ValidationBehavior{TRequest, TResponse}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="validator">The validator used in this behavior pipeline for the Mediator commands and queries.</param>
|
||||
public ValidationBehavior(IValidator<TRequest>? validator = null)
|
||||
{
|
||||
this.validator = validator;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ===================================================================== METHODS ===================================================================================
|
||||
/// <summary>
|
||||
/// Pipeline handler. Perform any additional behavior and await the <paramref name="next"/> delegate as necessary.
|
||||
/// </summary>
|
||||
/// <param name="request">Incoming request.</param>
|
||||
/// <param name="next">Awaitable delegate for the next action in the pipeline. Eventually this delegate represents the handler.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
public async ValueTask<TResponse> Handle(TRequest request, CancellationToken cancellationToken, MessageHandlerDelegate<TRequest, TResponse> next)
|
||||
{
|
||||
// if there is no validator, just invoke the handler
|
||||
if (validator is null)
|
||||
return await next(request, cancellationToken);
|
||||
// before the handler of the command is executed
|
||||
var validationResult = await validator.ValidateAsync(request, cancellationToken);
|
||||
// if there are no validation errors, invoke the command handler
|
||||
if (validationResult.IsValid)
|
||||
return await next(request, cancellationToken);
|
||||
// after the command handler is executed
|
||||
var errors = validationResult.Errors.ConvertAll(validationFailure => Error.Validation(validationFailure.ErrorMessage));
|
||||
// the compiler doesn't know there is an implicit converter from a list of errors to the ErrorOr object, and unfortunately, there is no way around this but to use some magic
|
||||
// this is acceptable because we DO know that we will always have a list of errors of type ErrorOr (check the generic constraint at the top of the class!)
|
||||
return (dynamic)errors;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -4,6 +4,18 @@
|
||||
<ProjectReference Include="..\Lumina.Domain\Lumina.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentValidation" Version="11.9.2" />
|
||||
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.2" />
|
||||
<PackageReference Include="Mediator.Abstractions" Version="2.1.7" />
|
||||
<PackageReference Include="Mediator.SourceGenerator" Version="2.1.7">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
|
||||
Reference in new issue
Block a user