Reputation: 26415
I want to set a validator on each element in a collection and also require that collection not be empty. It would be nice to do this all in one line, but I don't know if I can or how to do that. Right now my best guess is to write two rules (where Terms
is IReadOnlyCollection<TermData>
:
RuleFor(x => x.Terms).NotEmpty();
RuleForEach(x => x.Terms).SetValidator(new TermDataValidator());
Is there a way to unify these two into one rule?
Upvotes: 1
Views: 4201
Reputation: 258
Try this
RuleFor(x => x.Terms).NotEmpty().ForEach(x=>x.SetValidator(new TermDataValidator()));
Upvotes: 6