dotnet-starter-kit icon indicating copy to clipboard operation
dotnet-starter-kit copied to clipboard

[BUG] Automatic FluentValidation and Custom Validator

Open mmarquesbr opened this issue 3 years ago • 2 comments

Describe the bug When I try to add an entity, it causes an error related to automatic validation

{ "messages": [ "Validator "CreateBrandRequestValidator" can't be used with ASP.NET automatic validation as it contains asynchronous rules. ASP.NET's validation pipeline is not asynchronous and can't invoke asynchronous rules. Remove the asynchronous rules in order for this validator to run." ], "source": "FluentValidation.AbstractValidator`1", "exception": "Validator "CreateBrandRequestValidator" can't be used with ASP.NET automatic validation as it contains asynchronous rules. ASP.NET's validation pipeline is not asynchronous and can't invoke asynchronous rules. Remove the asynchronous rules in order for this validator to run.", "errorId": "d5d95cd8-69a2-499e-a26f-71451765ab57", "supportMessage": "Provide the ErrorId to the support team for further analysis.", "statusCode": 500 }

To Reproduce Steps to reproduce the behavior:

Post to URL: {{url}}/v1/brands Payload: { "name":"Bra1nod #29", "description":"Something Cool!" }

Error appears.

Remarks: This issue is related to FluentValidation.AspNetCore and FluentValidation.DependencyInjectionExtensions, which was bumped to v11.1.0 in my machine, back to v10.4.0 works normally.

mmarquesbr avatar Jun 29 '22 14:06 mmarquesbr

It happened to me when I updated the FluentValidation nuget package and look for solution on google. I found none, so I decided to downgrade the FluentValidation version.

kirkey avatar Jun 30 '22 03:06 kirkey

Yes, it's a known problem (https://github.com/fullstackhero/dotnet-webapi-boilerplate/issues/639). I've started the work here https://github.com/fullstackhero/dotnet-webapi-boilerplate/pull/693. That PR can use some help ;-)

fretje avatar Jun 30 '22 12:06 fretje

FluentValidation.DependencyInjectionExtensions -> 11.5.1 FluentValidation.AspNetCore -> 11.3.0

This issue no longer exists. Tested this on the dotnet-7 branch with the latest FV packages.

iammukeshm avatar Apr 07 '23 02:04 iammukeshm

I found the solution. I found this code on github https://github.com/salt-repositories/din-api/blob/97edd4b02a173dc6881cc19b12447c6154a94b98/src/Din.Domain.Middlewares/Mediatr/FluentValidationMiddleware.cs#L30

update to the latest FluentValidation packages: FluentValidation.AspNetCore Version="11.3.0" FluentValidation.DependencyInjectionExtensions Version="11.9.0"

public class ValidationBehavior<TRequest, TResponse>(IEnumerable<IValidator<TRequest>> validators) : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse> { public async Task<TResponse> Handle( TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken) { var context = new ValidationContext<TRequest>(request);

    var failures = (await Task.WhenAll(validators
            .Select(validator => validator.ValidateAsync(context, cancellationToken))).ConfigureAwait(false))
        .SelectMany(result => result.Errors)
        .Where(failure => failure != null).ToList();

    if (failures.Any())
    {
        var arr = failures.Select(x => $"{Environment.NewLine} {x.ErrorMessage}");
        throw new Exception(string.Concat(arr));
    }

    return await next();
}

}

kirkey avatar Dec 23 '23 08:12 kirkey