fullstackhero / dotnet-microservices-boilerplate

The Ultimate Microservices Starter Kit for .NET Developers!

Home Page:https://fullstackhero.net/dotnet-microservices-boilerplate/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Asyncronus validation does not work with this setup

baranacikgoz opened this issue · comments

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.

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