@dcmjs-org/schemas 0.0.0-stage → 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +120 -2
- package/package.json +26 -3
- package/schema/naturalized.schema.json +52282 -0
- package/src/schema/naturalizedRules.js +153 -0
- package/types/dcmjs-schema.d.ts +10374 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// The normative rule surface for the naturalized representation.
|
|
2
|
+
//
|
|
3
|
+
// The per-attribute rules are not a committed table: they derive on demand
|
|
4
|
+
// from the packed dictionary that dcmjs already ships, through the same
|
|
5
|
+
// isMultiVM rule the runtime uses, so the schema and the runtime cannot
|
|
6
|
+
// drift apart. Only the hand-decided data lives here: the envelope tokens,
|
|
7
|
+
// the Part 5 value formats (documentation, not validation), the multi-VR
|
|
8
|
+
// resolutions, and the exclusion lists. The generated JSON Schema and the
|
|
9
|
+
// .d.ts stay static files (ajv and tsc need static input); the generator
|
|
10
|
+
// in ../../generate derives them from this module.
|
|
11
|
+
import { getAllStandardTagEntries } from "../../../core/src/dicom.lookup.js";
|
|
12
|
+
import isMultiVM from "../../../core/src/utilities/isMultiVM.js";
|
|
13
|
+
import BINARY_VRS from "../../../core/src/utilities/binaryVRs.js";
|
|
14
|
+
|
|
15
|
+
export const SCHEMA_VERSION = "1.0.0";
|
|
16
|
+
|
|
17
|
+
export { BINARY_VRS };
|
|
18
|
+
|
|
19
|
+
// Every VR code an attribute rule may carry after MULTI_VR resolution.
|
|
20
|
+
export const KNOWN_VRS = new Set(
|
|
21
|
+
(
|
|
22
|
+
"AE AS AT CS DA DT LO LT SH ST TM UC UI UR UT " +
|
|
23
|
+
"DS FL FD IS SL SS UL US UV SV " +
|
|
24
|
+
"OB OD OF OL OV OW UN PN SQ"
|
|
25
|
+
).split(" ")
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
// Ambiguous dictionary VR codes -> explicit member unions (exhaustive for
|
|
29
|
+
// the packed dictionary; buildAttributeRules fails loudly on anything else).
|
|
30
|
+
export const MULTI_VR = {
|
|
31
|
+
xs: ["US", "SS"],
|
|
32
|
+
ox: ["OB", "OW"],
|
|
33
|
+
up: ["UL"],
|
|
34
|
+
lt: ["OW", "US", "SS"]
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Part 5 per-VR value-format constraints. Documentation only: the schema
|
|
38
|
+
// describes what dcmjs returns, and dcmjs passes values through as read, so
|
|
39
|
+
// these formats are NOT validation rules (envelope.values: "asReturned").
|
|
40
|
+
// DS and IS are absent because they naturalize as numbers, where a string
|
|
41
|
+
// format cannot apply.
|
|
42
|
+
export const vrFormats = {
|
|
43
|
+
AE: { maxLength: 16 },
|
|
44
|
+
AS: { pattern: "^\\d{3}[DWMY]$" },
|
|
45
|
+
CS: { maxLength: 16, pattern: "^[A-Z0-9 _]*$" },
|
|
46
|
+
DA: { pattern: "^\\d{8}$" },
|
|
47
|
+
DT: {
|
|
48
|
+
maxLength: 26,
|
|
49
|
+
pattern:
|
|
50
|
+
"^\\d{4}(\\d{2}(\\d{2}(\\d{2}(\\d{2}(\\d{2}(\\.\\d{1,6})?)?)?)?)?)?([+-]\\d{4})?$"
|
|
51
|
+
},
|
|
52
|
+
LO: { maxLength: 64 },
|
|
53
|
+
LT: { maxLength: 10240 },
|
|
54
|
+
SH: { maxLength: 16 },
|
|
55
|
+
ST: { maxLength: 1024 },
|
|
56
|
+
TM: { pattern: "^\\d{2}(\\d{2}(\\d{2}(\\.\\d{1,6})?)?)?$" },
|
|
57
|
+
UI: { maxLength: 64, pattern: "^[0-9.]+$" }
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// Group FFFE items are stream structure, not data elements (VR "na").
|
|
61
|
+
export const EXCLUDED_TAGS = new Set(["FFFEE000", "FFFEE00D", "FFFEE0DD"]);
|
|
62
|
+
|
|
63
|
+
// Retired tags the dictionary carries without a keyword (it gives all of
|
|
64
|
+
// them the placeholder "RETIRED_undefined", which cannot be a property
|
|
65
|
+
// name shared by unrelated tags). buildAttributeRules throws on any other
|
|
66
|
+
// tag whose keyword is missing or duplicated.
|
|
67
|
+
export const KEYWORDLESS_TAGS = new Set(["00180061", "04000315", "300A0782"]);
|
|
68
|
+
|
|
69
|
+
export const envelope = {
|
|
70
|
+
cardinality: "vm-based", // VM 1/0-1 -> scalar, any other VM -> list
|
|
71
|
+
values: "asReturned", // the schema describes what dcmjs returns; vrFormats documents Part 5 separately
|
|
72
|
+
personName: "componentObject", // {Alphabetic,Ideographic,Phonetic}
|
|
73
|
+
sequences: "datasetArrayOrItem", // SQ -> dataset[] | the single item
|
|
74
|
+
privateTags: "creatorGrouped", // '<slot>:<creator>' keys
|
|
75
|
+
unknownTags: "preserved", // { vr, Value, rawValue }
|
|
76
|
+
rawRetention: "inexactOnly", // raw kept when round-trip inexact
|
|
77
|
+
bulk: "referenceOrBinary" // {BulkDataURI} | {InlineBinary} | frame list
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
function bareTag(paren) {
|
|
81
|
+
// "(0008,0008)" -> "00080008"
|
|
82
|
+
const m = paren.match(/^\((\w{4}),(\w{4})\)$/);
|
|
83
|
+
if (!m) {
|
|
84
|
+
throw new Error(`unexpected tag format: ${paren}`);
|
|
85
|
+
}
|
|
86
|
+
return (m[1] + m[2]).toUpperCase();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
let cachedRules = null;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The per-attribute rule table, derived from the packed dictionary:
|
|
93
|
+
* tag -> { keyword, vr, vm, multi }. vr is a string, or an array when the
|
|
94
|
+
* dictionary VR code is a multi-VR union. multi follows the runtime
|
|
95
|
+
* isMultiVM rule. Built once, cached.
|
|
96
|
+
*/
|
|
97
|
+
export function buildAttributeRules() {
|
|
98
|
+
if (cachedRules) {
|
|
99
|
+
return cachedRules;
|
|
100
|
+
}
|
|
101
|
+
const attributes = {};
|
|
102
|
+
const tagByKeyword = new Map();
|
|
103
|
+
for (const e of getAllStandardTagEntries()) {
|
|
104
|
+
const tag = bareTag(e.tag);
|
|
105
|
+
if (EXCLUDED_TAGS.has(tag) || KEYWORDLESS_TAGS.has(tag)) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (!e.name || e.name === "RETIRED_undefined") {
|
|
109
|
+
throw new Error(
|
|
110
|
+
`${tag}: missing keyword — add the tag to KEYWORDLESS_TAGS or fix the dictionary`
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
const holder = tagByKeyword.get(e.name);
|
|
114
|
+
if (holder) {
|
|
115
|
+
throw new Error(
|
|
116
|
+
`${tag}: keyword "${e.name}" already used by ${holder} — keywords must be unique property names`
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
tagByKeyword.set(e.name, tag);
|
|
120
|
+
let vr;
|
|
121
|
+
if (KNOWN_VRS.has(e.vr)) {
|
|
122
|
+
vr = e.vr;
|
|
123
|
+
} else if (MULTI_VR[e.vr]) {
|
|
124
|
+
vr = MULTI_VR[e.vr];
|
|
125
|
+
} else {
|
|
126
|
+
throw new Error(`${tag} ${e.name}: unmapped VR code "${e.vr}"`);
|
|
127
|
+
}
|
|
128
|
+
attributes[tag] = {
|
|
129
|
+
keyword: e.name,
|
|
130
|
+
vr,
|
|
131
|
+
vm: e.vm,
|
|
132
|
+
multi: isMultiVM(e.vm)
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
cachedRules = Object.fromEntries(
|
|
136
|
+
Object.entries(attributes).sort(([a], [b]) => a.localeCompare(b))
|
|
137
|
+
);
|
|
138
|
+
return cachedRules;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The rule for one bare tag ("00100020"), or undefined. */
|
|
142
|
+
export function getAttributeRule(tag) {
|
|
143
|
+
return buildAttributeRules()[tag];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export const naturalizedRules = {
|
|
147
|
+
version: SCHEMA_VERSION,
|
|
148
|
+
envelope,
|
|
149
|
+
vrFormats,
|
|
150
|
+
get attributes() {
|
|
151
|
+
return buildAttributeRules();
|
|
152
|
+
}
|
|
153
|
+
};
|