Skip to main content
Version: v0.0.2

Function: unexpectedSegment()

unexpectedSegment(position, segmentId, context): X12ParseWarning

Build an X12_UNEXPECTED_SEGMENT warning. Emitted when a structurally meaningful envelope segment (GE, SE, body segments) appears outside its expected parent - e.g. a GE with no open GS, an SE with no open ST, or any body segment before the first ST. The parser preserves lenient-never-throw and continues; the warning carries the segment id (if it matches the X12 segment-id grammar - otherwise the literal (non-spec) is substituted to avoid echoing arbitrary bytes / PHI) and its global segment index so a consumer can locate the deviation.

Message NEVER echoes element values - only a SHAPE-VALIDATED 2-or-3- letter segment id (the spec name, not user data) and the structural context, mirroring the H-PHI invariant locked for @cosyte/hl7. A hostile input that puts PHI in the first slot of a malformed "segment" still has its bytes preserved on the parent container - they are simply not echoed in the diagnostic message.

Parameters

position

X12Position

segmentId

string

context

string

Returns

X12ParseWarning

Example

import { unexpectedSegment } from "@cosyte/x12";
const w = unexpectedSegment(
{ segmentIndex: 12, interchangeIndex: 0 },
"GE",
"no open functional group",
);