micro-elements/MicroElements.Swashbuckle.FluentValidation

Can't make it reflect swagger schema at all

leandrobattochio opened this issue · 1 comments

I basically have the same problem as him: https://stackoverflow.com/questions/69880702/fluentvalidation-rules-are-not-reflected-in-swagger-through-microelements-librar

This is my Program.cs:


builder.Services.AddHttpContextAccessor();


builder.Services.AddControllers(opt =>
{
    opt.Filters.Add(new AppExceptionInterceptorAsync());
});

builder.Services.AddEndpointsApiExplorer();


builder.Services.AddValidatorsFromAssemblyContaining<Program>(lifetime: ServiceLifetime.Transient);
builder.Services.AddFluentValidationRulesToSwagger();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "You api title", Version = "v1" });
});



var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

My class and validator:

    public class LoginInputDto
    {
        public string Email { get; set; } = string.Empty;
        public string Password { get; set; } = string.Empty;
    }


    public class LoginInputDtoValidator : AbstractValidator<LoginInputDto>
    {
        public LoginInputDtoValidator()
        {
            RuleFor(c => c.Email)
                .EmailAddress()
                .WithMessage("E-mail inválido");

            RuleFor(c => c.Email)
                .NotEmpty()
                .WithMessage("Email deve ser preenchido");

            RuleFor(c => c.Email)
                .NotNull()
                .WithMessage("Email deve ser preenchido");

            RuleFor(c => c.Password)
                .NotEmpty()
                .WithMessage("Senha deve ser preenchido");
        }
    }

Nuget Packages:

FluentValidation" Version="11.5.2"
FluentValidation.DependencyInjectionExtensions" Version="11.5.2"
MicroElements.Swashbuckle.FluentValidation" Version="5.7.0"
Microsoft.AspNetCore.OpenApi" Version="7.0.4"
Swashbuckle.AspNetCore" Version="6.4.0"
Swashbuckle.AspNetCore.Newtonsoft" Version="6.5.0"

It just does not like it and doesn't work. I tried to change the order of calling AddFluentValidationRulesToSwagger but didn't change anything.

image

What am I doing wrong?

Just fixed it by changing the version of MicroElements.Swashbuckle.FluentValidation to 6.0.0-beta.3