micro-elements / MicroElements.Swashbuckle.FluentValidation

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

Using FluentValidation instead of FluentValidation.AspNetCore #97

Closed lonix1 closed 3 years ago

lonix1 commented 3 years ago

Hello @petriashev, thanks for this great library! I've been using it for a while.

I am now using FluentValidation inside a Mediatr pipeline. So now I reference FluentValidation instead of FluentValidation.AspNetCore (otherwise I will get double validation, once by MVC and once inside Mediatr).

Of course that causes this warning:

WRN | MicroElements.Swashbuckle.FluentValidation.FluentValidationRules | ValidatorFactory is not provided. Please register FluentValidation.

And of course the validation details are not shown in Swagger.

However I am still using FluentValdation, just in a different way. My validator classes are still the same. The container still has registrations for all the validators.

How can I make this work?

lonix1 commented 3 years ago

TL;DR

Can this library work if I'm using FluentValidation instead of FluentValidation.AspNetCore?

petriashev commented 3 years ago

Can you create simple repro with Mediatr?

Main class FluentValidationRules uses DI for its dependencies. Try to register IValidatorFactory for example HttpContextServiceProviderValidatorFactory or standard ServiceProviderValidatorFactory

lonix1 commented 3 years ago

Thanks!!! Please tell me if I understand correctly?

If correct, it would be a good idea to add it to docs ("Common problems and workarounds").

petriashev commented 3 years ago

Yes you are right. It would be great if you make PR for that! ;)

lonix1 commented 3 years ago

@petriashev Ok see latest PR when you get a chance

CollinAlpert commented 2 years ago

When using this workaround, I am getting the following warning: CS0618: Interface 'FluentValidation.IValidatorFactory' is obsolete. Is this a known issue?

petriashev commented 2 years ago

When using this workaround, I am getting the following warning: CS0618: Interface 'FluentValidation.IValidatorFactory' is obsolete. Is this a known issue?

Yes, IValidatorFactory is obsolete. But it's not needed in latest version of package

CollinAlpert commented 2 years ago

What version is the "latest"? I am using Microelements.Swashbuckle.FluentValidation version 5.7.0 with FluentValidation.AspNetCore version 11.2.2 and get the message "ValidatorFactory is not provided. Please register FluentValidation." if I don't call services.TryAddTransient<IValidatorFactory, ServiceProviderValidatorFactory>();.

petriashev commented 2 years ago

@CollinAlpert Try 6.0.0-beta.1

CollinAlpert commented 2 years ago

Thanks so much, that fixed it!