fullstackhero / dotnet-starter-kit

Production Grade Cloud-Ready .NET 8 Starter Kit (Web API + Blazor Client) with Multitenancy Support, and Clean/Modular Architecture that saves roughly 200+ Development Hours! All Batteries Included.
https://fullstackhero.net/dotnet-webapi-boilerplate/
MIT License
5.22k stars 1.56k forks source link

[BUG] Automatic FluentValidation and Custom Validator #720

Closed mmarquesbr closed 1 year ago

mmarquesbr commented 2 years ago

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.

kirkey commented 2 years ago

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.

fretje commented 2 years ago

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 ;-)

iammukeshm commented 1 year ago

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.

kirkey commented 10 months ago

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> validators) : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest { public async Task Handle( TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) { var context = new ValidationContext(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();
}

}