-
-
Notifications
You must be signed in to change notification settings - Fork 70
Open
Description
See example below. The version for Data Annotaons works correctly. The version for Fluent does not work.
.NET 8.0, FluentValidation 11.11.0, MicroElements.Swashbuckle.FluentValidation 6.1.0, Swagger 7.3.2
[ApiController]
[Route("[controller]/[action]")]
public class TestController : ControllerBase
{
public TestController() { }
[HttpGet]
public void FluentTest([FromQuery] FluentTest fv) { }
[HttpGet]
public void DataAnnotationTest([FromQuery] DataAnnotationTest da) { }
}
#region FluentValidation
public class FluentTest
{
public string Par { get; set; }
public FluentOperation Operation { get; set; }
}
public class FluentOperation
{
public string Op { get; set; }
}
public class FluentTestValidator : AbstractValidator<FluentTest>
{
public FluentTestValidator()
{
RuleFor(sample => sample.Par).NotEmpty();
RuleFor(sample => sample.Operation).NotNull().SetValidator(new FluentOperationValidator());
}
}
public class FluentOperationValidator : AbstractValidator<FluentOperation>
{
public FluentOperationValidator()
{
RuleFor(sample => sample.Op).NotEmpty();
}
}
#endregion
#region DataAnnotation
public class DataAnnotationTest
{
[Required]
public string Par { get; set; }
[Required]
public DataAnnotationOperation Operation { get; set; }
}
public class DataAnnotationOperation
{
[Required]
public string Op { get; set; }
}
//Program.cs add:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<IValidator<FluentTest>, FluentTestValidator>();
builder.Services.AddScoped<IValidator<FluentOperation>, FluentOperationValidator>();
builder.Services.AddFluentValidationRulesToSwagger();
#endregionOpenApi3.0.1 -> swagger.json (validation on the swerver side works, but the docuemntation is wrong) SwaggerUi:

Metadata
Metadata
Assignees
Labels
No labels