Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I manually register FluentValidation validators, in ASP.NET Core?

I'm using ASP.NET Core, with the built-in container.

Registration is supposed to be done like so:

services
  .AddMvc()
  .AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>())

This automatically 1) configures FluentValidation, and 2) registers all validators found in the assembly.

But I want to register my validators manually. How do I do that?

like image 451
grokky Avatar asked Feb 17 '17 08:02

grokky


People also ask

How do I create a custom validation attribute in .NET core?

Validation attributes let you specify validation rules for model properties. We can create custom validation attributes, create a class that inherits from ValidationAttribute, and override the IsValid method. Here is the code. The IsValid method accepts an object named value, which is the input to be validated.

What is FluentValidation .NET core?

FluentValidation is a . NET library for building strongly-typed validation rules. FluentValidation 11 supports the following platforms: . NET Core 3.1.


1 Answers

// must first setup FV
services
    .AddMvc()
    .AddFluentValidation(fv => {});

// can then manually register validators
services.AddTransient<IValidator<Foo>, FooValidator>();
services.AddTransient<IValidator<Bar>, BarValidator>();
like image 67
grokky Avatar answered Oct 07 '22 15:10

grokky