-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Validation Pipeline and CustomExceptions.
- Loading branch information
1 parent
502e360
commit 5cb16e2
Showing
7 changed files
with
71 additions
and
38 deletions.
There are no files selected for viewing
58 changes: 23 additions & 35 deletions
58
src/SwiftLink.Application/Behaviors/ValidationBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,35 @@ | ||
using Azure; | ||
using FluentValidation; | ||
using FluentValidation; | ||
using MediatR; | ||
using System.Net; | ||
using SwiftLink.Application.Common; | ||
using SwiftLink.Application.Common.Exceptions; | ||
|
||
namespace SwiftLink.Application.Behaviors; | ||
|
||
/// <summary> | ||
/// Pipie line for Validation. | ||
/// </summary> | ||
/// <typeparam name="TRequest"></typeparam> | ||
/// <typeparam name="TResult"></typeparam> | ||
public class ValidationBehavior<TRequest, TResult>(IEnumerable<IValidator<TRequest>> validators) | ||
: IPipelineBehavior<TRequest, TResult> where TRequest : notnull | ||
public sealed class ValidationBehavior<TRequest, TResponse>(IEnumerable<IValidator<TRequest>> validators) | ||
: IPipelineBehavior<TRequest, TResponse> | ||
{ | ||
private readonly IEnumerable<IValidator<TRequest>> _validators = validators; | ||
|
||
public async Task<TResult> Handle(TRequest request, RequestHandlerDelegate<TResult> next, | ||
CancellationToken ct = default) | ||
public async Task<TResponse> Handle( | ||
TRequest request, | ||
RequestHandlerDelegate<TResponse> next, | ||
CancellationToken cancellationToken) | ||
{ | ||
if (!_validators.Any()) | ||
return await next(); | ||
|
||
var validationContext = new ValidationContext<TRequest>(request); | ||
|
||
var validationResults = await Task.WhenAll(_validators.Select(v => v.ValidateAsync(validationContext, ct))); | ||
var context = new ValidationContext<TRequest>(request); | ||
|
||
var errorList = validationResults.Where(r => r.Errors.Count != 0) | ||
.SelectMany(x => x.Errors) | ||
.ToList(); | ||
var validationFailures = await Task.WhenAll(_validators.Select(validator => validator.ValidateAsync(context))); | ||
|
||
if (errorList.Count > 0) | ||
{ | ||
var message = string.Join(" | ", errorList.Select(e => e.ErrorMessage)); | ||
var errors = validationFailures | ||
.Where(validationResult => !validationResult.IsValid) | ||
.SelectMany(validationResult => validationResult.Errors) | ||
.Select(validationFailure => new ValidationError( | ||
validationFailure.PropertyName, | ||
validationFailure.ErrorMessage)) | ||
.ToList(); | ||
|
||
if (Enum.TryParse(errorList.First().ErrorCode, out HttpStatusCode statusCode)) | ||
return GenerateResponse(message, statusCode); | ||
if (errors.Count != 0) | ||
throw new BusinessValidationException(errors); | ||
|
||
return GenerateResponse(message); | ||
} | ||
|
||
return await next(); | ||
var response = await next(); | ||
return response; | ||
} | ||
|
||
private static TResult GenerateResponse(string message, HttpStatusCode statusCode = HttpStatusCode.BadRequest) | ||
=> (TResult)Activator.CreateInstance(typeof(Result<object>), false, message, statusCode); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/SwiftLink.Application/Common/Exceptions/BusinessValidationException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace SwiftLink.Application.Common.Exceptions; | ||
public class BusinessValidationException(List<ValidationError> errors) : Exception | ||
{ | ||
public readonly List<ValidationError> Errors = errors; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
namespace SwiftLink.Application.Common; | ||
public record ValidationError(string PropertyName, string ErrorMessage); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/SwiftLink.Presentation/Middleware/BusinessValidationExceptionHandlingMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging; | ||
using SwiftLink.Application.Common.Exceptions; | ||
using System; | ||
|
||
namespace SwiftLink.Presentation.Middleware; | ||
|
||
public sealed class BusinessValidationExceptionHandlingMiddleware(RequestDelegate next) | ||
{ | ||
private readonly RequestDelegate _next = next; | ||
|
||
public async Task InvokeAsync(HttpContext context) | ||
{ | ||
try | ||
{ | ||
await _next(context); | ||
} | ||
catch (BusinessValidationException exception) | ||
{ | ||
var problemDetails = new ProblemDetails | ||
{ | ||
Status = StatusCodes.Status400BadRequest, | ||
Type = "ValidationFailure", | ||
Title = "Validation error", | ||
Detail = "One or more validation errors has occurred" | ||
}; | ||
|
||
if (exception.Errors is not null) | ||
problemDetails.Extensions["errors"] = exception.Errors; | ||
|
||
context.Response.StatusCode = StatusCodes.Status400BadRequest; | ||
await context.Response.WriteAsJsonAsync(problemDetails); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters