> ## 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.

# Constraints

> Every constraint, by the type of value it checks, and the conventions they all follow.

A constraint is a rule a value must pass, called inside the block of a property: `email()`, `minLength(3)`, `cpf()`.
Each one is an extension of `ValidationNode<V>`, so it is offered only where the value has the type it checks: `email()`
on a `String`, `positive()` on a number, `past()` on a date.

```kotlin theme={null}
::email required { email() }                       // String
::amount required { positive() }                    // BigDecimal
::items required { notEmpty() }                     // List<Item>
::birthDate required { minAge(18) }                 // LocalDate
```

## By type of value

> [card] [Any value](https://validity-kt.dev/constraints/any.md)
>
> `equalTo`, `oneOf`, `noneOf`, `notNull`

> [card] [Booleans](https://validity-kt.dev/constraints/booleans.md)
>
> `isTrue`, `isFalse`

> [card] [Strings](https://validity-kt.dev/constraints/strings.md)
>
> Length, blank, characters, `matches`

> [card] [Numbers](https://validity-kt.dev/constraints/numbers.md)
>
> Range, sign, digits, decimal places, unsigned

> [card] [Collections](https://validity-kt.dev/constraints/collections.md)
>
> Size, content, duplicates

> [card] [Maps](https://validity-kt.dev/constraints/maps.md)
>
> Size, keys, values

> [card] [Dates and times](https://validity-kt.dev/constraints/dates-and-times.md)
>
> `past`, `future`, `after`, `between`

> [card] [Calendar](https://validity-kt.dev/constraints/calendar.md)
>
> `minAge`, `today`, `withinNext`, `weekday`

> [card] [Durations](https://validity-kt.dev/constraints/durations.md)
>
> `kotlin.time.Duration` ranges

## By format of text

> [card] [Internet](https://validity-kt.dev/constraints/internet.md)
>
> `email`, `url`, `ipv4`, `cidr`, `jwt`

> [card] [Identifiers](https://validity-kt.dev/constraints/identifiers.md)
>
> `uuid`, `isbn`, `ean`, `imei`, `hash`

> [card] [Encodings](https://validity-kt.dev/constraints/encodings.md)
>
> `base64`, `hexadecimal`, `json`

> [card] [Finance](https://validity-kt.dev/constraints/finance.md)
>
> `creditCard`, `iban`, `bic`, `currencyCode`

> [card] [Location](https://validity-kt.dev/constraints/location.md)
>
> Countries, locales, `postalCode` of 72 countries

> [card] [Colors](https://validity-kt.dev/constraints/colors.md)
>
> `hexColor`, `rgbColor`, `hslColor`

> [card] [Date formats](https://validity-kt.dev/constraints/date-formats.md)
>
> `isoDate`, `isoDateTime`, `dateFormat`

> [card] [Countries](https://validity-kt.dev/constraints/countries.md)
>
> Documents of 14 countries: `cpf`, `dni`, `ein`...

> [card] [Phone numbers](https://validity-kt.dev/constraints/phone.md)
>
> `phone`, `mobilePhone`, in `validity-phone`

## What every constraint has in common

> [accordion] A code, which is its name
>
> The violation of `minLength` has the code `minLength`, in every language and every version. Programs branch on the
> code; people read the message.

> [accordion] A default message you can change
>
> Every constraint takes a `message`, which replaces the default for that call: `minLength(3, message = "too
>         short")`. The default itself is changed once, in [the messages](https://validity-kt.dev/guides/messages.md). Each section of these pages says
> which message to change.

> [accordion] A message built from the value
>
> A constraint that takes an argument has a second form whose message is a function of the value and the argument:
> `minLength(3, message = { min -> "has $length characters, needs $min" })`.

> [accordion] Null arguments check nothing
>
> `max(customer.creditLimit)`, `equalTo(password)` or `postalCode(country)` with a `null` argument skip the check. The
> missing value is reported once, by `required` on its own property, instead of breaking every rule that reads it.

> [accordion] Placeholders in the message
>
> Messages fill `{path}`, `{value}` and the arguments of the constraint by name: `must have at least {min}
>         characters`.

## Reading a section

Each constraint on these pages has the same parts:

```text theme={null}
minLength(min) on String                              the call, and the types it works on
Checks that the string has at least min characters.   what it checks
Code minLength · Message must have at least {min} characters · Change it minLength = "…"
```

**Change it** is the line to write inside `messages { }` to change its default message, for the whole application or
for one call. See [Messages](https://validity-kt.dev/guides/messages.md).

## Imports

The general constraints are in `io.github.soleworks.validity.constraints`, the constraints of a country in a package of
their own, such as `io.github.soleworks.validity.constraints.brazil`, and the phone constraints in
`io.github.soleworks.validity.phone`.

> [!TIP]
>
> Missing one? A constraint of your own is a few lines and looks exactly like these. See
> [Custom constraints](https://validity-kt.dev/guides/custom-constraints.md).


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