# Module /validation

Declarative Struct field constraint validation (#{:min-len ... :format :email ...}) returning ValidationErrors.

```tangu
(use /validation)
```

## Structs

### Validation

`Struct` in `/validation`

Structured validation result returned by `(validation/validate x)`.

| Field | Type | Default | Notes |
| :--- | :--- | :--- | :--- |
| `ok` | `Bool` | `—` | — |
| `errors` | `Map` | `—` | — |

### ValidationErrors

`Struct` in `/validation`

Form/validation error container pairing field error arrays with submitted values.

| Field | Type | Default | Notes |
| :--- | :--- | :--- | :--- |
| `errors` | `Map` | `—` | — |
| `values` | `Map` | `—` | — |

## Functions & symbols

### Validation

`(Validation ok errors) -> Validation`

Validation outcome Struct returned by `(validation/validate x)`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `ok` | `Bool` | no | Overall validation success flag |
| `errors` | `Map` | no | Map of {:field [error-messages...]} |

```tangu
(let res (Validation true {}))
```

### ValidationErrors

`(ValidationErrors errors [values]) -> ValidationErrors`

Form/validation error container Struct pairing field errors with submitted input values.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `errors` | `Map` | no | Field error arrays keyed by field KeySymbol |
| `values` | `Map` | yes | Submitted raw input values |

```tangu
(ValidationErrors {:email ["Invalid email"]})
```

### get-cached-descriptor

`(get-cached-descriptor struct-type) -> (Or Any None)`

Looks up the precompiled native validation descriptor for `struct-type`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `struct_type` | `Type | Struct` | no | Struct definition or name |

```tangu
(validation/get-cached-descriptor User)
```

### is-valid?

`(is-valid? instance [schema]) -> Bool`

Returns `true` if `instance` satisfies every field constraint.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `instance` | `Any` | no | Struct instance or Map |
| `schema` | `Struct | Map` | yes | Optional target Struct descriptor |

```tangu
(assert! (validation/is-valid? user))
```

### validate

`(validate instance [schema]) -> Validation`

Validates all field constraints on `instance` and returns a `Validation` struct `{:ok Bool :errors {:field ["msg" ...]}}`.

| Parameter | Type | Optional | Description |
| :--- | :--- | :--- | :--- |
| `instance` | `Any` | no | Struct instance or Map to validate |
| `schema` | `Struct | Map` | yes | Optional target Struct descriptor when validating raw Maps |

```tangu
(let v (validation/validate signup-form))
```
