Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate Bool using Fluent Validator

I want to validate a bool property using fluent validator. Which method should I use?

.NotNull() and .NotEmpty() functions didn't work.

Thanks.

like image 843
Leonardo Arruda Avatar asked Mar 13 '13 14:03

Leonardo Arruda


2 Answers

You should use .NotNull().

NotEmpty() will take only true as valid property. NotNull() will take both true and false as valid properties.

like image 194
Stan Avatar answered Sep 22 '22 00:09

Stan


In order to validate a boolean using fluent validator:

Create the following rule

            RuleFor(x => x.BooleanProperty)
            .Must(ValidateBoolean)
            .WithErrorCode("Boolean Validation Failed");

Define the predicate validator

        private bool ValidateBoolean(T arg1, bool arg2, PropertyValidatorContext arg3)
    {
        // validate arg2, return true if validation successful or false if failed
        throw new System.NotImplementedException();
    }
like image 31
dummyDev Avatar answered Sep 18 '22 00:09

dummyDev