Skip to content

juntossomosmais/JSM.FluentValidation.AspNet.AsyncFilter

Repository files navigation

JSM.FluentValidation.AspNet.AsyncFilter

Quality Gate Status Coverage Nuget

FluentValidation documentation states:

You should not use asynchronous rules when using automatic validation with ASP.NET as ASP.NET’s validation pipeline is not asynchronous. If you use asynchronous rules with ASP.NET’s automatic validation, they will always be run synchronously.

This library provides an ASP.NET filter that performs async Validation using FluentValidation.

Get Started

To use the filter, first install the NuGet package:

dotnet add package JSM.FluentValidation.AspNet.AsyncFilter

Then, it's necessary to register the filter on the ConfigureServices portion of the Startup.cs.

services
    .AddControllers
    .AddModelValidationAsyncActionFilter();

The next step is to register the validators of the API:

services
    .AddScoped<IValidator<MyClass>, MyClassValidator>>();

Or use automatic registration.

Customization

If also possible to apply the filter only to controllers that contains the ApiControllerAttribute.

    ...
    .AddModelValidationAsyncActionFilter(options =>
    {
        options.OnlyApiController = true;
    });

This way, other controllers requests without the attribute will be ignored.

Contributing

See CONTRIBUTING.md.