FluentValidation is not working in C#/.NET

47 views Asked by At

I'm trying to validate input parameter for my endpoints. I want to use FluentValidator and here is the logic

// This class is in in different assembly than Program.cs class
public class EIdLoginDtoValidator : AbstractValidator<EIdLoginDto>
{
    #region Constructors
    public EIdLoginDtoValidator()
    {
        RuleFor(x => x.EIdToken)
            .NotNull()
            .NotEmpty()
            .WithMessage("Some message");
    }

    #endregion
}

Then I do a registration of FluentValidator in Program.cs class like this:

builder.Services.AddFluentValidationAutoValidation();
builder.Services.AddValidatorsFromAssemblyContaining<EIdLoginDtoValidator>();

However when I debug my app breakpoint goes to EIdLoginDtoValidator which is good, but even EIdToken is null program executiong goes to endpoint. What I am doing wrong? I have installed FluentValidation.AspNetCore" Version="11.2.2" and I've using .net 8.

1

There are 1 answers

1
D A On

The FluentValidation.AspNetCore package is no longer being maintained. Check the link. So "AddFluentValidationAutoValidation" will not work. And on the fluentvalidation.net (the one you should use now) the automatic validation works only with ASP.NET, running on .NET Core 3.1, .NET 5 or .NET 6. Check the link.

For automatic validation with ASP.NET, FluentValidation supports ASP.NET running on .NET Core 3.1, .NET 5 or .NET 6.