void.pointer
void.pointer

Reputation: 26415

Fluent Validation: RuleForEach and also require the collection not be empty?

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

Answers (1)

Loong
Loong

Reputation: 258

Try this

RuleFor(x => x.Terms).NotEmpty().ForEach(x=>x.SetValidator(new TermDataValidator()));

Upvotes: 6

Related Questions