-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor validation behavior to use ErrorOr
- Loading branch information
Showing
16 changed files
with
246 additions
and
122 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@host = https://localhost:7098 | ||
|
||
|
||
### Delete Todo itemId = 10 with wrong itemId. Check for 404 Not Found | ||
DELETE {{host}}/api/todo-items/10 | ||
|
||
|
||
### Delete Todo itemId = 1, should return 204 No Content | ||
DELETE {{host}}/api/todo-items/1 |
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,14 @@ | ||
@host = https://localhost:7098 | ||
@itemId = 4 | ||
@listId = 1 | ||
|
||
### Update Todo itemId = 1 with PriorityLevel = 1 and Note = "Updated Todo Item 1" | ||
PUT {{host}}/api/todo-items/UpdateItemDetails?id={{itemId}} | ||
Content-Type: application/json | ||
|
||
{ | ||
"id": {{itemId}}, | ||
"listId": {{listId}}, | ||
"priorityLevel": 1, | ||
"note": "Updated Todo Item 1" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
@host = https://localhost:7098 | ||
|
||
|
||
### Delete Todo itemId = 10 with wrong itemId. Check for 404 Not Found | ||
DELETE {{host}}/api/todo-lists/10 | ||
|
||
|
||
### Delete Todo itemId = 1, should return 204 No Content | ||
DELETE {{host}}/api/todo-lists/1 |
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
32 changes: 0 additions & 32 deletions
32
src/Application/Common/Behaviours/UnhandledExceptionBehaviour.cs
This file was deleted.
Oops, something went wrong.
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,36 +1,40 @@ | ||
using FluentValidation; | ||
using ErrorOr; | ||
|
||
using MediatR; | ||
using FluentValidation; | ||
|
||
using ValidationException = VerticalSliceArchitecture.Application.Common.Exceptions.ValidationException; | ||
using MediatR; | ||
|
||
namespace VerticalSliceArchitecture.Application.Common.Behaviours; | ||
|
||
public class ValidationBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> | ||
where TRequest : IRequest<TResponse> | ||
public class ValidationBehavior<TRequest, TResponse>(IValidator<TRequest>? validator = null) | ||
: IPipelineBehavior<TRequest, TResponse> | ||
where TRequest : IRequest<TResponse> | ||
where TResponse : IErrorOr | ||
{ | ||
private readonly IEnumerable<IValidator<TRequest>> _validators; | ||
|
||
public ValidationBehaviour(IEnumerable<IValidator<TRequest>> validators) | ||
{ | ||
_validators = validators; | ||
} | ||
private readonly IValidator<TRequest>? _validator = validator; | ||
|
||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken) | ||
public async Task<TResponse> Handle( | ||
TRequest request, | ||
RequestHandlerDelegate<TResponse> next, | ||
CancellationToken cancellationToken) | ||
{ | ||
if (_validators.Any()) | ||
if (_validator is null) | ||
{ | ||
var context = new ValidationContext<TRequest>(request); | ||
return await next(); | ||
} | ||
|
||
var validationResults = await Task.WhenAll(_validators.Select(v => v.ValidateAsync(context, cancellationToken))); | ||
var failures = validationResults.SelectMany(r => r.Errors).Where(f => f != null).ToList(); | ||
var validationResult = await _validator.ValidateAsync(request, cancellationToken); | ||
|
||
if (failures.Count != 0) | ||
{ | ||
throw new ValidationException(failures); | ||
} | ||
if (validationResult.IsValid) | ||
{ | ||
return await next(); | ||
} | ||
|
||
return await next(); | ||
var errors = validationResult.Errors | ||
.ConvertAll(error => Error.Validation( | ||
code: error.PropertyName, | ||
description: error.ErrorMessage)); | ||
|
||
return (dynamic)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
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
Oops, something went wrong.