Fluentvalidation Validator用于多个可能的值

时间:2018-01-10 06:21:49

标签: c# fluentvalidation

Fluentvalidation是否有任何内置函数来验证多个可能的传递/失败选项,类似于SQL In运算符?

例如:在下面的代码中,我需要确保user_status必须是(“Active”,“Expired”,“Blocked”............)等。

对于user_role,它应该是user_role(“admin”,“manager”,“viewer”......)

public class Users
    {

        public string user_status { get; set; }
        public string user_role { get; set; }

    }

    public class UpdateUserValidator : NullReferenceAbstractValidator<Users>
    {
        public UpdateUserValidator()
        {
            RuleFor(user => user.user_status).NotNull();

        }       
    }

1 个答案:

答案 0 :(得分:0)

As far as I know, there's no built-in validator doing this.

You could either using a .Must and a collection of accepted values.

Something like that (of course, the collection of accepted values could be in a variable, and used in the must and message part).

RuleFor(x =>x.user_status)
.Must(x => new[]{"Active", "Expired", "Blocked"}.Contains(x))
.WithMessage("User status should be  either 'active', 'expired' or 'blocked'");

Or you could of course create your own custom validator.