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: ...}
Summary
Functions
Validates a typed message.
Functions
@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:
- Message rules — MSH presence, required MSH fields
- 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.
- Field rules — required fields, max repetitions per segment
Each error map has:
:level—:erroror:warning:location— segment identifier (e.g.,"MSH","PID","message"):field— field name atom ornilfor 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—trueto check coded fields against HL7-defined tables. Defaults tofalse.: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 tonil(read MSH-12).