gpt4 book ai didi

asp.net-core - 在 Asp.net Core 中使用 Swagger 进行流畅验证

转载 作者:行者123 更新时间:2023-12-02 07:37:07 25 4
gpt4 key购买 nike

我目前在 Web API 中使用 Fluent Validation 而不是 Data Annotations,并使用 swagger 作为 API 文档。流畅的验证规则未反射(reflect)在 swagger 模型中,因为我无法使用 swagger 架构过滤器配置流畅的验证规则。

This Blog有关于将其与 ASP.net MVC 一起使用的很好的解释。但我无法配置它以在 ASP.net Core 中使用它。

到目前为止,我已经尝试了以下代码,但无法获取验证器类型。

services.AddSwaggerGen(options => options.SchemaFilter<AddFluentValidationRules>());

public class AddFluentValidationRules : ISchemaFilter
{
public void Apply(Schema model, SchemaFilterContext context)
{
model.Required = new List<string>();
var validator = GetValidator(type); // How?
var validatorDescriptor = validator.CreateDescriptor();

foreach (var key in model.Properties.Keys)
{
foreach (var propertyValidator in validatorDescriptor.GetValidatorsForMember(key))
{
// Add to model properties as in blog
}
}
}
}

最佳答案

我根据 Mujahid Daud Khan 的回答创建了 github 项目和 nuget 包。我进行了重新设计以支持可扩展性并支持其他验证器。

github:https://github.com/micro-elements/MicroElements.Swashbuckle.FluentValidation

nuget:https://www.nuget.org/packages/MicroElements.Swashbuckle.FluentValidation

注意:对于 WebApi,请参阅:https://github.com/micro-elements/MicroElements.Swashbuckle.FluentValidation.WebApi

支持的验证器

  • INotNullValidator(NotNull)
  • INotEmptyValidator(NotEmpty)
  • ILengthValidator(长度、最小长度、最大长度、精确长度)
  • IRegularExpressionValidator(电子邮件、匹配)
  • IComparisonValidator(GreaterThan、GreaterThanOrEqual、LessThan、LessThanOrEqual)
  • IBetweenValidator(InclusiveBetween、ExclusiveBetween)

用法

1。在您的 Web 项目中引用包:

<PackageReference Include="FluentValidation.AspNetCore" Version="7.5.2" />
<PackageReference Include="MicroElements.Swashbuckle.FluentValidation" Version="0.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="2.3.0" />

2。更改 Startup.cs

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services
.AddMvc()
// Adds fluent validators to Asp.net
.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<CustomerValidator>());

services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
// Adds fluent validation rules to swagger
c.AddFluentValidationRules();
});
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app
.UseMvc()
// Adds swagger
.UseSwagger();

// Adds swagger UI
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
}

Swagger 示例模型和验证器

public class Sample
{
public string PropertyWithNoRules { get; set; }

public string NotNull { get; set; }
public string NotEmpty { get; set; }
public string EmailAddress { get; set; }
public string RegexField { get; set; }

public int ValueInRange { get; set; }
public int ValueInRangeExclusive { get; set; }
}

public class SampleValidator : AbstractValidator<Sample>
{
public SampleValidator()
{
RuleFor(sample => sample.NotNull).NotNull();
RuleFor(sample => sample.NotEmpty).NotEmpty();
RuleFor(sample => sample.EmailAddress).EmailAddress();
RuleFor(sample => sample.RegexField).Matches(@"(\d{4})-(\d{2})-(\d{2})");

RuleFor(sample => sample.ValueInRange).GreaterThanOrEqualTo(5).LessThanOrEqualTo(10);
RuleFor(sample => sample.ValueInRangeExclusive).GreaterThan(5).LessThan(10);
}
}

欢迎添加问题!

关于asp.net-core - 在 Asp.net Core 中使用 Swagger 进行流畅验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44638195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com