Skip to main content
A collection has two kinds of rules: rules about the collection itself, such as its size, and rules about each of its elements. Both go in the block of the property:
CartItem.kt
Cart.kt

each

each { } runs its block on every element, and the path of an element is the path of the collection and its index: items[0], coupons[2]. It works on any Iterable, such as a List or a Set, and on arrays. The block is a normal block of constraints, so it can hold valid() for objects, or another each for a list of lists:

Maps

On a map, each runs on every entry, as a Map.Entry. Most rules are about only the keys or only the values, and two shortcuts give the block the right type: Every path uses the key, so a violation about a key and one about its value point at the same entry.

Rules about the collection

The constraints of the collection itself sit next to each:

Size

minSize, maxSize, size, sizeBetween, notEmpty, empty

Content

contains, containsAll, containsAny, containsNone, distinct, distinctBy
Maps have their own: containsKey, onlyKeys, containsValue and the size rules, which count entries.

The order of the violations

Inside one property, the rules of the collection come first, then the violations of its elements, in the order of the elements. That is why distinct comes before coupons[2] above.

Elements that can be null

In a List<String?>, an element can be null, and the block of each receives a String?, which the string constraints do not accept. Check it with notNull():