> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fhiron.cl/llms.txt
> Use this file to discover all available pages before exploring further.

# Validar Patient con RUN (módulo 11)

> Patient chileno con identifier RUN, type.coding CSTipoIdentificador#01 y dígito verificador módulo 11.

## Escenario

Una clínica integra su sistema de admisión con un EHR externo y necesita enviar `Patient` con RUN como identificador nacional. El campo más sensible es el dígito verificador: el RUN `15420097-3` es válido, pero `15420097-2` no lo es. Inspect valida el DV localmente con la regla `cl-run-03` y obliga al `identifier.system` canónico del Registro Civil más `type.coding` con `CSTipoIdentificador` code `01`.

## Payload válido

```json Patient con RUN válido theme={null}
{
  "resourceType": "Patient",
  "identifier": [
    {
      "use": "official",
      "type": {
        "coding": [
          {
            "system": "https://hl7chile.cl/fhir/ig/clcore/CodeSystem/CSTipoIdentificador",
            "code": "01",
            "display": "RUN"
          }
        ]
      },
      "system": "http://www.registrocivil.cl/run",
      "value": "15420097-3"
    }
  ],
  "name": [
    {
      "use": "official",
      "family": "Rojas",
      "given": ["María", "Elena"]
    }
  ],
  "gender": "female",
  "birthDate": "1982-06-14"
}
```

## Cómo validarlo

<CodeGroup>
  ```bash cURL theme={null}
  curl -X POST https://fhiron.cl/api/validate \
    -H "Content-Type: application/json" \
    -H "X-API-Key: $FHIRON_API_KEY" \
    -d @patient.json
  ```

  ```javascript Node.js theme={null}
  import { readFileSync } from "node:fs";

  const patient = JSON.parse(readFileSync("patient.json", "utf8"));

  const response = await fetch("https://fhiron.cl/api/validate", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "X-API-Key": process.env.FHIRON_API_KEY,
    },
    body: JSON.stringify(patient),
  });

  const outcome = await response.json();
  ```

  ```python Python theme={null}
  import json, os, requests

  with open("patient.json") as f:
      patient = json.load(f)

  response = requests.post(
      "https://fhiron.cl/api/validate",
      headers={
          "Content-Type": "application/json",
          "X-API-Key": os.environ["FHIRON_API_KEY"],
      },
      json=patient,
  )
  outcome = response.json()
  ```
</CodeGroup>

## OperationOutcome esperado

Si el DV está correcto, `issue[]` viene vacío y el HTTP status es `200`.

Si el DV no cuadra con el módulo 11 (por ejemplo `15420097-2`), Inspect devuelve:

```json theme={null}
{
  "resourceType": "OperationOutcome",
  "issue": [
    {
      "severity": "error",
      "code": "invalid",
      "details": {
        "text": "Dígito verificador inválido. El módulo 11 del Registro Civil exige DV 3 para el cuerpo 15420097."
      },
      "expression": ["Patient.identifier[0].value"],
      "diagnostics": "{\"code\":\"cl-run-03\",\"why\":\"El DV se calcula con módulo 11 sobre el cuerpo numérico. 11 → 0, 10 → K mayúscula.\"}"
    }
  ]
}
```

Errores típicos cuando el caso falla:

* `cl-run-03`: DV no cuadra con el módulo 11.
* `cl-run-04`: `identifier.system` distinto del canónico `http://www.registrocivil.cl/run`.
* `cl-patient-*`: falta `name`, `gender` o `birthDate` exigidos por `CorePacienteCl`.

## Tips

<Tip>
  El cuerpo del RUN se escribe **sin puntos de miles**: `15420097-3`, no `15.420.097-3`. Inspect normaliza puntos pero `identifier.value` debe quedar canónico en lectura/escritura.
</Tip>

<Tip>
  La `K` del DV va en **mayúscula**. `15420097-k` se rechaza con `cl-run-03`.
</Tip>

<Tip>
  `type.coding.code` siempre es `"01"` (el código de RUN en `CSTipoIdentificador`). `"RUN"` solo sirve como `display`. Para pasaporte usar `"02"`, para DNI extranjero `"03"`.
</Tip>

<Tip>
  Pacientes extranjeros sin RUN: usar `type.coding.code` `"02"` (pasaporte) o `"03"` (DNI extranjero) con `system` distinto y **omitir** la validación módulo 11. Inspect la salta si `type.coding.code != "01"`.
</Tip>

<Note>
  Inspect valida este `Patient` contra el perfil `CorePacienteCl` de CL Core 1.9.4. Si `identifier.system` no es canónico o el DV no cuadra, devuelve `cl-run-03` / `cl-run-04` con `quickFix` aplicable desde el MCP.
</Note>
