I want to validate a bool property using fluent validator. Which method should I use?
.NotNull() and .NotEmpty() functions didn't work.
Thanks.
You should use .NotNull()
.
NotEmpty()
will take only true
as valid property. NotNull()
will take both true
and false
as valid properties.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With