DEV Community

Alex
Alex

Posted on

.NET Learning Notes: How to use FluentValidation and Custom attributes to check parameters

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

2.Register validators

services.AddScoped<IValidator<Person>, PersonValidator>();
Enter fullscreen mode Exit fullscreen mode

Or (DependencyInjectionExtensions)

services.AddValidatorsFromAssemblyContaining<UserValidator>();
Enter fullscreen mode Exit fullscreen mode

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)
    }
}
Enter fullscreen mode Exit fullscreen mode

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");
}
Enter fullscreen mode Exit fullscreen mode

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;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

2.Add this attribute to parameter

public IActionResult Validate([GuidValidator]Guid guid, Person person)
{
}
Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay