Reputation: 285
I have the following fluent validation:
RuleFor(o => o.zipCode)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty()
.Must((o, zipCode) => CountryInfoMap.IsValidPostalCode(zipCode, "AT"))
.When(o => !string.IsNullOrEmpty(o.ZipCode))
.WithMessage(ErrorDto.ToModelMessage(ErrorCode.InvalidZipCode, $"{{PropertyName}} '{{PropertyValue}}' is invalid for specified country."));
When no zipCode
value is passed it still calls the method CountryInfoMap.IsValidPostalCode
which is a static method. Why doesn't it stop on first failure of it being empty?
Upvotes: 0
Views: 355
Reputation: 2639
Remove When
:
RuleFor(o => o.zipCode)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty()
.Must((o, zipCode) => CountryInfoMap.IsValidPostalCode(zipCode, "AT"))
.WithMessage(ErrorDto.ToModelMessage(ErrorCode.InvalidZipCode, $"{{PropertyName}} '{{PropertyValue}}' is invalid for specified country."));
you are already validating it, for it not being empty.
Upvotes: 1