使用Fluent Validator验证Bool

Leo*_*uda 4 nhibernate fluent-nhibernate fluentvalidation

我想使用流利的验证器验证bool属性.我应该使用哪种方法?

.NotNull()和.NotEmpty()函数不起作用.

谢谢.

Sta*_*tan 5

你应该用.NotNull().

NotEmpty()仅true作为有效财产.NotNull()将两者true并false为有效的属性.


小智 5

为了使用流畅的验证器验证布尔值:

创建以下规则

            RuleFor(x => x.BooleanProperty)
            .Must(ValidateBoolean)
            .WithErrorCode("Boolean Validation Failed");
Run Code Online (Sandbox Code Playgroud)

定义谓词验证器

        private bool ValidateBoolean(T arg1, bool arg2, PropertyValidatorContext arg3)
    {
        // validate arg2, return true if validation successful or false if failed
        throw new System.NotImplementedException();
    }
Run Code Online (Sandbox Code Playgroud)


sca*_*man -1

尝试使用.Equal(true)- That outta do it。