micro-elements / MicroElements.Swashbuckle.FluentValidation

Use FluentValidation rules instead of ComponentModel attributes
MIT License
379 stars 61 forks source link

Make MicroElements.Swashbuckle.FluentValidation less host and generator dependent #63

Open petriashev opened 4 years ago

petriashev commented 4 years ago

There are many requests to support other hosts (AzureFunctions) or other generators (NSwag). Package should be splitted to several parts.

Supposed parts:

Supposed packages:

vbakc commented 4 years ago

Is it possible to make this package work without registering FluentValidation in Startup.cs? Registering FluentValidation leads to following issues (correct me if it's wrong):

  1. You are not able to use async validations Because to use them you have to call ValidateAsync method on validator object.
  2. You are not able to get ValidationResult.Errors (to me they are better than standard ASP.NET Core error response)
buvinghausen commented 4 years ago

@ChillerShock you are correct the built in MVC pipeline does everything synchronously therefore it is ultimately not a good choice for FluentValidation for anything but very rudimentary options.

The root of the issue is this library is dependent upon a IValidatorFactory instance which doesn't happen if you don't use the FluentValidation.AspNetCore package.

Just add the following line and you'll be good to go...

services.AddTransient<IValidatorFactory, HttpContextServiceProviderValidatorFactory>();

Edit: If you haven't already registered the HttpContextAccessor you need to do that as well

services.AddHttpContextAccessor();
buvinghausen commented 4 years ago

@petriashev I agree on all fronts.... this package is too opinionated as currently constructed thought it is incredibly useful and valuable.

petriashev commented 3 years ago

Started refactoring toward this issue