> ## Documentation Index
> Fetch the complete documentation index at: https://validity-kt.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Collections

> Size, content and duplicates of lists, sets and arrays.

The constraints of a collection as a whole. The size rules work on any `Collection`, on arrays and on `ByteArray`; the
content rules on `List`, `Set` and arrays. To check every element, use [`each`](https://validity-kt.dev/guides/collections-and-maps).

> [!NOTE]
>
> A `null` argument checks nothing. A rule that reads a value that may be missing, such as `max(customer.limit)`,
> never fails because of it: `required` on that value reports it once.

```kotlin Playlist.kt theme={null}
private val BANNED_TAGS = listOf("spam", "nsfw")

data class Playlist(
    val tracks: List<Track>?,
    val tags: Set<String>? = null
) : Validatable {
    override fun validation() = validation {
        ::tracks required {
            sizeBetween(min = 1, max = 200)
            distinctBy { it.id }
        }
        ::tags ifPresent {
            containsNone(BANNED_TAGS)
            each { notBlank() }
        }
    }
}
```

| Constraint | Checks |
| - | - |
| [`minSize`](#minsize) | Has at least `min` elements. |
| [`maxSize`](#maxsize) | Has at most `max` elements. |
| [`size`](#size) | Has exactly `size` elements. |
| [`sizeBetween`](#sizebetween) | Has between `min` and `max` elements, both included. |
| [`notEmpty`](#notempty) | Has at least one element. |
| [`empty`](#empty) | Has no elements. |
| [`contains`](#contains) | Contains `element`. |
| [`notContains`](#notcontains) | Does not contain `element`. |
| [`containsAll`](#containsall) | Contains every one of `elements`. |
| [`containsAny`](#containsany) | Contains at least one of `elements`. |
| [`containsNone`](#containsnone) | Contains none of `elements`. |
| [`distinct`](#distinct) | Has no element twice, by `equals`. |
| [`distinctBy`](#distinctby) | No two elements of the list have the same key, as `selector` picks it. |

## How many

### `minSize`

`minSize(min)` on `Collection`, `Array`, `ByteArray`

Checks that the collection has at least `min` elements.

**Code** `minSize`· **Message** `must have at least {min} items` · **Change it** `minSize = "…"`

### `maxSize`

`maxSize(max)` on `Collection`, `Array`, `ByteArray`

Checks that the collection has at most `max` elements.

**Code** `maxSize`· **Message** `must have at most {max} items` · **Change it** `maxSize = "…"`

### `size`

`size(size)` on `Collection`, `Array`, `ByteArray`

Checks that the collection has exactly `size` elements.

**Code** `size`· **Message** `must have exactly {size} items` · **Change it** `size = "…"`

### `sizeBetween`

`sizeBetween(min, max)` on `Collection`, `Array`, `ByteArray`

Checks that the collection has between `min` and `max` elements, both included.

**Code** `sizeBetween`· **Message** `must have between {min} and {max} items` · **Change it** `sizeBetween = "…"`

### `notEmpty`

`notEmpty()` on `Collection`, `Array`, `ByteArray`

Checks that the collection has at least one element.

**Code** `notEmpty`· **Message** `must not be empty` · **Change it** `notEmpty = "…"`

### `empty`

`empty()` on `Collection`, `Array`, `ByteArray`

Checks that the collection has no elements.

**Code** `empty`· **Message** `must be empty` · **Change it** `empty = "…"`

## Content

### `contains`

`contains(element)` on `List`, `Set`, `Array`

Checks that the list contains `element`.

**Code** `contains`· **Message** `must contain {element}` · **Change it** `containsElement = "…"`

### `notContains`

`notContains(element)` on `List`, `Set`, `Array`

Checks that the list does not contain `element`.

**Code** `notContains`· **Message** `must not contain {element}` · **Change it** `notContainsElement = "…"`

### `containsAll`

`containsAll(elements)` on `List`, `Set`, `Array`

Checks that the list contains every one of `elements`.

**Code** `containsAll`· **Message** `must contain all of {elements}` · **Change it** `containsAll = "…"`

### `containsAny`

`containsAny(elements)` on `List`, `Set`, `Array`

Checks that the list contains at least one of `elements`.

**Code** `containsAny`· **Message** `must contain any of {elements}` · **Change it** `containsAny = "…"`

### `containsNone`

`containsNone(elements)` on `List`, `Set`, `Array`

Checks that the list contains none of `elements`.

**Code** `containsNone`· **Message** `must not contain any of {elements}` · **Change it** `containsNone = "…"`

## Duplicates

### `distinct`

`distinct()` on `Collection`, `Array`

Checks that the collection has no element twice, by `equals`.

**Code** `distinct`· **Message** `must not contain duplicates` · **Change it** `distinct = "…"`

### `distinctBy`

`distinctBy(selector)` on `List`, `Set`, `Array`

Checks that no two elements of the list have the same key, as `selector` picks it.

**Code** `distinctBy`


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.