Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | 3x 2361x 16664x 3377x 13801x 5353x 13661x 12775x 16664x 515x 515x 515x 515x 515x 515x 31x 31x 31x 31x 1030x 31x 22x 22x 22x 22x 2x 22x 22x 22x 22x 22x 22x 22x 1x 22x 398x 22x 22x 398x 661x 2x 100x 78x 22x 78x 78x 156x 78x 78x 22x 9x 9x 9x 9x 9x 9x 9x 114x 225x 225x 105x 105x 9x 117x 117x | /** * The document rules of FHIR R4 (Bundle, `type = document`) that a builder * must keep and an import (#1173) checks before anything else: an * identifier and a timestamp, the Composition as the first entry, every * entry with a fullUrl, every reference inside the Bundle resolvable, and * every section holding text, entries or sub-sections (cmp-1) and an * emptyReason only when it has no entries (cmp-2). * * This is not profile validation; the HL7 validator does that * (scripts/eehrxf/validate.py, and `$validate` on import). It catches what a * document builder or a sender can get wrong in shape. Returns the problems, * empty when there are none. */ type Json = Record<string, unknown>; const obj = (v: unknown): Json => v && typeof v === "object" && !Array.isArray(v) ? (v as Json) : {}; /** Every `reference` string anywhere inside a value. */ function referencesIn(value: unknown, out: string[] = []): string[] { if (Array.isArray(value)) { value.forEach((v) => referencesIn(v, out)); } else if (value && typeof value === "object") { for (const [k, v] of Object.entries(value as Json)) { if (k === "reference" && typeof v === "string") out.push(v); else referencesIn(v, out); } } return out; } /** The keys an entry is found under: its fullUrl and `Type/id`. */ function keysOf(entry: Json): string[] { const r = obj(entry.resource); const keys: string[] = []; Eif (typeof entry.fullUrl === "string") keys.push(entry.fullUrl); Eif (typeof r.resourceType === "string" && typeof r.id === "string") { keys.push(`${r.resourceType}/${r.id}`); } return keys; } function entriesOf(bundle: unknown): Json[] { const b = obj(bundle); return Array.isArray(b.entry) ? b.entry.map(obj) : []; } function indexOf(entries: Json[]): Map<string, Json> { const byKey = new Map<string, Json>(); for (const e of entries) { for (const key of keysOf(e)) byKey.set(key, obj(e.resource)); } return byKey; } export function documentProblems(bundle: unknown): string[] { const b = obj(bundle); const problems: string[] = []; Iif (b.resourceType !== "Bundle") return ["not a FHIR Bundle"]; if (b.type !== "document") { problems.push(`Bundle.type is ${String(b.type)}, not document`); } const identifier = obj(b.identifier); Iif (!identifier.system || !identifier.value) { problems.push("Bundle.identifier needs a system and a value"); } Iif (typeof b.timestamp !== "string") { problems.push("Bundle.timestamp is missing"); } const entries = entriesOf(bundle); Iif (entries.length === 0) return [...problems, "the Bundle has no entries"]; const composition = obj(entries[0].resource); if (composition.resourceType !== "Composition") { problems.push("the first entry is not a Composition"); } entries.forEach((e, i) => { Iif (typeof e.fullUrl !== "string") { problems.push(`entry[${i}] has no fullUrl`); } }); const byKey = indexOf(entries); entries.forEach((e, i) => { for (const ref of referencesIn(e.resource)) { if (!byKey.has(ref)) { problems.push( `entry[${i}] refers to ${ref}, which is not in the Bundle`, ); } } }); const sections = (s: unknown): Json[] => (Array.isArray(s) ? s.map(obj) : []).flatMap((x) => [ x, ...sections(x.section), ]); for (const s of sections(composition.section)) { const title = typeof s.title === "string" ? s.title : "untitled"; const has = (k: string) => Array.isArray(s[k]) ? (s[k] as unknown[]).length > 0 : !!s[k]; Iif (!has("text") && !has("entry") && !has("section")) { problems.push(`section "${title}" has no text, entries or sections`); } Iif (has("entry") && s.emptyReason) { problems.push(`section "${title}" has entries and an emptyReason`); } } return problems; } /** * Entries no reference leads to from the Composition, a Provenance aside * (it points at the others). FHIR only warns about these; the hub's own * documents have none, and its tests hold it to that. */ export function unreachableEntries(bundle: unknown): string[] { const entries = entriesOf(bundle); Iif (entries.length === 0) return []; const byKey = indexOf(entries); const composition = obj(entries[0].resource); const reached = new Set<Json>([composition]); const queue: Json[] = [composition]; while (queue.length) { for (const ref of referencesIn(queue.shift())) { const target = byKey.get(ref); if (target && !reached.has(target)) { reached.add(target); queue.push(target); } } } return entries .map((e, i) => ({ r: obj(e.resource), i })) .filter(({ r }) => !reached.has(r) && r.resourceType !== "Provenance") .map(({ r, i }) => `entry[${i}] (${String(r.resourceType)})`); } |