I have been using (successfully) the following validation:
RuleFor(x => x.Items)
.SetCollectionValidator(new ItemValidator())
.Must(coll => coll.Sum(item => item.Percentage) == 100)
.When(x => x.Items != null);
As the above SetCollectionValidator
is (will be) deprecated, I changed it to:
RuleForEach(x => x.Items)
.SetValidator(new ItemValidator())
.Must(coll => coll.Sum(item => item.Percentage) == 100)
.When(x => x.Items != null);
However, Sum
is not recognized anymore.
How can I fix this?
You can use two separate rules. One of them is validate item, and other one is for validation of collection.
RuleForEach(x => x.Items)
.SetValidator(new ItemValidator());
RuleFor(x => x.Items)
.Must(coll => coll.Sum(item => item.Percentage) == 100)
.When(x => x.Items != null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With