@dcmjs-org/schemas 0.0.0-bootstrap.0 → 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 CHANGED
@@ -1 +1,121 @@
1
1
  # @dcmjs-org/schemas
2
+
3
+ The rules of the naturalized representation — the plain-JavaScript view of
4
+ a DICOM dataset that `DicomMetaDictionary.naturalizeDataset` returns —
5
+ published three ways: as a runtime rules module, as a JSON Schema, and as
6
+ TypeScript declarations.
7
+
8
+ ## What this package is for
9
+
10
+ dcmjs turns a parsed dataset into natural JavaScript: `dataset.PatientID`
11
+ instead of raw tag numbers. The *shape* of that view follows rules — which
12
+ keyword a tag maps to, whether a value is a scalar or a list, what a
13
+ sequence or a person name or pixel data looks like, and when a value is
14
+ null. Those rules used to live implicitly in parser code. This package
15
+ states them explicitly, for three consumers:
16
+
17
+ - **validators** (any JSON Schema tool) use `schema/naturalized.schema.json`
18
+ to check that an object has the naturalized shape;
19
+ - **editors and TypeScript builds** use `types/dcmjs-schema.d.ts` for
20
+ the `NaturalizedDataset` property types;
21
+ - **runtime code** (a validator UI, an anonymizer checking its tag list, a
22
+ form generator) imports the rules module to look up one attribute's rule.
23
+
24
+ ## How this differs from the dictionary dcmjs already has
25
+
26
+ It is not a copy of the dictionary. The per-attribute rules **derive on
27
+ demand** from the packed dictionary that `@dcmjs-org/core` already ships —
28
+ through the same `isMultiVM` rule the runtime uses, so the schema and the
29
+ runtime cannot drift apart. What this package adds is the *semantics* on
30
+ top of the raw `{tag, vr, vm}` table:
31
+
32
+ - the naturalized cardinality (`multi`: scalar vs list) per attribute,
33
+ - the resolution of ambiguous dictionary VR codes (`xs`, `ox`, `up`, `lt`)
34
+ into explicit VR unions,
35
+ - the contract decisions frozen in the `envelope` block (see below),
36
+ - the Part 5 value formats (`vrFormats`) as reference documentation,
37
+ - and the two static projections (JSON Schema, `.d.ts`) that ajv and tsc
38
+ need as files.
39
+
40
+ The package lives in this repository because the rules must change in the
41
+ same commit as the runtime behavior they describe — CI regenerates the
42
+ projections and fails when they drift. A consumer does not regenerate
43
+ anything: the two artifacts are committed, and the rules module computes
44
+ from the dictionary at require-time (about 5k rules, built once, cached).
45
+
46
+ ## The value contract (`envelope`)
47
+
48
+ `envelope.values: "asReturned"` — the schema describes **what dcmjs
49
+ returns**, not what Part 5 permits. Concretely: a present-but-empty
50
+ element is `null` (every property type carries null), list entries can be
51
+ null, an empty date string or a malformed age passes through unchanged,
52
+ and `vrFormats` documents the Part 5 formats without enforcing them. A
53
+ separate Part 5 conformance check can use `vrFormats` to report them.
54
+
55
+ Other tokens: sequences are a dataset array or the single item itself
56
+ (`datasetArrayOrItem` — a one-item sequence naturalizes as the item, with
57
+ a hidden `[0]`, `length`, and iterator); binary values are a frame list or
58
+ one bulk reference (`{BulkDataURI}` / `{InlineBinary}`); person names are
59
+ component objects (`{Alphabetic, Ideographic, Phonetic}`). UV and SV
60
+ values are `bigint` — note `JSON.stringify` throws on bigint.
61
+
62
+ ## Which artifact for which task
63
+
64
+ | Task | Artifact |
65
+ | --- | --- |
66
+ | Validate a naturalized dataset | `schema/naturalized.schema.json` with ajv |
67
+ | Type a dataset in TypeScript | `types/dcmjs-schema.d.ts` (`NaturalizedDataset`) |
68
+ | Look up one attribute's rule at runtime | `getAttributeRule(tag)` from the module |
69
+ | Check a keyword list / walk a dataset | `buildAttributeRules()` from the module |
70
+ | Cite a contract decision | `envelope` from the module |
71
+ | Report Part 5 format violations | `vrFormats` from the module |
72
+
73
+ ## Usage
74
+
75
+ Validate a naturalized dataset (what `test/schema/naturalizedValidation.test.js`
76
+ does):
77
+
78
+ ```js
79
+ import { readFileSync } from "fs";
80
+ import Ajv2020 from "ajv/dist/2020";
81
+ import dcmjs from "dcmjs";
82
+
83
+ const schema = JSON.parse(
84
+ readFileSync("packages/schemas/schema/naturalized.schema.json", "utf8")
85
+ );
86
+ // strictSchema off: the schema carries x-dicom-* annotation keywords
87
+ const validate = new Ajv2020({ strictSchema: false }).compile(schema);
88
+
89
+ const { DicomMessage, DicomMetaDictionary } = dcmjs.data;
90
+ const dataset = DicomMetaDictionary.naturalizeDataset(
91
+ DicomMessage.readFile(arrayBuffer).dict
92
+ );
93
+ if (!validate(dataset)) {
94
+ console.log(validate.errors);
95
+ }
96
+ ```
97
+
98
+ Look up rules at runtime:
99
+
100
+ ```js
101
+ import {
102
+ getAttributeRule,
103
+ envelope
104
+ } from "@dcmjs-org/schemas";
105
+
106
+ getAttributeRule("00080008");
107
+ // { keyword: "ImageType", vr: "CS", vm: "2-n", multi: true }
108
+ envelope.values; // "asReturned"
109
+ ```
110
+
111
+ ## Regeneration
112
+
113
+ `generate/` holds the generator; run `pnpm run generate-schema` from the
114
+ repository root to rebuild the two committed artifacts
115
+ (`schema/naturalized.schema.json`, `types/dcmjs-schema.d.ts`).
116
+ Regeneration is deterministic — CI fails when the committed artifacts do
117
+ not match the generator output.
118
+
119
+ The package is private until its package-merge review flips it; the
120
+ IOD catalog half of the schema work lands later with the validation
121
+ package that consumes it.
package/package.json CHANGED
@@ -1,11 +1,29 @@
1
1
  {
2
2
  "name": "@dcmjs-org/schemas",
3
- "version": "0.0.0-bootstrap.0",
4
- "description": "Placeholder that reserves the name. CI publishes the real releases.",
3
+ "version": "1.0.0-beta.1",
4
+ "description": "Machine-readable catalog of DICOM attributes: names, types, and rules, generated from the data dictionary",
5
+ "type": "module",
6
+ "main": "./src/schema/naturalizedRules.js",
7
+ "types": "./types/dcmjs-schema.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./types/dcmjs-schema.d.ts",
11
+ "import": "./src/schema/naturalizedRules.js",
12
+ "default": "./src/schema/naturalizedRules.js"
13
+ },
14
+ "./schema/naturalized.schema.json": "./schema/naturalized.schema.json",
15
+ "./package.json": "./package.json"
16
+ },
17
+ "files": [
18
+ "src/schema",
19
+ "schema",
20
+ "types/dcmjs-schema.d.ts",
21
+ "README.md"
22
+ ],
5
23
  "license": "MIT",
6
24
  "repository": {
7
25
  "type": "git",
8
26
  "url": "git+https://github.com/dcmjs-org/dcmjs.git",
9
27
  "directory": "packages/schemas"
10
28
  }
11
- }
29
+ }