# `HL7v2.Validation`
[🔗](https://github.com/Balneario-de-Cofrentes/hl7v2/blob/v3.10.1/lib/hl7v2/validation.ex#L1)

Validates HL7v2 typed messages.

Opt-in validation that returns accumulated errors without blocking parsing.
Call `validate/1` on a `HL7v2.TypedMessage` to check message-level and
field-level rules.

## Examples

    {:ok, msg} = HL7v2.parse(text, mode: :typed)
    :ok = HL7v2.Validation.validate(msg)

    {:error, errors} = HL7v2.Validation.validate(invalid_msg)
    # errors is a list of %{level: :error | :warning, location: ..., field: ..., message: ...}

# `validate`

```elixir
@spec validate(
  HL7v2.TypedMessage.t(),
  keyword()
) :: :ok | {:error, [map()]} | {:ok, [map()]}
```

Validates a typed message.

Returns `:ok` when no issues are found, `{:ok, warnings}` when only
warnings are present (non-fatal), or `{:error, errors}` when errors exist.

Runs three validation passes:
1. **Message rules** — MSH presence, required MSH fields
2. **Structural rules** — segment ordering, group anchors, cardinality
   for all 186 official v2.5.1 message structures. Unsupported structures produce a
   warning in lenient mode or an error in strict mode.
3. **Field rules** — required fields, max repetitions per segment

Each error map has:
- `:level` — `:error` or `:warning`
- `:location` — segment identifier (e.g., `"MSH"`, `"PID"`, `"message"`)
- `:field` — field name atom or `nil` for structural issues
- `:message` — human-readable description

## Options

- `:mode` — `:lenient` (default) or `:strict`. In lenient mode, ordering
  and cardinality issues are warnings. In strict mode, all structural
  violations are errors.
- `:validate_tables` — `true` to check coded fields against HL7-defined
  tables. Defaults to `false`.
- `:version` — explicit HL7 version override (e.g. `"2.7"`). When provided,
  version-specific rules (B-field exemptions, etc.) use this value instead
  of the one extracted from MSH-12. Invalid or unrecognized versions fall
  back to the MSH-12 value. Defaults to `nil` (read MSH-12).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
