Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent Validations RuleForEach When

I have a collection validator class which, overrides the validate function to load data. I'm trying to add a when on a rule for each that applies to the item passed in and not the collection

public class ACAStatusAdjustmentCollectionValidator 
   : BaseValidator<ItemCollection<IEmployeeACAStatus>>
{
    public ACAStatusAdjustmentCollectionValidator(ISecureReadContext isolatedContext) : base(isolatedContext)
    {
        this.RuleForEach(x => x.Items).Must(this.SomeValidation)
        //This when is based off of the collection items instead of the item itself
        //.When(x => x.);
    }
}

In my scenario I cannot use a collection validator. How can I specify a when On in the rule foreach, without checking my when Inside of the function

like image 204
johnny 5 Avatar asked May 27 '26 06:05

johnny 5


1 Answers

Had the same question today and found the answer. One can use the .Where() filter for that:

this.RuleForEach(x => x.Items).Where(item => <condition>).Must(this.SomeValidation)
like image 172
Andrey Stukalin Avatar answered May 30 '26 08:05

Andrey Stukalin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!