Reference:
https://docs.fluentvalidation.net/en/latest/installation.html
https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-9.0
Thanks for providing learning code from Zack.Yang
FluentValidation is a .NET library for building strongly-typed validation rules.
The FluentValidation.AspNetCore package is no longer being maintained and is now unsupported. We encourage users move away from this package and use the core FluentValidation package with a manual validation
1.Install NuGet package:
FluentValidation
if you want to let FluentValidation automatically find all the validators in a specific assembly using an extension method. You should install another NuGet package:
FluentValidation.DependencyInjectionExtensions
2.Register validators
services.AddScoped<IValidator<Person>, PersonValidator>();
Or (DependencyInjectionExtensions)
services.AddValidatorsFromAssemblyContaining<UserValidator>();
3.Define a set of validation rules for a class(e.g. Person) by inheriting from AbstractValidator
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public int Age { get; set; }
}
public class PersonValidator : AbstractValidator<Person>
{
public PersonValidator()
{
RuleFor(p => p.Id).NotNull();
RuleFor(p => p.Name).Length(5, 10);
RuleFor(p => p.Email).EmailAddress();
RuleFor(p => p.Age).InclusiveBetween(18, 60);
// if the field is a complex properties, you can set another
// validator for it
// RuleFor(p => p.Address).SetValidator(new AddressValidator)
}
}
4.Manual Validation
[HttpPost]
public IActionResult Validate([GuidValidator]Guid guid, Person person)
{
var result = validator.Validate(person);
if (!result.IsValid)
{
result.AddToModelState(this.ModelState);
return BadRequest(ModelState);
}
return Ok("Validate person ok");
}
Besides, For scenarios that the build-in validation attributes don't handle, you can create custom validation attributes.
1.Create a class that inherits from ValidationAttribute, and override the IsValid method
public class GuidValidatorAttribute : ValidationAttribute
{
public const string DefaultErrorMessage = "The {0} field is required and not Guid.Empty";
public GuidValidatorAttribute() : base(DefaultErrorMessage) { }
public override bool IsValid(object? value)
{
if (value is not null && value is Guid)
{
Guid guid = (Guid)value;
return guid != Guid.Empty;
}
else
{
return false;
}
}
}
2.Add this attribute to parameter
public IActionResult Validate([GuidValidator]Guid guid, Person person)
{
}
Top comments (0)