Lazar
Lazar

Reputation: 37

FluentValidation: validator without property

I have a case where if you have some settings in app.config, a Validator should always fail with some message (no matter of any property values). Is there a clean way to do this?

Right now I am using this code:

RuleFor(x => x.SomeRandomProperty).Must(srp => false).WithMessage("My message");

Upvotes: 2

Views: 1257

Answers (1)

Steven Yates
Steven Yates

Reputation: 2480

You can override Validate like this to check if the value exist and return a custom ValidationResult or stick to how you've done it above.

public override ValidationResult Validate(Person instance)
{

    if(ValueIsInConfigFile)
        return new ValidationResult(new List<ValidationFailure>(){new ValidationFailure("SomeProperty", "There is a value in the config file which made this fail")});
    return base.Validate(instance);//Will apply your normal Rules
}

Steve

Upvotes: 2

Related Questions