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

# Quick start

> Declare the rules of a class, validate it and read every broken rule, in five minutes.

**1. Implement Validatable and declare the rules**

A class that knows its own rules implements `Validatable` and returns them from `validation()`. Each line starts
with a property, says what `null` means for it, and lists its constraints:

```kotlin User.kt theme={null}
data class User(
    val name: String?,
    val email: String?,
    val age: Int?
) : Validatable {
    override fun validation() = validation {
        ::name required { minLength(2) }
        ::email required { email() }
        ::age ifPresent { min(18) }
    }
}
```

`::name required { minLength(2) }` reads as the requirement: the name is required, and has at least 2
characters. `::age ifPresent { min(18) }` says the age may be missing, but when it is there, it is at least 18.

**2. Validate**

`validate()` runs every rule and returns a `ValidationResult`:

```kotlin theme={null}
val result = User(name = "A", email = "ana@", age = 16).validate()

result.isValid      // false
result.violations   // every broken rule, in the order the rules are declared
```

`validate()` is a `suspend` function, so a rule can query a database or an API. Outside a coroutine, call
`validateBlocking()` instead. It runs the same rules and blocks the thread until they finish.

**3. Read the violations**

The three rules are broken at once, and each violation has a path, a message and a code:

| Path | Message | Code |
| - | - | - |
| `name` | must have at least 2 characters | `minLength` |
| `email` | must be a valid email | `email` |
| `age` | must be at least 18 | `min` |

Show the `message` to people and branch on the `code` in programs: codes never change with the language of the
messages.

**4. Or throw at the edge of your system**

In a controller or a handler, `validateOrThrow()` throws a `ValidationException` that carries the same list, so
one error handler can turn every invalid request into a response:

```kotlin theme={null}
user.validateOrThrow()   // or validateOrThrowBlocking()
```

## What to read next

> [card] [What null means](https://validity-kt.dev/guides/null-values.md)
>
> `required`, `ifPresent` and `forbidden`, the start of every rule.

> [card] [Nested objects](https://validity-kt.dev/guides/nested-objects.md)
>
> `valid()` follows the data, and the path follows it too.

> [card] [Rules across fields](https://validity-kt.dev/guides/rules-across-fields.md)
>
> One of two fields, a total that matches its items.

> [card] [Every constraint](https://validity-kt.dev/constraints/overview.md)
>
> 175 of them, by the type of value they check.


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