how to make fluentvalidation return detailed error message?

984 views Asked by At

I am using FluentValidation to validate my request and return errors if things are not correct. My code is like this:

public class GovernanceRequestValiator : AbstractValidator<GovernanceRequest>
{
    public GovernanceRequestValiator(IGovernanceFieldProvider govFieldsProvider)
    {
        RuleFor(
            x =>
        x.IssuerIds)
            .NotEmpty().Must(
            codes => !codes.Any(x => string.IsNullOrWhiteSpace(x) && codes.Count <= 100)
            )
            .WithMessage("IssuerIds can't be empty or more than 100");
        
        RuleFor(x => x)
            .Must(x => x.SelectedFields.Count ==0 || x.SelectedFields.Except(govFieldsProvider.DTODictionary.Keys, StringComparer.OrdinalIgnoreCase).Count() ==0)
            .WithMessage("Selected fields contain unrecognised fields");
    }
}

Ideally I want my error message to be more specific, for example, when SelectedFields contain unrecognised fields, I want to return a list of these fields to the caller, like Fields "aaa","bbb", "ccc" can't be recognised. With FluentValidation, I haven't found a way to achieve this.

0

There are 0 answers