Validation in ASP.NET Core
We can use the default method of adding DataAnnotations
attributes (like
[Required]
), build custom DataAnnotations
or use FluentValidation package.
For ASP.NET Core we need to install FluentValidation.AspNetCore.
FluentValidations
It’s a 3rd part validation library that is an alternative for the default
DataAnnotations
approach.
Some advantages:
DataAnnotations
is based onAttribute
, so it doesn’t play well with the DI- We can create validations that use multiple properties of an object (e.g.
StartDate
andEndDate
) - Easier to test
- Validation logic is outside of the models
Registration:
services.AddValidatorsFromAssemblyContaining<SomeDataValidator>();
services.AddFluentValidationAutoValidation(options =>
{
options.DisableDataAnnotationsValidation = true;
});
Usage:
public class CommandValidator : AbstractValidator<Command>
{
public CommandValidator()
{
RuleFor(n => n.Title)
.NotEmpty()
.WithMessge("Title cannot be empty");
}
}