Just wondering if there has been any thought on supporting detection of required Model properties when FluentValidation has been utilized and the use of AbstractValidators on Models? Maybe looking for NotNull rules?
-- simple validator
using FluentValidation;
public class PersonModelValidator : AbstractValidator
{
public PersonModelValidator()
{
this.RuleFor(contract => contract.FirstName).NotNull();
}
}
-- simple model
[Validator(typeof(PersonModelValidator))]
public class PersonModel : Person
{
public string FirstName { get; set; }
}
Actually, I don't think this would even be possible. Looking at your example, the rules are specified at run time (inside the validator constructor) and so there would be no simple way for Swashbuckle to determine this information statically.
Hmm. I think it might be possible... if a little complicated.
You should be able to ask for a validator for the type under inspection (PersonModel in this example) - that is ask for an IValidator
Once you have a validator object in hand, you should be able to examine the rules one by one, and programmatically work out whether the property is required...
Hmm, it is a fair amount of faffing around. But, I can certainly see the value of having a FluentValidation "plug in" for Swashbuckle/Swagger. I wonder if a) I have the time to do this, and b) can be bothered, and not just go about flinging the [Required] attribute everywhere (which is a horrible bastardisation of using DataAnnotations as well as FluentValidation, ugh!)
I came across this issue and this StackOverflow post while Googling how to include the fluent validation rules into the generated swagger.
@domaindrivendev Maybe now that a solution has been presented, it could be built into Swashbuckle instead of forcing each dev to implement a version of the scheme filter?
If anything, I would prefer to see this implemented as an independent library -e.g. Swashbuckle.FluentValidation that adds a simple extension method to SwaggerDocsConfig - e.g SupportFluentValidations(). I don鈥檛 want Swashbuckle Core to take on additional dependencies that only some people use.
This is my recommendation, however I don鈥檛 have the cycles to do the work so it鈥檚 going to have to be contributed from the community
I've created nuget package based on Mujahid Daud Khan answer on StackOwerflow.
See: https://www.nuget.org/packages/MicroElements.Swashbuckle.FluentValidation
Most helpful comment
Hmm. I think it might be possible... if a little complicated.
You should be able to ask for a validator for the type under inspection (PersonModel in this example) - that is ask for an IValidator from your DI container. Alternatively, you could reflect over the PersonModel class, look for the ValidatorAttribute, and instantiate the applicable validator class.
Once you have a validator object in hand, you should be able to examine the rules one by one, and programmatically work out whether the property is required...
Hmm, it is a fair amount of faffing around. But, I can certainly see the value of having a FluentValidation "plug in" for Swashbuckle/Swagger. I wonder if a) I have the time to do this, and b) can be bothered, and not just go about flinging the [Required] attribute everywhere (which is a horrible bastardisation of using DataAnnotations as well as FluentValidation, ugh!)