I have a property where I want to check if it is either empty or in a specific format. I know I could put both conditions in one regex, but using something like Empty().Or.Matches(...)
would be far easier to read - is something like that possible using FluentValdations?
So current code:
RuleFor(x => x.Property)
.Matches(@"^$|^[0-9]+$"); // did not check the regex, but for this example it should not matter
I would prefer something like that for better readability:
RuleFor(x => x.Property)
.Empty().Or().Matches(@"^[0-9]+$");
With the method's that are available now, it would have to by chaining or's ||
in the Must
method.
Regex regex = new Regex("\\b(jack|jill)\\b");
RuleFor(validateField => validateField.Name)
.Must(name => string.IsNullOrEmpty(name) || regex.IsMatch(name))
.WithMessage("Name must be empty or 'jack' or 'jill'");