question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding support for FluentValidations and swagger schema.

See original GitHub issue

I wish to register FluentValidation rules, so that they show up in swagger schema. Given the sample code below:

public class Validator : AbstractValidator<ClosureScheduleCommand> 
{
    public Validator()
    {
        RuleFor(x => x.Warehouse).NotEmpty();
    }
}

We should be able to correctly mark the property as required. Example of the wanted schema presentation in Swagger image The required field is marked by a red asterix. In this example i relied on the attribute [JsonRequired]

Similar implementations in MVC aspnetcore 3.1:

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:17 (10 by maintainers)

github_iconTop GitHub Comments

6reactions
wjaxcommented, May 20, 2022

After some discussion with @dj-nitehawk we will keep the current support for a single validator against the request type of the endpoint. We will not make complex things injecting base validators automatically that could break the surface api or make things too complex.

Of course the requestDto can use inheritance and it will play nice with both the validator and the swagger support.

So, in case anyone needs to reuse a validator from a base type, he/she can use inheritance in the validator like this:

public class Request : BaseIdentifier
{
    public string? NullableIdentifier { get; set; }
    public string? Message { get; set; }
}

public class BaseIdentifier
{
    public string Identifier { get; set; }
    public string baseMessage { get; set; }
}

public class RequestValidator : BaseIdentifierValidator<Request>
{
    public RequestValidator()
    {
        RuleFor(x => x.Identifier).NotEmpty();
        RuleFor(x => x.NullableIdentifier).NotEmpty();
        RuleFor(x => x.Message).Length(3, 100);
    }
}

public class BaseIdentifierValidator<TDto> : Validator<TDto> where TDto : BaseIdentifier
{
    public BaseIdentifierValidator()
    {
        RuleFor(x => x.baseMessage).Length(10, 100);
    }
}
2reactions
andreas-sorokocommented, May 6, 2022

I am also highly interested in that feature 👍🏼

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding support for FluentValidations and swagger schema.
I wish to register FluentValidation rules, so that they show up in swagger schema. Given the sample code below: public class Validator ...
Read more >
Fluent Validation with Swagger in Asp.net Core
I've created github project and nuget package based on Mujahid Daud Khan answer. I made redesign to support extensibility and supported ...
Read more >
Making FluentValidation compatible with Swagger ...
Building Swagger schema based on FluentValdation rules​​ The schemaModel has some predefined structure, on which properties have to be set based ...
Read more >
How to Generate API Client Code Using NSwag ...
Get started with NSwag and ASP.NET Core by using FluentValidation rules to define Swagger schema in ASP.NET Core API.
Read more >
Fluent Validation with Swagger in Asp.net Core
So far i have tried the following code but i am unable to get validator type. services.AddSwaggerGen(options => options.SchemaFilter<AddFluentValidationRules>() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found