Is there a way to enforce the filling of the ErrorCode in FluentValidation?

49 Views Asked by At

I want to use FluentValidation in my API and I want to enforce every rule to get its ErrorCode defined, it cannot be "optional".

My goal is to provide a "dictionary" of errors making it easier for the client to implement its own handling and localization of error messages returned.

Is there any way to accomplish that?

1

There are 1 best solutions below

1
Habib Yousuf On

FluentValidation in ASP.NET Core allows you to create custom validators and rules, which means you can implement your own validation logic to enforce the filling of the ErrorCode property.

You can create a base class for your custom validators that enforces the ErrorCode property.

protected CustomValidatorBase()
    {
        RuleFor(x => x.ErrorCode).NotEmpty().WithMessage("ErrorCode must be specified.");
    }