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

# Reglas locales cl-*

> 35+ reglas offline cubriendo los 13 recursos más usados de CL Core v1.9.4.

Las reglas locales corren offline en el MCP connector y en Inspect. No consumen cuota. La librería se distribuye vendorizada dentro del conector, sin dependencias adicionales en runtime.

## Formato y estructura

| Código       | Severidad | Descripción                                                                 |
| ------------ | --------- | --------------------------------------------------------------------------- |
| `cl-json-01` | error     | JSON inválido (string de entrada no parseable).                             |
| `cl-json-02` | error     | El argumento no es un objeto FHIR®.                                         |
| `cl-json-03` | error     | Falta `resourceType`.                                                       |
| `cl-json-04` | warning   | `resourceType` no es un tipo oficial de FHIR R4 (el servidor lo rechazará). |

## Encounter (CoreEncounterCl)

| Código      | Severidad | Descripción                                                                                                  |
| ----------- | --------- | ------------------------------------------------------------------------------------------------------------ |
| `cl-enc-01` | error     | Falta `status` (cardinalidad 1..1).                                                                          |
| `cl-enc-02` | error     | Falta `class`.                                                                                               |
| `cl-enc-03` | error     | Falta `subject`.                                                                                             |
| `cl-enc-04` | error     | `status` fuera del ValueSet `encounter-status` (binding required). Ej: `complete` que devuelven HIS legacy.  |
| `cl-enc-05` | warning   | `class.system` no canónico: usar `http://terminology.hl7.org/CodeSystem/v3-ActCode` (AMB · IMP · EMER · HH). |

## MedicationRequest

| Código         | Severidad | Descripción                                                                                     |
| -------------- | --------- | ----------------------------------------------------------------------------------------------- |
| `cl-medreq-01` | error     | Falta `status`.                                                                                 |
| `cl-medreq-02` | error     | Falta `intent` (1..1). Sin él no se distingue prescripción firmada de propuesta IA o protocolo. |
| `cl-medreq-03` | error     | Falta `subject`.                                                                                |
| `cl-medreq-04` | error     | Falta `medication[x]` (`medicationReference` o `medicationCodeableConcept`).                    |

## Terminologías y bindings transversales

| Código            | Severidad | Descripción                                                                                               |
| ----------------- | --------- | --------------------------------------------------------------------------------------------------------- |
| `cl-system-02`    | error     | `identifier.system` no es URI absoluta: usar la URI oficial del NamingSystem (Registro Civil, ISP, etc.). |
| `cl-meta-01`      | error     | `meta.profile` no es URL canónica HTTPS: usar el canonical del StructureDefinition completo.              |
| `cl-ref-01`       | error     | `Reference.reference` no respeta el formato `ResourceType/id`.                                            |
| `cl-date-01`      | error     | Fecha/hora fuera de ISO-8601 (`2024/01/15` en vez de `2024-01-15`).                                       |
| `cl-narrative-01` | error     | `text.status` fuera de `NarrativeStatus`.                                                                 |
| `cl-comuna-01`    | warning   | `Patient.address.city` no aparece en el decreto 817: corregir grafía o reemplazar por comuna oficial.     |

## Identificador nacional (dentro de `CorePacienteCl`)

| Código      | Severidad | Descripción                                                                                                        |
| ----------- | --------- | ------------------------------------------------------------------------------------------------------------------ |
| `cl-run-01` | error     | RUN con puntos: incluye **quickFix** que los elimina automáticamente.                                              |
| `cl-run-02` | error     | Identifier con system RUN pero sin `value`.                                                                        |
| `cl-run-03` | error     | Dígito verificador incorrecto (módulo 11 del Registro Civil).                                                      |
| `cl-run-04` | warning   | `identifier.system` legacy: usar el canonical `http://www.registrocivil.cl/run` + `CSTipoIdentificador` code `01`. |

## Patient (CorePacienteCl)

| Código          | Severidad | Descripción                                 |
| --------------- | --------- | ------------------------------------------- |
| `cl-patient-01` | error     | Falta `identifier`.                         |
| `cl-patient-02` | error     | Falta `name`.                               |
| `cl-patient-03` | error     | Elemento de `name` sin `family` ni `given`. |
| `cl-patient-04` | warning   | `gender` recomendado.                       |

## Practitioner (CorePrestadorCl)

| Código               | Severidad | Descripción         |
| -------------------- | --------- | ------------------- |
| `cl-practitioner-01` | error     | Falta `identifier`. |
| `cl-practitioner-02` | error     | Falta `name`.       |

## Observation

| Código      | Severidad | Descripción      |
| ----------- | --------- | ---------------- |
| `cl-obs-01` | error     | Falta `status`.  |
| `cl-obs-02` | error     | Falta `code`.    |
| `cl-obs-03` | error     | Falta `subject`. |

## Medication (CoreMedicamentoCl)

| Código      | Severidad | Descripción                                                             |
| ----------- | --------- | ----------------------------------------------------------------------- |
| `cl-med-01` | error     | Falta `code`.                                                           |
| `cl-med-02` | warning   | Sistema de codificación no reconocido (TFC/SNOMED/RxNorm recomendados). |

## Otros recursos cubiertos

`Condition` · `AllergyIntolerance` · `Procedure` · `Coverage` · `Organization` · `Immunization` · `DiagnosticReport` · `Bundle`

Códigos: `cl-cond-*`, `cl-allergy-*`, `cl-proc-*`, `cl-cov-*`, `cl-org-*`, `cl-imm-*`, `cl-dr-*`, `cl-bundle-*`. Listado completo accesible vía el recurso MCP `fhiron://errors` o la tool `fhiron_explain_code`.

## Reglas locales vs validación completa

Las reglas locales son un subconjunto de las restricciones del IG. Cubren los errores estructurales más frecuentes (campos required ausentes, value-sets fuera de binding, fechas mal formateadas, references rotas) y producen feedback inmediato sin consumo de cuota.

La validación completa contra el IG cargado en el servidor cubre el resto: invariantes FHIRPath, bindings a ValueSets, terminologías, slicing y extensions. Cuesta 1 crédito y se invoca con `fhiron_validate` (o `fhiron_validate_file` cuando el recurso vive en un `.json` local).

```mermaid theme={null}
flowchart TD
    A([Recurso FHIR]) --> B{Reglas locales<br/>cl-*}
    B -->|Pass| D{Validación completa<br/>contra IG}
    B -->|Issues| C([Issues + quickFix])
    D -->|Pass| E([Válido])
    D -->|Issues| C

    classDef start fill:#1e1f24,stroke:#635BFF,stroke-width:2px,color:#fff
    classDef check fill:#1e1f24,stroke:#7C7BFF,color:#fff
    classDef ok fill:#0a3a2a,stroke:#10b981,color:#fff
    classDef fail fill:#3a0f1a,stroke:#ef4444,color:#fff

    class A start
    class B,D check
    class C fail
    class E ok
```

| Etapa               | Costo            | Latencia típica |
| ------------------- | ---------------- | --------------- |
| Reglas locales      | Gratis · offline | \< 5 ms         |
| Validación completa | 1 crédito        | \< 3 s (p95)    |

<Note>
  Patrón típico: `fhiron_lint` en cada cambio del editor; `fhiron_validate` antes de comprometer el recurso al destino. Si el lint devuelve un `quickFix`, llamar `fhiron_apply_fix` y volver a lintar antes de gastar crédito.
</Note>
