Skip to content

Support for nested object #162

@tryhp3

Description

@tryhp3

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();

#endregion

OpenApi3.0.1 -> swagger.json (validation on the swerver side works, but the docuemntation is wrong) SwaggerUi:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions