FluentValidation规则未反映在swagger模式上

k2arahey  于 2023-03-14  发布在  其他
关注(0)|答案(1)|浏览(163)

我基本上和他有同样的问题:Fluentvalidation Rules are not reflected in Swagger through MicroElements library
这是我的程序. 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();

我的类和验证器:

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软件包:
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"
它只是不喜欢它,不工作。我尝试更改调用AddFluentValidationRulesToSwagger的顺序,但没有更改任何内容。

我做错了什么?看了看文档,要么已经过时,要么根本没有帮助。在其他地方也没有找到太多的信息。

vdzxcuhz

vdzxcuhz1#

只是通过将MicroElements.Swashbuckle.FluentValidation的版本更改为6.0.0-beta.3来修复它

相关问题