question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to set rules on collection elements.

See original GitHub issue

It would be nice to be able to recursively add rules on collection item properties.

      RuleForEach(s => s.Administrators)
        .RuleFor(c => c.Username, 
          r => r.NotEmpty()
                .EmailAddress());       

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Jun 5, 2015

Yes, this is doable. Instead of using RuleForEach, use RuleFor with a ChildCollectionValidatorAdaptor. This will keep the correct property names and indicies.

public static class MyValidatorExtensions {
    public static void NestedRules<T, TCollectionElement>(this IRuleBuilder<T, IEnumerable<TCollectionElement>> ruleBuilder, Action<InlineValidator<TCollectionElement>> setup) {
        var inlineValidator = new InlineValidator<TCollectionElement>();
        setup(inlineValidator);
        var adaptor = new ChildCollectionValidatorAdaptor(inlineValidator);
        ruleBuilder.SetValidator(adaptor);
    }
}

Useable like this:

public class MyValidator : AbstractValidator<Person> {
    public MyValidator() {
        RuleFor(x => x.Orders).NestedRules(v => {
            v.RuleFor(order => order.ProductName).NotNull();
            //etc
        });
    }
}
0reactions
shlok57commented, Jul 18, 2016

Is this functionality added?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Quality Specific Collection Elements
Oracle Quality provides you with three predefined collection element types, Attribute, Variable, and Reference, which are available as selections on a list of ......
Read more >
Set (mathematics)
A set is the mathematical model for a collection of different things; a set contains elements or members, which can be mathematical objects...
Read more >
How to Write the Elements of a Set from Rules or Patterns
The two main methods for describing a set are roster and rule (or set-builder). A roster is a list of the elements in...
Read more >
Power set - Definition, Examples, Formula, Properties and ...
A power set is the collection of all subsets, empty set and the original set itself. Learn power set notation, cardinality and properties...
Read more >
Business Rules Collection Data Types
Rule Editor divides all collections and arrays into two major categories: 1) collections of reference types and 2) collections of value types. Value...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found