Skip to content

pyx12

Validation with pyx12. Needs pip install "oxedi[pyx12]". See Validating with pyx12 for the guide.

validate

validate(
    source: Source,
    mask: Optional[Mask] = None,
    spec: Optional[Spec] = None,
) -> list[Diagnostic]

Validates an 835 file with pyx12 and returns what it finds.

source is the file's bytes, a path, or a file object open in binary mode. pyx12 picks its map from the file's own version declaration. pyx12's own defaults apply: extended character set (charset="E"), no external code lists excluded (exclude_external_codes=None), and user/system configuration files (~/.pyx12.conf.xml, <prefix>/etc/pyx12.conf.xml) are not read, so findings do not depend on the machine.

Every error pyx12's engine records is returned: interchange, group and transaction errors, segment errors and element errors, including those of the envelope segments. They are read from the error tree pyx12 builds; pyx12's logging is not used. The pyx12 logger gets a logging.NullHandler the first time, when it has none, so pyx12's records are not printed by default and still reach the handlers the caller configures; levels, propagation and logging.disable stay as the caller set them.

Each finding is an oxedi.Diagnostic, the type parse returns, so both lists mix, sort by level and filter by origin. A pyx12 finding has kind == "External", origin == "pyx12" and code set to pyx12's own error code; rule is pyx12's message. level follows where pyx12 reports it: interchange, group and transaction findings are level 1, segment and element findings level 2. segment is the index of the segment at fault in the file's document (None when pyx12 names none), and document[d.segment].span gives its byte range. An interchange, group or transaction finding about a trailer (a count or a control number that does not match) lands on the trailer segment at the element holding that value; one about a duplicate or missing control structure lands on the header, at its control number when that is the offending value. datum is the value pyx12 reports, or else the bytes of the element in the file, or the segment id for a finding with no element. Findings come in segment order. path names the loops open at that segment, as in the diagnostics parse gives about the same segment.

A file pyx12 cannot read, or an exception inside pyx12, gives one level 1 finding with no code whose rule starts with could not finish validating, instead of raising: a file pyx12 rejects points at the segment holding the ISA with its bytes as the datum; a failure while reading names the segment pyx12 was processing, and one after the last segment names the last segment it completed. A file with no ISA to read the delimiters from raises oxedi.ParseError, as oxedi.parse does. Nothing is written to disk and no acknowledgement is generated.

With mask, an oxedi.Mask, the datum of a finding at a place a column flags as patient data reads is masked as parse masks its own diagnostics with that mask. In the finding's message, the original datum and each value its segment holds at such a place are replaced by their masked forms, whether or not the finding names an element. A value of 4 characters or more is replaced wherever it occurs. A shorter one is replaced only where it stands apart: the character on each side is the edge of the message, a character that is not an ASCII letter or digit (an accented letter or - counts), or part of another replaced value. Short values that overlap or touch are replaced as one group when the group stands apart. A short value glued to letters or digits that are not flagged values stays as written (with XAB and ABYZ flagged, QXABYZ keeps QX), and two short values that together spell a word of the message replace that word. Every other attribute, and the findings' number and order, stay as without it. The flagged places are those of the spec the parse uses: spec, an oxedi.Spec, or the built-in spec of the file's version when it is None, so a patched spec that flags more columns masks them here as parse(source, spec=spec, mask=mask) does. The same spec locates the findings' loop paths, as in parse(source, spec=spec). A failure finding for an exception inside pyx12, whose text no flagged place points at, keeps the exception's type and drops its text: could not finish validating: ValueError, whose message is not shown with a mask; ..., or its error tree could not be read (ValueError, whose message is not shown with a mask). The rest of such a finding is text oxedi wrote, so the mask does not rewrite it; its datum is masked as any other. The finding for a file pyx12 rejects quotes pyx12's X12Error, which can hold the whole ISA line, and its message is masked as pyx12's messages are; its datum is ISA instead of the ISA line, which names no element. pyx12 still reads the original file: the mask hides what validate returns, not what pyx12 is given. mask of any other type than oxedi.Mask or None raises TypeError, and so does spec of any other type than oxedi.Spec or None.