0

I am trying to write validation rule for multiple string properties, to check whether they are unique. so i made validator to check it. _service method returns list of error codes, that correspond with properties which are not unique. Now how can I get response message or several in accordance with returned result codes?

RuleFor(x => x.User)
    .MustAsync(async (q, context, token) =>
    {
        var errors = await _service.CheckUserNameEmailExistsAsync(q.User.UserName, q.User.Email, token);
         return !errors.Any();
     }).WithMessage(...);
1
  • What is errors exactly? I'd make a sub-validator for User. Then you can check uniqueness of each of it's properties and add a failure to the context if appropriate. Commented May 31, 2021 at 13:56

1 Answer 1

2

You can go this way: Create a local variable of your type, and put the result there. And in "WithMessage" refer to it and display the message you need.

example:

            SomeType errors;

            RuleFor(x => x.User)
                .MustAsync(async (q, context, token) =>
                {
                    errors = await _service.CheckUserNameEmailExistsAsync(q.User.UserName, q.User.Email, token);
                    return !errors.Any();
                }).WithMessage((_) => 
                {
                    if (errors....)
                        return "some message";
                    else if (errors...)
                        return "other some message";
                    else
                        return "another some message";
                });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.