Skip to main content
Version: v0.1.13

src/arbitraries

fast-check generators for C-CDA, at @cosyte/ccda/arbitraries: spec-clean documents and the structured input that builds them, the clinical entries inside them, messy document text the parser accepts, and hostile input. We use them to test this library, and you can use them to test code you build on it.

fast-check is an optional peer dependency: install it to use this entry point. The main entry point, @cosyte/ccda, never loads it.

Every generated value is synthetic and random. Nothing in one looks like a patient's name, date of birth, address or identifier: names and identifiers start with Z, and codes are random numbers that name no real concept.

Example​

import fc from "fast-check";
import { parseCcda } from "@cosyte/ccda";
import { document } from "@cosyte/ccda/arbitraries";

fc.assert(
fc.property(document(), (doc) => parseCcda(doc.toString()).toString() === doc.toString()),
);

Interfaces​

CodeOptions​

Options for code.

Properties​

codeSystems?​

readonly optional codeSystems?: readonly string[]

The code system OIDs to name. The default leaves the system out, so the builder fills in the one its slot defaults to.


DocumentOptions​

Options for documentInit and document.

Properties​

documentTypes?​

readonly optional documentTypes?: readonly BuildableDocumentType[]

The document types to build. Defaults to all six the builder emits: "ccd", "referralNote", "dischargeSummary", "progressNote", "historyAndPhysical" and "consultationNote".

maxEntries?​

readonly optional maxEntries?: number

The most entries in each clinical list. Defaults to 2.


HostileInputOptions​

Options for hostileInput.

Properties​

maxLength?​

readonly optional maxLength?: number

The most characters of random text or markup. Defaults to 200.


QuantityOptions​

Options for quantity.

Properties​

units?​

readonly optional units?: readonly string[]

The units to draw from. Defaults to a list of valid, case-correct UCUM units.


TextOptions​

Options for text.

Properties​

maxLength?​

readonly optional maxLength?: number

The most characters. Defaults to 24.

minLength?​

readonly optional minLength?: number

The fewest characters. Defaults to 1.


TimestampOptions​

Options for timestamp.

Properties​

maxYear?​

readonly optional maxYear?: number

The latest year. Defaults to 2099.

minYear?​

readonly optional minYear?: number

The earliest year. Defaults to 1900.

Functions​

allergy()​

allergy(): Arbitrary<BuildCcdaAllergy>

Generate an allergy: an allergen with an optional type, reaction, severity, criticality and dates, or a "no known allergies" assertion.

Returns​

Arbitrary<BuildCcdaAllergy>

An arbitrary of a BuildCcdaAllergy.

Example​

import fc from "fast-check";
import { allergy } from "@cosyte/ccda/arbitraries";

const [a] = fc.sample(allergy(), 1);
// a is like { allergen: { code: "7980", displayName: "Zq" }, reaction: { ... } }

code()​

code(options?): Arbitrary<BuildCode>

Generate a coded value for the builder: a synthetic code (digits, a LOINC-style part number, or letters and digits), a displayName drawn from text, and sometimes a codeSystemName. The codes are random, so they name no real concept.

Parameters​

options?​

CodeOptions = {}

The code systems to name; see CodeOptions.

Returns​

Arbitrary<BuildCode>

An arbitrary of a BuildCode.

Example​

import fc from "fast-check";
import { code } from "@cosyte/ccda/arbitraries";

const [coded] = fc.sample(code(), 1);
// coded is like { code: "4417-2", displayName: "Zq&k" }

document()​

document(options?): Arbitrary<CcdaDocument>

Generate a spec-clean C-CDA R2.1 document: a CCD, a Referral Note, a Discharge Summary, a Progress Note, a History and Physical or a Consultation Note that buildCcda builds from documentInit, with its patient, problems, allergies, medications, results, vital signs, immunizations, procedures, encounters, social history, functional and mental status, past medical history, plan of treatment and family history. Every value in it is synthetic and random.

Its warnings describe what the input left out or asserted (a medication with no dose, a refused immunization, a smoking status code outside the value set), never a repair: the document serializes to text that parses back to the same document. A failing document prints as the buildCcda(...) call that rebuilds it.

Parameters​

options?​

DocumentOptions = {}

The document types and list sizes; see DocumentOptions.

Returns​

Arbitrary<CcdaDocument>

An arbitrary of a parsed CcdaDocument.

Example​

import fc from "fast-check";
import { parseCcda } from "@cosyte/ccda";
import { document } from "@cosyte/ccda/arbitraries";

fc.assert(
fc.property(document(), (doc) => parseCcda(doc.toString()).toString() === doc.toString()),
);

documentInit()​

documentInit(options?): Arbitrary<BuildCcdaInit>

Generate the structured input for buildCcda: a document type, a fixed document id and time (so the same input always builds the same bytes), a patient, and clinical lists of up to maxEntries items each, with the narrative sections and the encompassing encounter each type carries.

Parameters​

options?​

DocumentOptions = {}

The document types and list sizes; see DocumentOptions.

Returns​

Arbitrary<BuildCcdaInit>

An arbitrary of a BuildCcdaInit that buildCcda accepts.

Example​

import fc from "fast-check";
import { buildCcda } from "@cosyte/ccda";
import { documentInit } from "@cosyte/ccda/arbitraries";

const [init] = fc.sample(documentInit(), 1);
const doc = buildCcda(init!);

documentText()​

documentText(options?): Arbitrary<string>

Generate messy document text a real sender might produce, which the parser always accepts: a document from documentInit, written by the serializer, then changed by any of these: no XML declaration, a byte order mark, a stylesheet instruction, comments, indented header lines, CRLF line endings, single-quoted attributes, explicit end tags on empty elements, numeric character references and a carriage return in text, CDATA titles, the sdtc namespace declared, a vendor extension element in a namespace of its own, an unknown template, document templates with no version stamp, and sections recognized only by their LOINC code. The parser may warn about what it found, but it never refuses this text.

Parameters​

options?​

DocumentOptions = {}

The document types and list sizes; see DocumentOptions.

Returns​

Arbitrary<string>

An arbitrary of document text.

Example​

import fc from "fast-check";
import { parseCcda } from "@cosyte/ccda";
import { documentText } from "@cosyte/ccda/arbitraries";

fc.assert(fc.property(documentText(), (xml) => parseCcda(xml).documentType !== undefined));

encounter()​

encounter(): Arbitrary<BuildCcdaEncounter>

Generate an encounter: a coded encounter type with an optional status and period.

Returns​

Arbitrary<BuildCcdaEncounter>

An arbitrary of a BuildCcdaEncounter.

Example​

import fc from "fast-check";
import { encounter } from "@cosyte/ccda/arbitraries";

const [e] = fc.sample(encounter(), 1);
// e is like { type: { code: "99213", displayName: "Zq" }, period: { low: "20230615" } }

hostileInput()​

hostileInput(options?): Arbitrary<string>

Generate hostile input: random strings, random markup, document text cut short or with text inserted and deleted, a document type declaration with internal or external entities, an entity expansion bomb, nesting deeper than the parser's limit, and roots that are not an HL7 v3 ClinicalDocument. The parser may refuse any of it with one of its documented fatal errors, but it throws nothing else.

Parameters​

options?​

HostileInputOptions = {}

The length of random text; see HostileInputOptions.

Returns​

Arbitrary<string>

An arbitrary of a string.

Example​

import fc from "fast-check";
import { CcdaParseError, parseCcda } from "@cosyte/ccda";
import { hostileInput } from "@cosyte/ccda/arbitraries";

fc.assert(
fc.property(hostileInput(), (raw) => {
try {
parseCcda(raw);
} catch (err) {
if (!(err instanceof CcdaParseError)) throw err;
}
}),
);

immunization()​

immunization(): Arbitrary<BuildCcdaImmunization>

Generate an immunization: a coded vaccine with an optional dose, route and date, sometimes refused.

Returns​

Arbitrary<BuildCcdaImmunization>

An arbitrary of a BuildCcdaImmunization.

Example​

import fc from "fast-check";
import { immunization } from "@cosyte/ccda/arbitraries";

const [i] = fc.sample(immunization(), 1);
// i is like { vaccine: { code: "140", displayName: "Zq" }, effectiveTime: "20240101" }

medication()​

medication(): Arbitrary<BuildCcdaMedication>

Generate a medication: a coded drug, with an optional dose, route, frequency, therapy window and status.

Returns​

Arbitrary<BuildCcdaMedication>

An arbitrary of a BuildCcdaMedication.

Example​

import fc from "fast-check";
import { medication } from "@cosyte/ccda/arbitraries";

const [m] = fc.sample(medication(), 1);
// m is like { drug: { code: "314076", displayName: "Zq" }, dose: { value: 1, unit: "{tablet}" } }

patient()​

patient(): Arbitrary<BuildCcdaPatient>

Generate the record-target patient: a synthetic MRN, name parts that start with Z, an administrative gender code and a birth time, each sometimes left out (the builder then writes a nullFlavor).

Returns​

Arbitrary<BuildCcdaPatient>

An arbitrary of a BuildCcdaPatient.

Example​

import fc from "fast-check";
import { patient } from "@cosyte/ccda/arbitraries";

const [p] = fc.sample(patient(), 1);
// p is like { mrn: "ZK41", given: ["Zqa"], family: "Zmo", gender: "F" }

problem()​

problem(): Arbitrary<BuildCcdaProblem>

Generate a problem: a coded condition, a status, and an onset date; a resolved one may carry a resolution date.

Returns​

Arbitrary<BuildCcdaProblem>

An arbitrary of a BuildCcdaProblem.

Example​

import fc from "fast-check";
import { problem } from "@cosyte/ccda/arbitraries";

const [p] = fc.sample(problem(), 1);
// p is like { problem: { code: "4417", displayName: "Zq" }, status: "active" }

procedure()​

procedure(): Arbitrary<BuildCcdaProcedure>

Generate a procedure of each kind (an operative procedure, an act, or an observation with its value), performed or planned, with an optional date.

Returns​

Arbitrary<BuildCcdaProcedure>

An arbitrary of a BuildCcdaProcedure.

Example​

import fc from "fast-check";
import { procedure } from "@cosyte/ccda/arbitraries";

const [p] = fc.sample(procedure(), 1);
// p is like { code: { code: "80146002", displayName: "Zq" }, disposition: "performed" }

quantity()​

quantity(options?): Arbitrary<BuildQuantity>

Generate a quantity for the builder: a finite number (whole, decimal, negative, very small or very large) and a valid, case-correct UCUM unit.

Parameters​

options?​

QuantityOptions = {}

The units to draw from; see QuantityOptions.

Returns​

Arbitrary<BuildQuantity>

An arbitrary of a BuildQuantity.

Example​

import fc from "fast-check";
import { quantity } from "@cosyte/ccda/arbitraries";

const [q] = fc.sample(quantity(), 1);
// q is like { value: 98.6, unit: "[degF]" }

resultPanel()​

resultPanel(): Arbitrary<BuildCcdaResultPanel>

Generate a results panel: a coded panel with one to three results, each carrying exactly one value form (a quantity, a coded value or a string), and optional reference ranges, interpretations and times.

Returns​

Arbitrary<BuildCcdaResultPanel>

An arbitrary of a BuildCcdaResultPanel.

Example​

import fc from "fast-check";
import { resultPanel } from "@cosyte/ccda/arbitraries";

const [panel] = fc.sample(resultPanel(), 1);
// panel.results.length is 1, 2 or 3

text()​

text(options?): Arbitrary<string>

Generate free text a reader would see in a narrative or a displayName: letters, digits, the characters XML has to escape (<, >, &, ", '), the CDATA close ]]>, punctuation, tabs, line breaks and characters outside ASCII. It never starts or ends with whitespace and is never empty, so it reads back exactly as it was written.

Parameters​

options?​

TextOptions = {}

The length bounds; see TextOptions.

Returns​

Arbitrary<string>

An arbitrary of a string.

Example​

import fc from "fast-check";
import { text } from "@cosyte/ccda/arbitraries";

const [label] = fc.sample(text(), 1);
// label is like "q<&]]>\u00e9 7"

timestamp()​

timestamp(options?): Arbitrary<string>

Generate an HL7 v3 timestamp (TS) the parser reads as a date: YYYY, YYYYMM, YYYYMMDD, or a time of day to the hour, minute or second, the last with an optional fraction and an optional +HHMM or -HHMM offset. Every one is a real calendar date.

Parameters​

options?​

TimestampOptions = {}

The year bounds; see TimestampOptions.

Returns​

Arbitrary<string>

An arbitrary of a timestamp string.

Example​

import fc from "fast-check";
import { timestamp } from "@cosyte/ccda/arbitraries";

const [ts] = fc.sample(timestamp(), 1);
// ts is like "20240229" or "20210704153000.25-0500"

vitalsPanel()​

vitalsPanel(): Arbitrary<BuildCcdaVitalsPanel>

Generate a vital signs panel: one to three readings, each a coded vital with a quantity, and an optional interpretation and time.

Returns​

Arbitrary<BuildCcdaVitalsPanel>

An arbitrary of a BuildCcdaVitalsPanel.

Example​

import fc from "fast-check";
import { vitalsPanel } from "@cosyte/ccda/arbitraries";

const [panel] = fc.sample(vitalsPanel(), 1);
// panel.vitals.length is 1, 2 or 3