Created
April 28, 2011 14:15
-
-
Save JeremySkinner/946422 to your computer and use it in GitHub Desktop.
FluentValidation syntax for conditional validation of collection elements
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
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.