I have the below code which is throwing an exception for null check:
RuleFor(x => x.TestString)
.Must(x => !string.IsNullOrEmpty(x))
.When(y => y.OtherArray!= null && y.OtherArray.Count > 0)
.WithMessage("required")
.Must(x=> x.TestString.Equals("tiktok"))
.WithMessage("Invalid")
but the above code is throwing Object null reference exception when TestString is null.
My requirement is when "OtherArray" has a value, then "TestString" must be equal to "tiktok" when an invalid TestString Invalid error message should be shown.
When "OtherArray" is null or empty then no need to check the "TestString". Can anyone please help?
Try this:
RuleFor(x => x.TestString)
.Must(x => !string.IsNullOrEmpty(x))
.When(y => y.OtherArray!= null && y.OtherArray.Count > 0)
.WithMessage("required")
.Must(x=> x.TestString != null && x.TestString.Equals("tiktok"))
.WithMessage("Invalid")