How to create ValidationBehavior without throwing exception and return validation errors? #172
Unanswered
odysseus1973
asked this question in
Q&A
Replies: 2 comments
-
You would create your own, similar to the one baked in: public sealed class ValidationBehavior<TRequest, TResponse>
: Behavior<TRequest, TResponse>
where TRequest : IValidationTarget<TRequest>
where TResponse : SomeResultType<TResponse>
{
/// <summary>
/// Validate the <paramref name="request"/> and throw an exception if it fails.
/// </summary>
/// <inheritdoc />
/// <exception cref="ValidationException">
/// Thrown if the <paramref name="request"/> does not validate successfully.
/// </exception>
public override async ValueTask<TResponse> HandleAsync(TRequest request, CancellationToken cancellationToken)
{
var result = TRequest.Validate(request);
if (!result.IsValid)
return new SomeResultType<TResponse>(failed);
return await Next(request, cancellationToken).ConfigureAwait(false);
}
} This is not built-in because it is not possible for IV to know all potential result types that users may use, or how to correctly interact with them. Code is provided as example; further editing will be required. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks for answer! Trying everything with Result from FluentResult library but without success. In my case I need return Result with validation errors (from FluentValidation library) when validation errors exists or return Result when validation success. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
How to create ValidationBehavior without throwing exception and return validation errors?
Beta Was this translation helpful? Give feedback.
All reactions