.net.net-corefluentvalidation

How To register FluentValidation from multiple assemblies


How can i add fluentvalidation which have validation in multiple assemblies. I have validators also in a shared class Library:

    public class TestModelValidator : AbstractValidator<TestModel>
    {
      rules...
    }

    public class TestModel2Validator : AbstractValidator<TestModel2>
    {
      rules...
    }
...
    public class TestMode999Validator : AbstractValidator<TestMode999>
    {
      rules...
    }
in my startup.cs:
        services.AddValidatorsFromAssemblyContaining<Startup>();
        services.AddFluentValidationAutoValidation();
        services.AddFluentValidationClientsideAdapters();

Solution

  • Install package FluentValidation.AspNetCore

    Then you can use extension method like this:

     services.AddFluentValidation(fv =>
            {
                    fv.RegisterValidatorsFromAssembly(Assembly.GetExecutingAssembly());
                fv.RegisterValidatorsFromAssemblyContaining<Assembly1Class>();
                fv.RegisterValidatorsFromAssemblyContaining<Assembly2Class>();
            });
    

    Or you can use method fv.RegisterValidatorsFromAssemblies() and pass list of assemblies.