On one of my projects we are using FluentValidation. After upgrading to FluentValidation 8 the way we had to validate our collections changed.
Before we were using this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RuleFor(m => m.ToAddresses).SetCollectionValidator(new MailAddressValidator()); |
Now we had to use the RuleForEach method:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RuleForEach(m => m.ToAddresses).SetValidator(new MailAddressValidator()); | |
But what if you also want to validate if the collection itself is empty or not. This is still possible through RuleFor:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RuleFor(m => m.ToAddresses).NotEmpty().WithMessage("You need to set at least one to address"); |