I've chosen NSwag over Swashbuckle for my api because I really like how well the NSwag Studio is able to hit the swagger endpoint and simply create a C# client or a TypeScript client for my api. I've also chosen FluentValidation over DataAnnotations as my incoming request model validation since it's still quite easy to use even in complex validation scenarios. However, the downside of this choice is that the NSwag Swagger Generator does not seem to support reading / outputting the FluentValidation rules in the generated Swagger document.
Is there any chance that NSwag could be updated to support the FluentValidation rules?
Perhaps in an addon package similar to the one someone made for Swashbuckle if you are worried about bloat in the main generator package.
I think it shouldnt be that hard to rewrite Swashbuckle filter as an NSwag operation processor...
That would be awesome.
Any chance seeing this soon?
I'd love it!
+1
I'm trying to have something similar. If you look at the fluent validator docs you need a [Validator(typeof(PersonValidator))] attribute and a piece in your Controller
if(! ModelState.IsValid) { // re-render the view when validation failed.
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
I'm able to customize the Controller part in the liquid templates but I can't seem to find where I can customize the template to add the attribute.
@RSuter could that be done as well? I think I need an addition to Class.Annotations.liquid from NJsonSchema but setting that file in my custom templates folder doesn't seem to work.
@nojaf I think this should work - and you should be able to override the Class.Annotations.liquid file of NJS... maybe the template model does not expose enough information for you to generate the validator correctly - if this is the case we need to add more properties to the model in NJS
FYI: This here is about reading the validator rules to generate a correct swagger spec - but you are talking about generating validator rules in the controller code generator... maybe you should create a new issue for this.
Any news? (= Someone did it already or maybe find some solution for NSwag?
I ended up porting the MicroElements.Swashbuckle.FluentValidation package to NSwag in case anyone is still looking for a solution.
Github: https://github.com/zymlabs/nswag-fluentvalidation
NuGet: https://www.nuget.org/packages/ZymLabs.NSwag.FluentValidation/
I'd love to see @geoffreytran's library merged into this project to get first class support! thanks for writing it!
@geoffreytran heads up on some changes you'll need to consider if/when you update to FluentValidation 9, see the issue I linked yesterday to FluentValidation#1434. Also for anyone else who might have ported this themselves or have a similar library or want to work on this issue.
@bbarry Thanks for the heads up! I'll have a look as soon as I can and get those updates in to match.
Most helpful comment
I think it shouldnt be that hard to rewrite Swashbuckle filter as an NSwag operation processor...