Ogu.FluentValidation.AspNetCore.Attribute is a library that extends FluentValidation with additional attributes to simplify validation in ASP.NET Core applications. It allows you to specify validation rules directly on the model properties using attributes, reducing the need for explicit validation logic in your controllers.
- Supports validation rules through attributes on model properties.
- Provides
[Validate]
and[ValidateAsync]
attribute to automatically validate model objects in controller actions. To escape validation[SkipValidate]
attribute can be used. - Provides
IInvalidValidationResponse
interface for controlling invalid validation responses. - Built on top of FluentValidation, so you still have the full power and flexibility of FluentValidation for more complex validation scenarios.
You can install the library via NuGet Package Manager:
dotnet add package Ogu.FluentValidation.AspNetCore.Attribute
Controller:
[HttpPost]
[Validate(typeof(UserModel))]
public IActionResult Post([FromBody]UserModel userModel)
{
...
}
Startup:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddScoped<IValidator<UserModel>, UserModelValidator>();
...
}
UserModelValidator:
public class UserModelValidator : AbstractValidator<UserModel>
{
public UserModelValidator()
{
RuleFor(u => u.Username).NotEmpty();
}
}
(optional):
You may customize validation failure response by using default implemented [IInvalidValidationResponse]
interface
public void ConfigureServices(IServiceCollection services)
{
...
services.AddInvalidValidationResponse(failures => BadRequest(failures));
...
}
Or you can implement [IInvalidValidationResponse]
interface
public class MyCustomizedInvalidValidationResponse : IInvalidValidationResponse
{
public IActionResult GetResult(object model, List<ValidationFailure> validationFailures)
{
return new BadRequestObjectResult(validationFailures);
}
public async Task<IActionResult> GetResultAsync(object model, List<ValidationFailure> validationFailures,
CancellationToken cancellationToken = default)
{
return await Task.FromResult(new BadRequestObjectResult(validationFailures));
}
}
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<IInvalidValidationResponse, MyCustomizedInvalidValidationResponse>();
...
}