Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Automatic FluentValidation and Custom Validator #720

Closed
mmarquesbr opened this issue Jun 29, 2022 · 4 comments
Closed

[BUG] Automatic FluentValidation and Custom Validator #720

mmarquesbr opened this issue Jun 29, 2022 · 4 comments

Comments

@mmarquesbr
Copy link

mmarquesbr commented Jun 29, 2022

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 mmarquesbr changed the title [BUG] [BUG] Automatic FluentValidation and Custom Validator Jun 29, 2022
@kirkey
Copy link

kirkey commented Jun 30, 2022

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
Copy link
Contributor

fretje commented Jun 30, 2022

Yes, it's a known problem (#639). I've started the work here #693. That PR can use some help ;-)

@iammukeshm
Copy link
Member

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
Copy link

kirkey commented Dec 23, 2023

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

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants