Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using FluentValidation instead of FluentValidation.AspNetCore #97

Closed
lonix1 opened this issue Oct 13, 2021 · 10 comments · Fixed by #98
Closed

Using FluentValidation instead of FluentValidation.AspNetCore #97

lonix1 opened this issue Oct 13, 2021 · 10 comments · Fixed by #98

Comments

@lonix1
Copy link
Contributor

lonix1 commented Oct 13, 2021

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
Copy link
Contributor Author

lonix1 commented Oct 14, 2021

TL;DR

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

@petriashev
Copy link
Member

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
Copy link
Contributor Author

lonix1 commented Oct 22, 2021

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

  • when using FluentValidation.AspNetCore library, it automatically registers ServiceProviderValidatorFactory as implementation of IValidatorFactory
  • but when using the basic FluentValidation.DependencyInjectionExtensions library, that is not registered
  • so we must manually register it like so:
    services.TryAddTransient<IValidatorFactory, ServiceProviderValidatorFactory>();
    services.AddFluentValidationRulesToSwagger();

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

@petriashev
Copy link
Member

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

@lonix1 lonix1 mentioned this issue Oct 22, 2021
@lonix1
Copy link
Contributor Author

lonix1 commented Oct 22, 2021

@petriashev Ok see latest PR when you get a chance

@CollinAlpert
Copy link

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

@petriashev
Copy link
Member

petriashev commented Sep 5, 2022

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
Copy link

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
Copy link
Member

@CollinAlpert Try 6.0.0-beta.1

@CollinAlpert
Copy link

Thanks so much, that fixed it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants