validate_hl7_message
Validate HL7 v2.x messages for standards compliance by checking required fields, data types, segment structure, and cross-segment consistency.
Instructions
[Premium] Validate an HL7 v2.x message against the standard. Checks required fields, data types, table values, segment structure, and cross-segment consistency.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| message | Yes | Raw HL7 v2.x message string to validate. |
Implementation Reference
- The validate_hl7_message function validates an HL7 v2.x message, performing checks on the MSH header, segment order, individual segments, and cross-segment relationships.
def validate_hl7_message(message: str) -> str: """Validate an HL7 v2.x message against the standard.""" premium_check = require_premium("validate_hl7_message") if premium_check: return premium_check if not message or not message.strip(): return "Error: Empty message provided." segments = _normalize_message(message) if not segments: return "FATAL: No segments found in message." errors: list[str] = [] warnings: list[str] = [] info: list[str] = [] # 1. Validate MSH header result = _validate_msh(segments[0], errors, warnings) if result is None: return _format_validation_result(errors, warnings, info) field_sep, component_sep, msg_type_str = result # 2. Validate segment structure segment_ids = [s.split(field_sep)[0] if field_sep in s else s[:3] for s in segments] _validate_segment_order(msg_type_str, segment_ids, errors, info) # 3. Validate individual segments _validate_segments(segments, field_sep, component_sep, errors, warnings, info) # 4. Cross-segment validation _validate_cross_segments(segments, segment_ids, field_sep, component_sep, errors, warnings, info) return _format_validation_result(errors, warnings, info)