Skip to content

Instantly share code, notes, and snippets.

@JeremySkinner
Created April 28, 2011 14:15
Show Gist options
  • Save JeremySkinner/946422 to your computer and use it in GitHub Desktop.
Save JeremySkinner/946422 to your computer and use it in GitHub Desktop.
FluentValidation syntax for conditional validation of collection elements
public class TestValidator : AbstractValidator<Customer> {
public TestValidator() {
RuleFor(cust => cust.Orders)
.SetCollectionValidator(new OrderValidator())
.IncludeWhere(order => order.Amount > 50);
//OR
RuleFor(cust => cust.Orders)
.SetCollectionValidator(new OrderValidator(), includeWhere: order => order.Amount > 50);
}
}
@davidalpert
Copy link

What about:

RuleFor(cust => cust.Orders).Where(order => order.Amount > 50).Apply<OrderValidator>().End();

or

RuleFor(cust => cust.Orders).Where(order => order.Amount > 50).Apply(new OrderValidator(someArgs)).End();

where the End() call functions like jQuery to escape up out of a nested set of rules.

Alternately, the Apply() could escape up the same way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment