Validate two properties with Fluent Validation

Viewed 52

I have this model class:

public class Model
{
    public string FirstProperty { get; set; }
    public string SecondProperty { get; set; }
}

Validation should pass when:

  1. FirstProperty value is not empty and SecondProperty value is empty
  2. SecondProperty value is not empty and FirstProperty value is empty
  3. FirstProperty value is empty and SecondProperty value is empty

I tried this:

RuleFor(x => x.FirstProperty)
.NotEmpty()
.When(x => string.IsNullOrEmpty(x.SecondProperty)

RuleFor(x => x.SecondProperty)
.NotEmpty()
.When(x => string.IsNullOrEmpty(x.FirstProperty)

But how to applied also third requirement (Both properties could be empty) ?

0 Answers
Related