@driftsys/markspec 0.2.0 → 0.3.0

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.
Files changed (202) hide show
  1. package/README.md +32 -8
  2. package/esm/compiler/inverses.d.ts +22 -0
  3. package/esm/compiler/inverses.d.ts.map +1 -0
  4. package/esm/compiler/inverses.js +147 -0
  5. package/esm/compiler/link_target.d.ts +19 -0
  6. package/esm/compiler/link_target.d.ts.map +1 -0
  7. package/esm/compiler/link_target.js +66 -0
  8. package/esm/compiler/mod.d.ts +7 -1
  9. package/esm/compiler/mod.d.ts.map +1 -1
  10. package/esm/compiler/mod.js +61 -14
  11. package/esm/compiler/schema.d.ts.map +1 -1
  12. package/esm/compiler/schema.js +20 -1
  13. package/esm/config/markspec.d.ts +51 -0
  14. package/esm/config/markspec.d.ts.map +1 -0
  15. package/esm/config/markspec.js +216 -0
  16. package/esm/config/mod.d.ts +4 -0
  17. package/esm/config/mod.d.ts.map +1 -1
  18. package/esm/config/mod.js +27 -16
  19. package/esm/formatter/mod.d.ts +12 -2
  20. package/esm/formatter/mod.d.ts.map +1 -1
  21. package/esm/formatter/mod.js +130 -22
  22. package/esm/mod.d.ts +10 -6
  23. package/esm/mod.d.ts.map +1 -1
  24. package/esm/mod.js +5 -2
  25. package/esm/model/attributes.d.ts +47 -0
  26. package/esm/model/attributes.d.ts.map +1 -0
  27. package/esm/model/attributes.js +97 -0
  28. package/esm/model/mod.d.ts +255 -26
  29. package/esm/model/mod.d.ts.map +1 -1
  30. package/esm/model/mod.js +27 -1
  31. package/esm/model/profile.d.ts +189 -0
  32. package/esm/model/profile.d.ts.map +1 -0
  33. package/esm/model/profile.js +31 -0
  34. package/esm/parser/attributes.d.ts +13 -1
  35. package/esm/parser/attributes.d.ts.map +1 -1
  36. package/esm/parser/attributes.js +47 -0
  37. package/esm/parser/frontmatter.d.ts +53 -0
  38. package/esm/parser/frontmatter.d.ts.map +1 -0
  39. package/esm/parser/frontmatter.js +185 -0
  40. package/esm/parser/grammars.js +11 -2
  41. package/esm/parser/markdown.d.ts +11 -4
  42. package/esm/parser/markdown.d.ts.map +1 -1
  43. package/esm/parser/markdown.js +85 -38
  44. package/esm/parser/mod.d.ts +26 -10
  45. package/esm/parser/mod.d.ts.map +1 -1
  46. package/esm/parser/mod.js +39 -9
  47. package/esm/parser/source.d.ts +12 -8
  48. package/esm/parser/source.d.ts.map +1 -1
  49. package/esm/parser/source.js +20 -13
  50. package/esm/profile/cache.d.ts +29 -0
  51. package/esm/profile/cache.d.ts.map +1 -0
  52. package/esm/profile/cache.js +45 -0
  53. package/esm/profile/chain.d.ts +42 -0
  54. package/esm/profile/chain.d.ts.map +1 -0
  55. package/esm/profile/chain.js +193 -0
  56. package/esm/profile/git-cache.d.ts +73 -0
  57. package/esm/profile/git-cache.d.ts.map +1 -0
  58. package/esm/profile/git-cache.js +96 -0
  59. package/esm/profile/load.d.ts +28 -0
  60. package/esm/profile/load.d.ts.map +1 -0
  61. package/esm/profile/load.js +50 -0
  62. package/esm/profile/manifest.d.ts +21 -0
  63. package/esm/profile/manifest.d.ts.map +1 -0
  64. package/esm/profile/manifest.js +830 -0
  65. package/esm/profile/merge.d.ts +26 -0
  66. package/esm/profile/merge.d.ts.map +1 -0
  67. package/esm/profile/merge.js +518 -0
  68. package/esm/profile/mod.d.ts +23 -0
  69. package/esm/profile/mod.d.ts.map +1 -0
  70. package/esm/profile/mod.js +14 -0
  71. package/esm/profile/npm.d.ts +51 -0
  72. package/esm/profile/npm.d.ts.map +1 -0
  73. package/esm/profile/npm.js +186 -0
  74. package/esm/profile/resolver.d.ts +60 -0
  75. package/esm/profile/resolver.d.ts.map +1 -0
  76. package/esm/profile/resolver.js +132 -0
  77. package/esm/reporter/mod.js +14 -27
  78. package/esm/validator/attributes.d.ts +43 -0
  79. package/esm/validator/attributes.d.ts.map +1 -0
  80. package/esm/validator/attributes.js +169 -0
  81. package/esm/validator/mod.d.ts +27 -6
  82. package/esm/validator/mod.d.ts.map +1 -1
  83. package/esm/validator/mod.js +132 -124
  84. package/esm/validator/normalize.d.ts +21 -0
  85. package/esm/validator/normalize.d.ts.map +1 -0
  86. package/esm/validator/normalize.js +58 -0
  87. package/esm/validator/pattern.d.ts +23 -0
  88. package/esm/validator/pattern.d.ts.map +1 -0
  89. package/esm/validator/pattern.js +44 -0
  90. package/esm/validator/pipeline.d.ts +30 -0
  91. package/esm/validator/pipeline.d.ts.map +1 -0
  92. package/esm/validator/pipeline.js +115 -0
  93. package/esm/validator/traceability.d.ts +48 -0
  94. package/esm/validator/traceability.d.ts.map +1 -0
  95. package/esm/validator/traceability.js +141 -0
  96. package/esm/validator/types.d.ts +48 -0
  97. package/esm/validator/types.d.ts.map +1 -0
  98. package/esm/validator/types.js +140 -0
  99. package/esm/validator/value_types.d.ts +20 -0
  100. package/esm/validator/value_types.d.ts.map +1 -0
  101. package/esm/validator/value_types.js +126 -0
  102. package/package.json +1 -1
  103. package/script/compiler/inverses.d.ts +22 -0
  104. package/script/compiler/inverses.d.ts.map +1 -0
  105. package/script/compiler/inverses.js +150 -0
  106. package/script/compiler/link_target.d.ts +19 -0
  107. package/script/compiler/link_target.d.ts.map +1 -0
  108. package/script/compiler/link_target.js +69 -0
  109. package/script/compiler/mod.d.ts +7 -1
  110. package/script/compiler/mod.d.ts.map +1 -1
  111. package/script/compiler/mod.js +62 -14
  112. package/script/compiler/schema.d.ts.map +1 -1
  113. package/script/compiler/schema.js +20 -1
  114. package/script/config/markspec.d.ts +51 -0
  115. package/script/config/markspec.d.ts.map +1 -0
  116. package/script/config/markspec.js +222 -0
  117. package/script/config/mod.d.ts +4 -0
  118. package/script/config/mod.d.ts.map +1 -1
  119. package/script/config/mod.js +28 -16
  120. package/script/formatter/mod.d.ts +12 -2
  121. package/script/formatter/mod.d.ts.map +1 -1
  122. package/script/formatter/mod.js +131 -22
  123. package/script/mod.d.ts +10 -6
  124. package/script/mod.d.ts.map +1 -1
  125. package/script/mod.js +48 -19
  126. package/script/model/attributes.d.ts +47 -0
  127. package/script/model/attributes.d.ts.map +1 -0
  128. package/script/model/attributes.js +101 -0
  129. package/script/model/mod.d.ts +255 -26
  130. package/script/model/mod.d.ts.map +1 -1
  131. package/script/model/mod.js +34 -2
  132. package/script/model/profile.d.ts +189 -0
  133. package/script/model/profile.d.ts.map +1 -0
  134. package/script/model/profile.js +34 -0
  135. package/script/parser/attributes.d.ts +13 -1
  136. package/script/parser/attributes.d.ts.map +1 -1
  137. package/script/parser/attributes.js +48 -0
  138. package/script/parser/frontmatter.d.ts +53 -0
  139. package/script/parser/frontmatter.d.ts.map +1 -0
  140. package/script/parser/frontmatter.js +188 -0
  141. package/script/parser/grammars.js +11 -2
  142. package/script/parser/markdown.d.ts +11 -4
  143. package/script/parser/markdown.d.ts.map +1 -1
  144. package/script/parser/markdown.js +84 -37
  145. package/script/parser/mod.d.ts +26 -10
  146. package/script/parser/mod.d.ts.map +1 -1
  147. package/script/parser/mod.js +41 -10
  148. package/script/parser/source.d.ts +12 -8
  149. package/script/parser/source.d.ts.map +1 -1
  150. package/script/parser/source.js +20 -13
  151. package/script/profile/cache.d.ts +29 -0
  152. package/script/profile/cache.d.ts.map +1 -0
  153. package/script/profile/cache.js +48 -0
  154. package/script/profile/chain.d.ts +42 -0
  155. package/script/profile/chain.d.ts.map +1 -0
  156. package/script/profile/chain.js +229 -0
  157. package/script/profile/git-cache.d.ts +73 -0
  158. package/script/profile/git-cache.d.ts.map +1 -0
  159. package/script/profile/git-cache.js +104 -0
  160. package/script/profile/load.d.ts +28 -0
  161. package/script/profile/load.d.ts.map +1 -0
  162. package/script/profile/load.js +53 -0
  163. package/script/profile/manifest.d.ts +21 -0
  164. package/script/profile/manifest.d.ts.map +1 -0
  165. package/script/profile/manifest.js +833 -0
  166. package/script/profile/merge.d.ts +26 -0
  167. package/script/profile/merge.d.ts.map +1 -0
  168. package/script/profile/merge.js +521 -0
  169. package/script/profile/mod.d.ts +23 -0
  170. package/script/profile/mod.d.ts.map +1 -0
  171. package/script/profile/mod.js +31 -0
  172. package/script/profile/npm.d.ts +51 -0
  173. package/script/profile/npm.d.ts.map +1 -0
  174. package/script/profile/npm.js +191 -0
  175. package/script/profile/resolver.d.ts +60 -0
  176. package/script/profile/resolver.d.ts.map +1 -0
  177. package/script/profile/resolver.js +136 -0
  178. package/script/reporter/mod.js +14 -27
  179. package/script/validator/attributes.d.ts +43 -0
  180. package/script/validator/attributes.d.ts.map +1 -0
  181. package/script/validator/attributes.js +173 -0
  182. package/script/validator/mod.d.ts +27 -6
  183. package/script/validator/mod.d.ts.map +1 -1
  184. package/script/validator/mod.js +145 -123
  185. package/script/validator/normalize.d.ts +21 -0
  186. package/script/validator/normalize.d.ts.map +1 -0
  187. package/script/validator/normalize.js +61 -0
  188. package/script/validator/pattern.d.ts +23 -0
  189. package/script/validator/pattern.d.ts.map +1 -0
  190. package/script/validator/pattern.js +47 -0
  191. package/script/validator/pipeline.d.ts +30 -0
  192. package/script/validator/pipeline.d.ts.map +1 -0
  193. package/script/validator/pipeline.js +118 -0
  194. package/script/validator/traceability.d.ts +48 -0
  195. package/script/validator/traceability.d.ts.map +1 -0
  196. package/script/validator/traceability.js +146 -0
  197. package/script/validator/types.d.ts +48 -0
  198. package/script/validator/types.d.ts.map +1 -0
  199. package/script/validator/types.js +144 -0
  200. package/script/validator/value_types.d.ts +20 -0
  201. package/script/validator/value_types.d.ts.map +1 -0
  202. package/script/validator/value_types.js +129 -0
@@ -0,0 +1,169 @@
1
+ /**
2
+ * @module core/validator/attributes
3
+ *
4
+ * Validator Stage 3 — typed attribute validation.
5
+ *
6
+ * Runs after Stage 2 classification. For each entry, computes the effective
7
+ * attribute scope (universal ∪ shape ∪ type) and checks:
8
+ * - Required presence (MSL-A001)
9
+ * - Cardinality (MSL-A002 upper / MSL-A003 lower)
10
+ * - Value-type conformance (MSL-A004, delegated to value_types.ts)
11
+ * - Unknown attributes (MSL-A005 warning)
12
+ */
13
+ import { UNIVERSAL_ATTRIBUTE_KEYS } from "../model/mod.js";
14
+ import { validateValue } from "./value_types.js";
15
+ /** Core-reserved attribute keys that are always permitted regardless of profile. */
16
+ const CORE_RESERVED_KEYS = new Set([
17
+ "Id",
18
+ "Type",
19
+ ...UNIVERSAL_ATTRIBUTE_KEYS,
20
+ ]);
21
+ /**
22
+ * Compute the effective attribute scope for a given entry against the
23
+ * profile. Uses universal + shape scope always; adds type scope only when
24
+ * `entry.type` is set and the type is declared in the profile.
25
+ */
26
+ export function effectiveScope(entry, profile) {
27
+ const required = [];
28
+ const attributes = new Map();
29
+ // Universal scope.
30
+ required.push(...profile.required.value);
31
+ for (const [name, attrEntry] of profile.attributes) {
32
+ attributes.set(name, attrEntry.value);
33
+ }
34
+ // Shape scope.
35
+ const shapeScope = entry.shape === "identified"
36
+ ? profile.identified
37
+ : profile.referenced;
38
+ required.push(...shapeScope.required.value);
39
+ for (const [name, e] of shapeScope.attributes) {
40
+ attributes.set(name, e.value);
41
+ }
42
+ // Type scope (only when classified).
43
+ if (entry.type !== undefined) {
44
+ const typeEntry = profile.types.get(entry.type);
45
+ if (typeEntry !== undefined) {
46
+ required.push(...typeEntry.value.required.value);
47
+ for (const [name, e] of typeEntry.value.attributes) {
48
+ attributes.set(name, e.value);
49
+ }
50
+ }
51
+ }
52
+ // Synthesize implicit id-list attribute declarations for trace rule link
53
+ // names that aren't explicitly declared as attributes. A trace rule IS an
54
+ // id-list attribute by definition — profile authors shouldn't have to
55
+ // declare it in both places.
56
+ const shapeTrace = entry.shape === "identified"
57
+ ? profile.identified.traceability
58
+ : profile.referenced.traceability;
59
+ for (const [name] of shapeTrace) {
60
+ if (!attributes.has(name)) {
61
+ attributes.set(name, synthesizedLinkAttr(name));
62
+ }
63
+ }
64
+ if (entry.type !== undefined) {
65
+ const typeEntry = profile.types.get(entry.type);
66
+ if (typeEntry !== undefined) {
67
+ for (const [name] of typeEntry.value.traceability) {
68
+ if (!attributes.has(name)) {
69
+ attributes.set(name, synthesizedLinkAttr(name));
70
+ }
71
+ }
72
+ }
73
+ }
74
+ return { required, attributes };
75
+ }
76
+ /**
77
+ * Build a synthesized `id-list` AttrDecl for a trace rule's link name when
78
+ * the profile doesn't explicitly declare the attribute. This keeps Stage 2.5
79
+ * (CSV normalization) and Stage 3 (unknown-attribute check) honest without
80
+ * requiring profile authors to double-declare link attributes.
81
+ *
82
+ * Cardinality defaults to 0..N (unbounded list). Cardinality on the trace
83
+ * rule itself is enforced by Stage 4 (traceability), which reads the rule
84
+ * directly.
85
+ */
86
+ function synthesizedLinkAttr(name) {
87
+ return {
88
+ name,
89
+ type: "id-list",
90
+ required: false,
91
+ cardinality: { lower: 0, upper: Infinity },
92
+ };
93
+ }
94
+ /**
95
+ * Run Stage 3 structural + value-type checks for one entry. Returns all
96
+ * diagnostics the entry produced.
97
+ *
98
+ * Task 6.2 ships required / cardinality / unknown checks. Task 6.6 wires in
99
+ * value-type conformance (MSL-A004) through the value-types registry.
100
+ */
101
+ export function validateAttributesForEntry(entry, profile) {
102
+ const diagnostics = [];
103
+ const scope = effectiveScope(entry, profile);
104
+ const present = entry.typedAttributes;
105
+ // MSL-A001: required attribute missing.
106
+ for (const name of scope.required) {
107
+ if (!present.has(name)) {
108
+ diagnostics.push({
109
+ code: "MSL-A001",
110
+ severity: "error",
111
+ message: `${entry.displayId}: required attribute '${name}' is missing`,
112
+ location: entry.location,
113
+ });
114
+ }
115
+ }
116
+ // Iterate attributes present on the entry.
117
+ for (const [name, values] of present) {
118
+ const decl = scope.attributes.get(name);
119
+ if (decl === undefined) {
120
+ // MSL-A005: unknown attribute (warn). Skip core-reserved keys.
121
+ if (!CORE_RESERVED_KEYS.has(name)) {
122
+ diagnostics.push({
123
+ code: "MSL-A005",
124
+ severity: "warning",
125
+ message: `${entry.displayId}: attribute '${name}' is not declared in the profile scope`,
126
+ location: entry.location,
127
+ });
128
+ }
129
+ continue;
130
+ }
131
+ // MSL-A002: upper cardinality.
132
+ if (values.length > decl.cardinality.upper) {
133
+ diagnostics.push({
134
+ code: "MSL-A002",
135
+ severity: "error",
136
+ message: `${entry.displayId}: attribute '${name}' has ${values.length} values ` +
137
+ `but max is ${formatUpper(decl.cardinality.upper)}`,
138
+ location: entry.location,
139
+ });
140
+ }
141
+ // MSL-A003: lower cardinality (only when attribute is present).
142
+ if (values.length < decl.cardinality.lower) {
143
+ diagnostics.push({
144
+ code: "MSL-A003",
145
+ severity: "error",
146
+ message: `${entry.displayId}: attribute '${name}' has ${values.length} values ` +
147
+ `but min is ${decl.cardinality.lower}`,
148
+ location: entry.location,
149
+ });
150
+ }
151
+ // MSL-A004: value-type conformance.
152
+ for (const v of values) {
153
+ const detail = validateValue(v, decl);
154
+ if (detail !== null) {
155
+ diagnostics.push({
156
+ code: "MSL-A004",
157
+ severity: "error",
158
+ message: `${entry.displayId}: attribute '${name}' has invalid value: ${detail}`,
159
+ location: entry.location,
160
+ });
161
+ break; // one diagnostic per attribute is enough
162
+ }
163
+ }
164
+ }
165
+ return diagnostics;
166
+ }
167
+ function formatUpper(u) {
168
+ return u === Infinity ? "N" : String(u);
169
+ }
@@ -1,10 +1,19 @@
1
1
  /**
2
2
  * @module validator
3
3
  *
4
- * ID graph validator. Performs file-local checks and cross-file checks:
5
- * broken references, missing Ids, malformed entries, duplicate IDs.
4
+ * Core structural validator. Performs file-local checks and cross-file
5
+ * checks for structural integrity: well-formed `Id:` values, unique
6
+ * display IDs, unique identity values, resolvable cross-references.
7
+ *
8
+ * Shape discrimination follows the language spec Part 2.4: `Id:` value
9
+ * is a bare ULID → identified entry; `Id:` value is a scheme-qualified
10
+ * URI (RFC 3986) → referenced entry; anything else is an error.
11
+ *
12
+ * Type-level rules (required-per-type attributes, relation target-type
13
+ * constraints, enum vocabularies) are profile-declared and evaluated by
14
+ * a profile-aware validator layer (not in the core).
6
15
  */
7
- import type { Diagnostic, Entry } from "../model/mod.js";
16
+ import type { Attribute, Diagnostic, Entry } from "../model/mod.js";
8
17
  /** Result of a validation pass. */
9
18
  export interface ValidateResult {
10
19
  /** Diagnostics found during validation. */
@@ -13,11 +22,23 @@ export interface ValidateResult {
13
22
  readonly valid: boolean;
14
23
  }
15
24
  /**
16
- * Validate a set of parsed entries for structural correctness
17
- * and reference integrity.
25
+ * Validate a set of parsed entries for structural correctness and
26
+ * cross-reference integrity.
18
27
  *
19
28
  * @param entries - Parsed entries to validate
20
- * @returns Validation result with diagnostics
21
29
  */
22
30
  export declare function validate(entries: readonly Entry[]): ValidateResult;
31
+ export declare function indexByDisplayId(entries: readonly Entry[]): ReadonlyMap<string, Entry>;
32
+ export declare function findAttr(attrs: readonly Attribute[], key: string): Attribute | undefined;
33
+ export { runPipeline } from "./pipeline.js";
34
+ export type { PipelineResult } from "./pipeline.js";
35
+ export { classifyEntriesStage, classifyEntry } from "./types.js";
36
+ export type { ClassifyResult, ClassifyStageResult } from "./types.js";
37
+ export { compileDisplayIdPattern } from "./pattern.js";
38
+ export { effectiveScope, validateAttributesForEntry } from "./attributes.js";
39
+ export type { EffectiveAttrScope } from "./attributes.js";
40
+ export { validateValue } from "./value_types.js";
41
+ export type { ValueValidator } from "./value_types.js";
42
+ export { normalizeListValues } from "./normalize.js";
43
+ export { effectiveTraceRules, matchesAnyTarget, validateTraceabilityForEntry, } from "./traceability.js";
23
44
  //# sourceMappingURL=mod.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/validator/mod.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAa,UAAU,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AA6BpE,mCAAmC;AACnC,MAAM,WAAW,cAAc;IAC7B,2CAA2C;IAC3C,QAAQ,CAAC,WAAW,EAAE,SAAS,UAAU,EAAE,CAAC;IAC5C,gDAAgD;IAChD,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;CACzB;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,SAAS,KAAK,EAAE,GAAG,cAAc,CAQlE"}
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/validator/mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAWpE,mCAAmC;AACnC,MAAM,WAAW,cAAc;IAC7B,2CAA2C;IAC3C,QAAQ,CAAC,WAAW,EAAE,SAAS,UAAU,EAAE,CAAC;IAC5C,gDAAgD;IAChD,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;CACzB;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,OAAO,EAAE,SAAS,KAAK,EAAE,GAAG,cAAc,CAQlE;AAyLD,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,SAAS,KAAK,EAAE,GACxB,WAAW,CAAC,MAAM,EAAE,KAAK,CAAC,CAM5B;AAGD,wBAAgB,QAAQ,CACtB,KAAK,EAAE,SAAS,SAAS,EAAE,EAC3B,GAAG,EAAE,MAAM,GACV,SAAS,GAAG,SAAS,CAEvB;AAED,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,YAAY,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEpD,OAAO,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACjE,YAAY,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAEtE,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAEvD,OAAO,EAAE,cAAc,EAAE,0BAA0B,EAAE,MAAM,iBAAiB,CAAC;AAC7E,YAAY,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAE1D,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,YAAY,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAErD,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAChB,4BAA4B,GAC7B,MAAM,mBAAmB,CAAC"}
@@ -1,39 +1,26 @@
1
1
  /**
2
2
  * @module validator
3
3
  *
4
- * ID graph validator. Performs file-local checks and cross-file checks:
5
- * broken references, missing Ids, malformed entries, duplicate IDs.
4
+ * Core structural validator. Performs file-local checks and cross-file
5
+ * checks for structural integrity: well-formed `Id:` values, unique
6
+ * display IDs, unique identity values, resolvable cross-references.
7
+ *
8
+ * Shape discrimination follows the language spec Part 2.4: `Id:` value
9
+ * is a bare ULID → identified entry; `Id:` value is a scheme-qualified
10
+ * URI (RFC 3986) → referenced entry; anything else is an error.
11
+ *
12
+ * Type-level rules (required-per-type attributes, relation target-type
13
+ * constraints, enum vocabularies) are profile-declared and evaluated by
14
+ * a profile-aware validator layer (not in the core).
6
15
  */
7
- /** Known attribute keys for typed entries. */
8
- const TYPED_ATTR_KEYS = new Set([
9
- "Id",
10
- "Satisfies",
11
- "Derived-from",
12
- "Allocates",
13
- "Component",
14
- "Constrains",
15
- "Between",
16
- "Interface",
17
- "Verifies",
18
- "Implements",
19
- "Labels",
20
- ]);
21
- /** Known attribute keys for reference entries. */
22
- const REF_ATTR_KEYS = new Set([
23
- "Document",
24
- "URL",
25
- "Status",
26
- "Superseded-by",
27
- "Derived-from",
28
- ]);
29
- /** ULID format: TYPE prefix + underscore + 12-26 alphanumeric chars. */
30
- const ULID_RE = /^[A-Z]+_[0-9A-Z]{12,26}$/;
16
+ import { IDENTITY_KEY, ULID_RE, UNIVERSAL_ATTRIBUTE_KEYS, URI_SCHEME_RE, } from "../model/mod.js";
17
+ /** Universal attribute keys the core recognizes. */
18
+ const UNIVERSAL_KEYS = new Set(UNIVERSAL_ATTRIBUTE_KEYS);
31
19
  /**
32
- * Validate a set of parsed entries for structural correctness
33
- * and reference integrity.
20
+ * Validate a set of parsed entries for structural correctness and
21
+ * cross-reference integrity.
34
22
  *
35
23
  * @param entries - Parsed entries to validate
36
- * @returns Validation result with diagnostics
37
24
  */
38
25
  export function validate(entries) {
39
26
  const diagnostics = [];
@@ -45,162 +32,183 @@ export function validate(entries) {
45
32
  /** MSL-R structural checks on individual entries and cross-entry uniqueness. */
46
33
  function checkStructural(entries, diagnostics) {
47
34
  const displayIds = new Map();
48
- const ulids = new Map();
35
+ const ids = new Map();
49
36
  for (const entry of entries) {
50
- const isTyped = entry.entryType != null;
51
- // MSL-R003: Typed entry must have Id attribute with valid ULID format.
52
- if (isTyped) {
53
- if (!entry.id) {
54
- diagnostics.push({
55
- code: "MSL-R003",
56
- severity: "error",
57
- message: `${entry.displayId}: missing Id attribute`,
58
- location: entry.location,
59
- });
60
- }
61
- else if (!ULID_RE.test(entry.id)) {
37
+ // MSL-R003: exactly one `Id:` attribute per entry.
38
+ const idAttrs = entry.rawAttributes.filter((a) => a.key === IDENTITY_KEY);
39
+ if (idAttrs.length === 0) {
40
+ diagnostics.push({
41
+ code: "MSL-R003",
42
+ severity: "error",
43
+ message: `${entry.displayId}: missing Id: attribute`,
44
+ location: entry.location,
45
+ });
46
+ }
47
+ else if (idAttrs.length > 1) {
48
+ diagnostics.push({
49
+ code: "MSL-R003",
50
+ severity: "error",
51
+ message: `${entry.displayId}: multiple Id: attributes (${idAttrs.length}) — exactly one is required`,
52
+ location: entry.location,
53
+ });
54
+ }
55
+ // MSL-R004: `Id:` value must be a ULID or a scheme-qualified URI.
56
+ if (idAttrs.length > 0) {
57
+ const value = idAttrs[0].value;
58
+ const isUlid = ULID_RE.test(value);
59
+ const isUri = URI_SCHEME_RE.test(value);
60
+ if (!isUlid && !isUri) {
62
61
  diagnostics.push({
63
- code: "MSL-R003",
62
+ code: "MSL-R004",
64
63
  severity: "error",
65
- message: `${entry.displayId}: malformed Id '${entry.id}'`,
64
+ message: `${entry.displayId}: Id: value '${value}' is neither a bare ULID nor a scheme-qualified URI`,
66
65
  location: entry.location,
67
66
  });
68
67
  }
69
- }
70
- // MSL-R004: Exactly one Id per entry.
71
- if (isTyped) {
72
- const idCount = entry.attributes.filter((a) => a.key === "Id").length;
73
- if (idCount > 1) {
68
+ // Shape consistency: ULID ↔ identified, URI ↔ referenced.
69
+ if (isUlid && entry.shape !== "identified") {
74
70
  diagnostics.push({
75
71
  code: "MSL-R004",
76
72
  severity: "error",
77
- message: `${entry.displayId}: multiple Id attributes (${idCount})`,
73
+ message: `${entry.displayId}: Id: value is a ULID but entry shape is ${entry.shape}`,
78
74
  location: entry.location,
79
75
  });
80
76
  }
81
- }
82
- // MSL-R007: Display ID type prefix must match ULID type prefix.
83
- if (isTyped && entry.id && ULID_RE.test(entry.id)) {
84
- const displayPrefix = entry.entryType;
85
- const ulidPrefix = entry.id.split("_")[0];
86
- if (displayPrefix !== ulidPrefix) {
77
+ else if (isUri && entry.shape !== "referenced") {
87
78
  diagnostics.push({
88
- code: "MSL-R007",
79
+ code: "MSL-R004",
89
80
  severity: "error",
90
- message: `${entry.displayId}: type prefix '${displayPrefix}' does not match Id prefix '${ulidPrefix}'`,
81
+ message: `${entry.displayId}: Id: value is a URI but entry shape is ${entry.shape}`,
91
82
  location: entry.location,
92
83
  });
93
84
  }
94
85
  }
95
86
  // MSL-R006: Display ID unique across all entries.
96
- const existing = displayIds.get(entry.displayId);
97
- if (existing) {
87
+ const existingDisplay = displayIds.get(entry.displayId);
88
+ if (existingDisplay) {
98
89
  diagnostics.push({
99
90
  code: "MSL-R006",
100
91
  severity: "error",
101
- message: `duplicate display ID '${entry.displayId}' (also at ${existing.location.file}:${existing.location.line})`,
92
+ message: `duplicate display ID '${entry.displayId}' (also at ${existingDisplay.location.file}:${existingDisplay.location.line})`,
102
93
  location: entry.location,
103
94
  });
104
95
  }
105
96
  else {
106
97
  displayIds.set(entry.displayId, entry);
107
98
  }
108
- // MSL-R005: ULID unique across all entries.
99
+ // MSL-R005: identity value unique across all entries.
109
100
  if (entry.id) {
110
- const existingUlid = ulids.get(entry.id);
111
- if (existingUlid) {
101
+ const existingId = ids.get(entry.id);
102
+ if (existingId) {
112
103
  diagnostics.push({
113
104
  code: "MSL-R005",
114
105
  severity: "error",
115
- message: `duplicate Id '${entry.id}' (also at ${existingUlid.location.file}:${existingUlid.location.line})`,
106
+ message: `duplicate Id '${entry.id}' (also at ${existingId.location.file}:${existingId.location.line})`,
116
107
  location: entry.location,
117
108
  });
118
109
  }
119
110
  else {
120
- ulids.set(entry.id, entry);
111
+ ids.set(entry.id, entry);
121
112
  }
122
113
  }
123
- // MSL-R010: Unknown attribute keys.
124
- const knownKeys = isTyped ? TYPED_ATTR_KEYS : REF_ATTR_KEYS;
125
- for (const attr of entry.attributes) {
126
- if (!knownKeys.has(attr.key)) {
114
+ // MSL-R010: Unknown attributes are warnings in the core. A
115
+ // profile-aware validator widens this check to include profile-declared
116
+ // attributes; until then, anything outside the universal set is
117
+ // unrecognized.
118
+ for (const attr of entry.rawAttributes) {
119
+ if (!UNIVERSAL_KEYS.has(attr.key) && attr.key !== "Type") {
127
120
  diagnostics.push({
128
121
  code: "MSL-R010",
129
122
  severity: "warning",
130
- message: `${entry.displayId}: unknown attribute '${attr.key}'`,
123
+ message: `${entry.displayId}: unknown attribute '${attr.key}' (not in core universal set; profile-declared attributes are permitted when a profile is loaded)`,
131
124
  location: entry.location,
132
125
  });
133
126
  }
134
127
  }
135
128
  }
136
129
  }
137
- /** MSL-T reference integrity checks. */
130
+ /** Cross-reference integrity checks. */
138
131
  function checkReferences(entries, diagnostics) {
139
- const knownIds = new Set(entries.map((e) => e.displayId));
132
+ // Build a display-ID index for cross-reference resolution.
133
+ const byDisplayId = new Map();
140
134
  for (const entry of entries) {
141
- // MSL-T001: Satisfies targets must exist.
142
- const satisfies = findAttr(entry.attributes, "Satisfies");
143
- if (satisfies) {
144
- const targets = satisfies.value.split(",").map((s) => s.trim());
145
- for (const target of targets) {
146
- if (!target)
147
- continue;
148
- if (!knownIds.has(target)) {
149
- diagnostics.push({
150
- code: "MSL-T001",
151
- severity: "error",
152
- message: `${entry.displayId}: unresolved reference '${target}' in Satisfies`,
153
- location: entry.location,
154
- });
155
- }
156
- }
135
+ if (!byDisplayId.has(entry.displayId)) {
136
+ byDisplayId.set(entry.displayId, entry);
137
+ }
138
+ }
139
+ // Also index by `Id:` value so references may target ULIDs or URIs
140
+ // directly (profile tooling commonly accepts either).
141
+ const byId = new Map();
142
+ for (const entry of entries) {
143
+ if (entry.id && !byId.has(entry.id)) {
144
+ byId.set(entry.id, entry);
145
+ }
146
+ }
147
+ // MSL-T012: `Supersedes:` — the one baked-in relation. Target must
148
+ // resolve; profile rules check type/shape compatibility separately.
149
+ for (const entry of entries) {
150
+ const supersedes = entry.rawAttributes.find((a) => a.key === "Supersedes");
151
+ if (!supersedes)
152
+ continue;
153
+ const target = supersedes.value.trim();
154
+ const resolved = byDisplayId.get(target) ?? byId.get(target);
155
+ if (!resolved) {
156
+ diagnostics.push({
157
+ code: "MSL-T012",
158
+ severity: "error",
159
+ message: `${entry.displayId}: Supersedes target '${target}' not found`,
160
+ location: entry.location,
161
+ });
157
162
  }
158
- // MSL-T004: Derived-from ID portion checked against known entries.
159
- const derivedFrom = findAttr(entry.attributes, "Derived-from");
160
- if (derivedFrom) {
161
- const idPart = derivedFrom.value.split(/\s/)[0];
162
- if (idPart && !knownIds.has(idPart)) {
163
+ }
164
+ // MSL-T005: `References:` — citations must resolve to a referenced
165
+ // entry. This is a universal relation (citing identified → referenced).
166
+ for (const entry of entries) {
167
+ const citations = entry.rawAttributes.filter((a) => a.key === "References");
168
+ for (const attr of citations) {
169
+ // Citation format: "slug [free-text locator]"; take the first token.
170
+ const slug = attr.value.split(/\s/)[0];
171
+ if (!slug)
172
+ continue;
173
+ const resolved = byDisplayId.get(slug) ?? byId.get(slug);
174
+ if (!resolved) {
163
175
  diagnostics.push({
164
- code: "MSL-T004",
165
- severity: "warning",
166
- message: `${entry.displayId}: unresolved Derived-from reference '${idPart}'`,
176
+ code: "MSL-T005",
177
+ severity: "error",
178
+ message: `${entry.displayId}: References: unresolved citation '${slug}'`,
167
179
  location: entry.location,
168
180
  });
181
+ continue;
169
182
  }
170
- }
171
- // MSL-T008: Allocates targets must be SRS entries (resolve against known IDs).
172
- const allocates = findAttr(entry.attributes, "Allocates");
173
- if (allocates) {
174
- const targets = allocates.value.split(",").map((s) => s.trim());
175
- for (const target of targets) {
176
- if (!target)
177
- continue;
178
- if (!knownIds.has(target)) {
179
- diagnostics.push({
180
- code: "MSL-T008",
181
- severity: "error",
182
- message: `${entry.displayId}: unresolved reference '${target}' in Allocates`,
183
- location: entry.location,
184
- });
185
- }
186
- }
187
- }
188
- // MSL-T009: Between must list exactly two parties.
189
- const between = findAttr(entry.attributes, "Between");
190
- if (between) {
191
- const parties = between.value.split(",").map((s) => s.trim()).filter((s) => s.length > 0);
192
- if (parties.length !== 2) {
183
+ if (resolved.shape !== "referenced") {
193
184
  diagnostics.push({
194
- code: "MSL-T009",
185
+ code: "MSL-T005",
195
186
  severity: "error",
196
- message: `${entry.displayId}: Between must list exactly 2 parties, found ${parties.length}`,
187
+ message: `${entry.displayId}: References: target '${slug}' is shape '${resolved.shape}', expected 'referenced'`,
197
188
  location: entry.location,
198
189
  });
199
190
  }
200
191
  }
201
192
  }
202
193
  }
203
- /** Find first attribute by key. */
204
- function findAttr(attrs, key) {
194
+ // Re-exported for callers that want to write their own cross-reference
195
+ // checks over the core index.
196
+ export function indexByDisplayId(entries) {
197
+ const map = new Map();
198
+ for (const entry of entries) {
199
+ if (!map.has(entry.displayId))
200
+ map.set(entry.displayId, entry);
201
+ }
202
+ return map;
203
+ }
204
+ // Retained export for downstream consumers.
205
+ export function findAttr(attrs, key) {
205
206
  return attrs.find((a) => a.key === key);
206
207
  }
208
+ export { runPipeline } from "./pipeline.js";
209
+ export { classifyEntriesStage, classifyEntry } from "./types.js";
210
+ export { compileDisplayIdPattern } from "./pattern.js";
211
+ export { effectiveScope, validateAttributesForEntry } from "./attributes.js";
212
+ export { validateValue } from "./value_types.js";
213
+ export { normalizeListValues } from "./normalize.js";
214
+ export { effectiveTraceRules, matchesAnyTarget, validateTraceabilityForEntry, } from "./traceability.js";
@@ -0,0 +1,21 @@
1
+ /**
2
+ * @module core/validator/normalize
3
+ *
4
+ * Stage 2.5 normalization: split comma-separated values for profile-declared
5
+ * list-typed attributes.
6
+ *
7
+ * Runs between Stage 2 (classification) and Stage 3 (typed attributes). The
8
+ * core parser already CSV-splits values for core-catalog list types; this
9
+ * step handles list types declared by a profile (which the parser doesn't
10
+ * see). Idempotent — values that don't contain commas pass through unchanged.
11
+ *
12
+ * Empty-string fragments (e.g. from `"a,,b"`) are dropped; surrounding
13
+ * whitespace is trimmed.
14
+ */
15
+ import type { EffectiveProfile, Entry } from "../model/mod.js";
16
+ /**
17
+ * Return a new Entry with list-typed attribute values split on commas. If no
18
+ * change is needed, returns the input entry unchanged (reference equality).
19
+ */
20
+ export declare function normalizeListValues(entry: Entry, profile: EffectiveProfile): Entry;
21
+ //# sourceMappingURL=normalize.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../../src/validator/normalize.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,KAAK,EAAa,MAAM,iBAAiB,CAAC;AAK1E;;;GAGG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,gBAAgB,GACxB,KAAK,CAoBP"}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @module core/validator/normalize
3
+ *
4
+ * Stage 2.5 normalization: split comma-separated values for profile-declared
5
+ * list-typed attributes.
6
+ *
7
+ * Runs between Stage 2 (classification) and Stage 3 (typed attributes). The
8
+ * core parser already CSV-splits values for core-catalog list types; this
9
+ * step handles list types declared by a profile (which the parser doesn't
10
+ * see). Idempotent — values that don't contain commas pass through unchanged.
11
+ *
12
+ * Empty-string fragments (e.g. from `"a,,b"`) are dropped; surrounding
13
+ * whitespace is trimmed.
14
+ */
15
+ import { effectiveScope } from "./attributes.js";
16
+ const LIST_TYPES = new Set(["id-list", "tag-list"]);
17
+ /**
18
+ * Return a new Entry with list-typed attribute values split on commas. If no
19
+ * change is needed, returns the input entry unchanged (reference equality).
20
+ */
21
+ export function normalizeListValues(entry, profile) {
22
+ const scope = effectiveScope(entry, profile);
23
+ const rewritten = new Map();
24
+ let anyChange = false;
25
+ for (const [name, values] of entry.typedAttributes) {
26
+ const decl = scope.attributes.get(name);
27
+ if (decl !== undefined && LIST_TYPES.has(decl.type)) {
28
+ const split = splitListValues(values);
29
+ if (!arraysEqual(split, values)) {
30
+ rewritten.set(name, split);
31
+ anyChange = true;
32
+ continue;
33
+ }
34
+ }
35
+ rewritten.set(name, values);
36
+ }
37
+ if (!anyChange)
38
+ return entry;
39
+ return { ...entry, typedAttributes: rewritten };
40
+ }
41
+ function splitListValues(values) {
42
+ const out = [];
43
+ for (const v of values) {
44
+ const parts = v.split(",").map((s) => s.trim()).filter((s) => s.length > 0);
45
+ if (parts.length === 0)
46
+ continue;
47
+ out.push(...parts);
48
+ }
49
+ return out;
50
+ }
51
+ function arraysEqual(a, b) {
52
+ if (a.length !== b.length)
53
+ return false;
54
+ for (let i = 0; i < a.length; i++)
55
+ if (a[i] !== b[i])
56
+ return false;
57
+ return true;
58
+ }