fullstackhero / dotnet-microservices-boilerplate

The Ultimate Microservices Starter Kit for .NET Developers!
https://fullstackhero.net/dotnet-microservices-boilerplate/
502 stars 120 forks source link

Asyncronus validation does not work with this setup #31

Closed baranacikgoz closed 1 year ago

baranacikgoz commented 1 year ago

Something like this

public sealed record AddTagToArticleCommand(Guid ArticleId, Guid TagId) : ICommand<ArticleQueryDto>;

public class AddTagToArticleCommandValidator : AbstractValidator<AddTagToArticleCommand>
{
    public AddTagToArticleCommandValidator(IArticleRepository articleRepository, ITagRepository tagRepository)
    {
        RuleFor(request => request.ArticleId)
            .NotEmpty()
            .WithMessage("ArticleId is required.")
            .MustAsync(articleRepository.ExistsAsync)
            .WithMessage("Article with the provided id does not exist.");

        RuleFor(request => request.TagId)
            .NotEmpty()
            .WithMessage("TagId is required.")
            .MustAsync(tagRepository.ExistsAsync)
            .WithMessage("Tag with the provided id does not exist.");
    }
}

With services.AddFluentValidationAutoValidation().AddFluentValidationClientsideAdapters();

Throws:

Validator "AddTagToArticleCommandValidator" 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.
iammukeshm commented 1 year ago

Hi, thanks for pointing out the issue. There doesn't seem to be a straight forward workaround for this. I have changed the application structure to use mediatrs pipeline behaviour and introduced a internal validatesync method. I have added a very small validator at create product to test this scenario. Please let me know