micro-elements / MicroElements.Swashbuckle.FluentValidation

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

Configure to work with manual validation #112

Open wonea opened 2 years ago

wonea commented 2 years ago

Hi, I'm currently moving an API from FluentValidation 10 to 11, removing automatic validation and doing manual validation via an IAsyncActionFilter. This is because there is quite a lot of async validation throughout the API.

The IAsyncActionFilter is working well however MicroElements.Swashbuckle.FluentValidation is no longer decorating the Swagger pages with validation. How can I configure MicroElements.Swashbuckle.FluentValidation to work with the service provider.

https://docs.fluentvalidation.net/en/latest/aspnet.html https://github.com/FluentValidation/FluentValidation/issues/1961

Add validators to the service collection

services.AddValidatorsFromAssemblies(typeof(MyClass).Assembly);

I can get the annotations working using the following line, however now I get deprecation warnings in my code

services.TryAddTransient<IValidatorFactory, ServiceProviderValidatorFactory>();
petriashev commented 2 years ago

I like to work in my vacations fo free... Right now I am doing migration to FV11

wonea commented 2 years ago

It's cool, thanks for your hard work. I've worked around it for now.

petriashev commented 2 years ago

Please check 6.0.0-beta.1

tnlthanzeel commented 2 years ago

services.TryAddTransient<IValidatorFactory, ServiceProviderValidatorFactory>();

hi @wonea , im also having the same problem. im using asp co6 6. whats the work around you are using?

cremor commented 2 years ago

That quoted line works fine. Just suppress or ignore the obsolete warnings. Or update to version 6.0.0-beta.1 of this NuGet package.

tnlthanzeel commented 2 years ago

yes it worked. but then i updated the fluent validation to the latest version and now again the swagger UI does not show the required fields. any possible help please

That quoted line works fine. Just suppress or ignore the obsolete warnings. Or update to version 6.0.0-beta.1 of this NuGet package.

yes it worked. but then i updated the fluent validation to the latest version and now again the swagger UI does not show the required fields. any possible help please

wonea commented 2 years ago

At the moment and I'm using the following

I'll update those dependencies when my project constraints allow.

Those action filter approach is a much better way of working, it allows better debug access rather than using automatic validation from the non-async AspNet validation request pipeline.

EniacMlezi commented 1 year ago

What's the status on this? I'm currently trying to use this package in my minimal api project that uses Manual validation, but the attributes don't seem to be getting added to swagger (Using 6.0.0-beta.3)

petriashev commented 1 year ago

Here is minimal API example: https://github.com/micro-elements/MicroElements.Swashbuckle.FluentValidation/tree/master/samples/MinimalApi @EniacMlezi Please provide example of your project, maybe some repro

EniacMlezi commented 1 year ago

Finally had time again today to check this out. Making a repro I found out 6.0.0-beta.3 did indeed work for me, validations got when using Manual validation (not using FluentValidation.AspNetCore package). My problem was with nested property validations not showing up in swagger, making it look like it didn't work.

I'll be making a different issue for my nested validations problem.