@aestheticfunction/dspack-emit 0.6.0 → 0.8.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.
- package/README.md +23 -15
- package/dist/cli.js +4 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/targets/a2ui/surface.js +108 -4
- package/dist/transform/emit-v1_0.d.ts +45 -0
- package/dist/transform/emit-v1_0.js +190 -0
- package/dist/transform/emit.d.ts +6 -9
- package/dist/transform/emit.js +5 -44
- package/dist/transform/index.js +3 -1
- package/dist/types.d.ts +4 -0
- package/dist/validate/ajv.d.ts +26 -0
- package/dist/validate/ajv.js +150 -23
- package/dist/validate/meta/a2ui-v1_0.d.ts +9 -0
- package/dist/validate/meta/a2ui-v1_0.js +811 -0
- package/dist/validate/meta/catalog-meta.js +34 -21
- package/package.json +6 -2
- package/src/byte-neutral.test.ts +6 -3
- package/src/cli.ts +5 -2
- package/src/engine-boundary.test.ts +10 -0
- package/src/functions.test.ts +48 -1
- package/src/index.ts +1 -1
- package/src/instance-diagnostics.test.ts +194 -0
- package/src/profile-v2.test.ts +1 -1
- package/src/review-findings.test.ts +4 -1
- package/src/surface-emit.test.ts +43 -11
- package/src/surface-fidelity.test.ts +12 -0
- package/src/t1.test.ts +9 -8
- package/src/targets/a2ui/surface.ts +124 -4
- package/src/transform/emit-v1_0.ts +215 -0
- package/src/transform/emit.ts +10 -54
- package/src/transform/index.ts +3 -1
- package/src/transform.test.ts +33 -1
- package/src/types.ts +4 -0
- package/src/v1_0-catalog.test.ts +267 -0
- package/src/validate/ajv.ts +169 -22
- package/src/validate/meta/a2ui-catalog.meta.1_0.json +67 -13
- package/src/validate/meta/a2ui-v1_0.ts +820 -0
- package/src/validate/meta/catalog-meta.ts +34 -21
- package/dist/validate/meta/a2ui-catalog.meta.0_9_1.json +0 -39
- package/dist/validate/meta/a2ui-catalog.meta.1_0.json +0 -34
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The A2UI 1.0 catalog, discrepancy by discrepancy (dspack-studio docs/repositioning/protocol-check/p0/README.md,
|
|
3
|
+
* "1.0 discrepancies to close"), against the pinned upstream specification (fixtures/a2ui/PIN.md). The 0.9.1 catalog
|
|
4
|
+
* is pinned byte-for-byte in byte-neutral.test.ts; nothing here may move it.
|
|
5
|
+
*/
|
|
6
|
+
import { readFileSync } from "node:fs";
|
|
7
|
+
import { describe, expect, it } from "vitest";
|
|
8
|
+
import { transformFromJson } from "./transform/index.js";
|
|
9
|
+
import { emitCatalogV1_0, fidelityV1_0, COMMON_TYPES_V1_0, ARIA_TO_ACCESSIBILITY } from "./transform/emit-v1_0.js";
|
|
10
|
+
import { scaffoldProfile } from "./transform/scaffold.js";
|
|
11
|
+
import { loadProfile } from "./transform/profile-load.js";
|
|
12
|
+
import { validateCatalog, catalogCompanionSchemas } from "./validate/ajv.js";
|
|
13
|
+
import Ajv2020 from "ajv/dist/2020.js";
|
|
14
|
+
import addFormats from "ajv-formats";
|
|
15
|
+
import { catalogDefinitionV1_0, commonTypesV1_0 } from "./validate/meta/a2ui-v1_0.js";
|
|
16
|
+
import type { A2uiCatalog, DspackDoc, Json } from "./types.js";
|
|
17
|
+
import type { MappingResult } from "./transform/mapping.js";
|
|
18
|
+
import { shadcnProfile } from "./transform/profiles.js";
|
|
19
|
+
|
|
20
|
+
const read = (rel: string) => JSON.parse(readFileSync(new URL(rel, import.meta.url), "utf8"));
|
|
21
|
+
const shadcnDoc = read("../input/shadcn-ui.dspack.json") as DspackDoc;
|
|
22
|
+
const astryxDoc = read("../input/astryx.dspack.json") as DspackDoc;
|
|
23
|
+
const IDENTIFIER = /^[\p{XID_Start}_][\p{XID_Continue}]*$/u;
|
|
24
|
+
const ALLOWED_TOP_LEVEL = ["$schema", "$id", "protocolVersion", "title", "description", "catalogId", "instructions", "metadata", "components", "functions", "$defs"];
|
|
25
|
+
|
|
26
|
+
const catalogs: Record<string, () => A2uiCatalog> = {
|
|
27
|
+
"shadcn/ui": () => transformFromJson(shadcnDoc, { a2uiVersion: "1.0" }).catalog,
|
|
28
|
+
"Astryx (scaffolded)": () =>
|
|
29
|
+
transformFromJson(astryxDoc, {
|
|
30
|
+
a2uiVersion: "1.0",
|
|
31
|
+
profile: loadProfile(scaffoldProfile(astryxDoc, { catalogIdBase: "https://example.test/catalogs/astryx-scaffold" }).profile),
|
|
32
|
+
}).catalog,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function refs(node: unknown, acc: string[] = []): string[] {
|
|
36
|
+
if (Array.isArray(node)) node.forEach((n) => refs(n, acc));
|
|
37
|
+
else if (node && typeof node === "object") {
|
|
38
|
+
for (const [k, v] of Object.entries(node)) {
|
|
39
|
+
if (k === "$ref" && typeof v === "string") acc.push(v);
|
|
40
|
+
else refs(v, acc);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return acc;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function propertyNames(component: Json): string[] {
|
|
47
|
+
const own = (component.properties ?? {}) as Record<string, unknown>;
|
|
48
|
+
const nested = ((component.allOf ?? []) as Json[]).flatMap((p) => Object.keys((p.properties ?? {}) as object));
|
|
49
|
+
return [...Object.keys(own), ...nested];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
for (const [name, build] of Object.entries(catalogs)) {
|
|
53
|
+
describe(`A2UI 1.0 catalog: ${name}`, () => {
|
|
54
|
+
const catalog = build();
|
|
55
|
+
|
|
56
|
+
it("declares protocolVersion 1.0, and uses only the root keys the specification allows", () => {
|
|
57
|
+
expect(catalog.protocolVersion).toBe("1.0");
|
|
58
|
+
for (const key of Object.keys(catalog)) expect(ALLOWED_TOP_LEVEL).toContain(key);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("$defs holds anyComponent and anyFunction only; with no functions, anyFunction admits no call", () => {
|
|
62
|
+
expect(Object.keys(catalog.$defs).sort()).toEqual(["anyComponent", "anyFunction"]);
|
|
63
|
+
expect(catalog.functions).toBeUndefined();
|
|
64
|
+
expect(catalog.$defs.anyFunction).toEqual({ not: {} });
|
|
65
|
+
expect(catalog.$defs.surfaceProperties).toBeUndefined();
|
|
66
|
+
expect(catalog.$defs.theme).toBeUndefined();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("references standard types in common_types.json only, and only the listed ones", () => {
|
|
70
|
+
for (const ref of refs(catalog.components)) {
|
|
71
|
+
if (ref.startsWith("#/")) continue;
|
|
72
|
+
const m = /^common_types\.json#\/\$defs\/(.+)$/.exec(ref);
|
|
73
|
+
expect(m, ref).not.toBeNull();
|
|
74
|
+
expect(COMMON_TYPES_V1_0 as readonly string[]).toContain(m![1]);
|
|
75
|
+
}
|
|
76
|
+
expect(refs(catalog).some((r) => r.includes("ComponentCommon"))).toBe(false);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("components are flat, closed by the envelope rather than by themselves", () => {
|
|
80
|
+
for (const [cname, c] of Object.entries(catalog.components)) {
|
|
81
|
+
expect(IDENTIFIER.test(cname), cname).toBe(true);
|
|
82
|
+
expect(c.unevaluatedProperties, cname).toBeUndefined();
|
|
83
|
+
for (const part of (c.allOf ?? []) as Json[]) if ("$ref" in part) expect(String(part.$ref), cname).toMatch(/common_types\.json#\/\$defs\/Checkable$/);
|
|
84
|
+
const discriminator = ((c.properties ?? ((c.allOf as Json[]).find((p) => !("$ref" in p)) as Json).properties) as Record<string, Json>).component;
|
|
85
|
+
expect(discriminator, cname).toEqual({ const: cname });
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("every property name is a UAX #31 identifier", () => {
|
|
90
|
+
for (const [cname, c] of Object.entries(catalog.components)) {
|
|
91
|
+
for (const prop of propertyNames(c)) expect(IDENTIFIER.test(prop), `${cname}.${prop}`).toBe(true);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("design tokens are catalog metadata, under a product-prefixed extension", () => {
|
|
96
|
+
const ext = (catalog.metadata as { extensions: Record<string, Json> }).extensions;
|
|
97
|
+
expect(Object.keys(ext)).toEqual(["dspack_tokens"]);
|
|
98
|
+
expect(ext.dspack_tokens.palette).toBeTypeOf("object");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("passes gates A1 and A2, the second being the official catalog definition", () => {
|
|
102
|
+
const report = validateCatalog(catalog, "1.0");
|
|
103
|
+
for (const g of report.gates) expect(g.pass, `${g.name}: ${JSON.stringify(g.errors)}`).toBe(true);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
describe("ARIA properties map onto the envelope's @accessibility", () => {
|
|
109
|
+
// dspack-emit's own shadcn/ui input declares no ARIA-named property; Composer's shadcn/ui v3 contract does (Spinner,
|
|
110
|
+
// Skeleton), and the conformance run over Composer's catalogs covers that real case. Here, a probe with the same
|
|
111
|
+
// shape goes through the same emitter and report.
|
|
112
|
+
const mapping = structuredClone(transformFromJson(shadcnDoc, { a2uiVersion: "0.9.1" }).mapping);
|
|
113
|
+
mapping.components.Spinner = {
|
|
114
|
+
type: "object",
|
|
115
|
+
allOf: [
|
|
116
|
+
{ $ref: "#/$defs/ComponentCommon" },
|
|
117
|
+
{
|
|
118
|
+
type: "object",
|
|
119
|
+
properties: {
|
|
120
|
+
component: { const: "Spinner" },
|
|
121
|
+
className: { type: "string" },
|
|
122
|
+
"aria-label": { type: "string", default: "Loading" },
|
|
123
|
+
"aria-hidden": { type: "boolean" },
|
|
124
|
+
},
|
|
125
|
+
required: ["component"],
|
|
126
|
+
},
|
|
127
|
+
],
|
|
128
|
+
unevaluatedProperties: false,
|
|
129
|
+
};
|
|
130
|
+
mapping.componentOrder = [...mapping.componentOrder, "Spinner"];
|
|
131
|
+
mapping.fidelity = [
|
|
132
|
+
...mapping.fidelity,
|
|
133
|
+
{ source: "components.spinner.props.aria-label", target: "components.Spinner.properties.aria-label", class: "maps-cleanly", note: "Property mapped onto the A2UI property." },
|
|
134
|
+
];
|
|
135
|
+
const catalog = emitCatalogV1_0(mapping, shadcnProfile, "https://example.test/v1_0/catalog.json");
|
|
136
|
+
const spinner = (catalog.components as Record<string, Json>).Spinner;
|
|
137
|
+
|
|
138
|
+
it("the component no longer declares aria-* properties; the mapping is recorded on the component", () => {
|
|
139
|
+
expect(Object.keys(spinner.properties as object).sort()).toEqual(["className", "component"]);
|
|
140
|
+
expect(spinner.metadata).toEqual({
|
|
141
|
+
extensions: {
|
|
142
|
+
dspack_accessibility: {
|
|
143
|
+
properties: {
|
|
144
|
+
"aria-label": { carriedBy: "@accessibility.label", schema: { type: "string", default: "Loading" } },
|
|
145
|
+
"aria-hidden": { carriedBy: "@accessibility.hidden", schema: { type: "boolean" } },
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
expect(validateCatalog(catalog, "1.0").gates.every((g) => g.pass)).toBe(true);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("an instance carries the accessible name in @accessibility, and the old property name is refused", () => {
|
|
154
|
+
const instance = (props: Record<string, unknown>) => ({ messages: [{ updateComponents: { components: [{ id: "spin", component: "Spinner", ...props }] } }] });
|
|
155
|
+
const gate = (props: Record<string, unknown>) => validateCatalog(catalog, "1.0", instance(props)).gates.find((g) => g.name === "instance")!.pass;
|
|
156
|
+
expect(gate({ "@accessibility": { label: "Loading results", hidden: false } })).toBe(true);
|
|
157
|
+
expect(gate({ "aria-label": "Loading results" })).toBe(false);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("the mapping covers exactly the four AccessibilityAttributes of common_types.json", () => {
|
|
161
|
+
const attributes = Object.keys(((commonTypesV1_0.$defs as Record<string, Json>).AccessibilityAttributes.properties ?? {}) as object).sort();
|
|
162
|
+
expect([...new Set(Object.values(ARIA_TO_ACCESSIBILITY))].sort()).toEqual(attributes);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("the 1.0 report says where each ARIA property goes, and nothing points at surfaceProperties", () => {
|
|
166
|
+
const entries = fidelityV1_0(mapping);
|
|
167
|
+
expect(entries.find((e) => e.source === "components.spinner.props.aria-label")?.target).toBe("components.Spinner instance @accessibility.label");
|
|
168
|
+
expect(entries.some((e) => /surfaceProperties|theme\./.test(e.target))).toBe(false);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe("the 1.0 emitter refuses what 1.0 cannot represent", () => {
|
|
173
|
+
const base = () => transformFromJson(shadcnDoc, { a2uiVersion: "0.9.1" }).mapping;
|
|
174
|
+
const withComponent = (schema: Json): MappingResult => {
|
|
175
|
+
const m = structuredClone(base());
|
|
176
|
+
m.components.Probe = schema;
|
|
177
|
+
m.componentOrder = [...m.componentOrder, "Probe"];
|
|
178
|
+
return m;
|
|
179
|
+
};
|
|
180
|
+
const emit = (m: MappingResult) => () => emitCatalogV1_0(m, shadcnProfile, "https://example.test/v1_0/catalog.json");
|
|
181
|
+
|
|
182
|
+
it("a property name that is not an identifier and has no mapping", () => {
|
|
183
|
+
expect(emit(withComponent({ type: "object", allOf: [{ $ref: "#/$defs/ComponentCommon" }, { type: "object", properties: { component: { const: "Probe" }, "data-x": { type: "string" } } }] })))
|
|
184
|
+
.toThrow(/Probe\.data-x: not a UAX #31 identifier/);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("a required ARIA property, which the optional @accessibility cannot carry", () => {
|
|
188
|
+
expect(emit(withComponent({ type: "object", allOf: [{ type: "object", properties: { component: { const: "Probe" }, "aria-label": { type: "string" } }, required: ["component", "aria-label"] }] })))
|
|
189
|
+
.toThrow(/required ARIA property/);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("a reference to a definition common_types.json does not offer", () => {
|
|
193
|
+
expect(emit(withComponent({ type: "object", allOf: [{ type: "object", properties: { component: { const: "Probe" }, x: { $ref: "#/$defs/Theme" } } }] })))
|
|
194
|
+
.toThrow(/#\/\$defs\/Theme' has no A2UI 1.0 counterpart/);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("the reserved component name Surface", () => {
|
|
198
|
+
const m = structuredClone(base());
|
|
199
|
+
m.components.Surface = m.components[m.componentOrder[0]!]!;
|
|
200
|
+
m.componentOrder = [...m.componentOrder, "Surface"];
|
|
201
|
+
expect(emit(m)).toThrow(/reserved/);
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe("gate A2 for 1.0 is the official catalog definition: controls", () => {
|
|
206
|
+
const good = () => structuredClone(catalogs["shadcn/ui"]!()) as Json;
|
|
207
|
+
const shape = (c: Json) => validateCatalog(c as unknown as A2uiCatalog, "1.0").gates.find((g) => g.name === "catalog-shape")!;
|
|
208
|
+
|
|
209
|
+
it("positive control: upstream's own pinned 1.0 basic catalog passes", () => {
|
|
210
|
+
const basic = read("../fixtures/a2ui/basic-catalog.v1_0.json") as Json;
|
|
211
|
+
// dspack's invariants require a description; upstream's basic catalog has one.
|
|
212
|
+
expect(shape(basic).pass, JSON.stringify(shape(basic).errors)).toBe(true);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
const negatives: Record<string, (c: Json) => void> = {
|
|
216
|
+
"anyFunction missing": (c) => delete (c.$defs as Json).anyFunction,
|
|
217
|
+
"surfaceProperties present": (c) => ((c.$defs as Json).surfaceProperties = { type: "object" }),
|
|
218
|
+
"a component wrapped in ComponentCommon": (c) => {
|
|
219
|
+
const comps = c.components as Record<string, Json>;
|
|
220
|
+
comps.Badge = { type: "object", allOf: [{ $ref: "common_types.json#/$defs/ComponentCommon" }, comps.Badge] };
|
|
221
|
+
},
|
|
222
|
+
"protocolVersion missing": (c) => delete c.protocolVersion,
|
|
223
|
+
"a function in the 0.9 shape": (c) => {
|
|
224
|
+
c.functions = { f: { description: "x", returnType: "boolean" } };
|
|
225
|
+
},
|
|
226
|
+
"an unknown root key": (c) => (c.theme = {}),
|
|
227
|
+
};
|
|
228
|
+
for (const [label, mutate] of Object.entries(negatives)) {
|
|
229
|
+
it(`negative control: ${label} fails`, () => {
|
|
230
|
+
const c = good();
|
|
231
|
+
mutate(c);
|
|
232
|
+
expect(shape(c).pass).toBe(false);
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
describe("vendored 1.0 schema mirrors", () => {
|
|
238
|
+
it("the catalog definition mirror equals fixtures/a2ui/catalog_definition.v1_0.json", () => {
|
|
239
|
+
expect(catalogDefinitionV1_0).toStrictEqual(read("../fixtures/a2ui/catalog_definition.v1_0.json"));
|
|
240
|
+
});
|
|
241
|
+
it("the common_types mirror equals fixtures/a2ui/common_types.v1_0.json", () => {
|
|
242
|
+
expect(commonTypesV1_0).toStrictEqual(read("../fixtures/a2ui/common_types.v1_0.json"));
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
describe("catalogCompanionSchemas: what a consumer registers beside a catalog it compiles itself", () => {
|
|
247
|
+
const compile = (catalog: A2uiCatalog, companions: Json[]) => {
|
|
248
|
+
const ajv = new (Ajv2020 as unknown as new (o: object) => { addSchema(s: Json): void; compile(s: Json): unknown })({ strict: false });
|
|
249
|
+
(addFormats as unknown as (a: unknown) => void)(ajv);
|
|
250
|
+
for (const c of companions) ajv.addSchema(c);
|
|
251
|
+
ajv.addSchema(catalog as unknown as Json);
|
|
252
|
+
// As a consumer does: compile what instances are checked against (ajv resolves references lazily).
|
|
253
|
+
return () => ajv.compile({ $ref: `${catalog.$id}#/$defs/anyComponent` } as Json);
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
it("a 0.9.1 catalog is self-contained: no companions", () => {
|
|
257
|
+
expect(catalogCompanionSchemas(transformFromJson(shadcnDoc, { a2uiVersion: "0.9.1" }).catalog)).toEqual([]);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it("a 1.0 catalog needs the pinned common_types beside it, and compiles with it but not without it", () => {
|
|
261
|
+
const catalog = catalogs["shadcn/ui"]!();
|
|
262
|
+
const companions = catalogCompanionSchemas(catalog);
|
|
263
|
+
expect(companions.map((c) => c.$id)).toEqual([new URL("common_types.json", catalog.$id).href]);
|
|
264
|
+
expect(compile(catalog, [])).toThrow(/common_types\.json/);
|
|
265
|
+
expect(compile(catalog, companions)).not.toThrow();
|
|
266
|
+
});
|
|
267
|
+
});
|
package/src/validate/ajv.ts
CHANGED
|
@@ -12,11 +12,16 @@
|
|
|
12
12
|
* a2ui-catalog.meta.<ver>.json (the literal "catalog schema" check; this is what
|
|
13
13
|
* makes v0.9.1 vs v1.0 conformance distinct — theme vs surfaceProperties).
|
|
14
14
|
* 3. instance: every component instance in the hand-authored surface validates
|
|
15
|
-
* against the catalog's own #/$defs/anyComponent
|
|
15
|
+
* against the catalog's own #/$defs/anyComponent — checked by manual
|
|
16
|
+
* discrimination (each instance against its own #/components/<name>
|
|
17
|
+
* branch), which is semantically equivalent and keeps every reported
|
|
18
|
+
* error inside the instance's own branch. See the gate 3 body.
|
|
16
19
|
*/
|
|
17
20
|
import Ajv2020 from "ajv/dist/2020.js";
|
|
18
21
|
import addFormats from "ajv-formats";
|
|
19
22
|
import { catalogMetaSchemas } from "./meta/catalog-meta.js";
|
|
23
|
+
import { catalogDefinitionV1_0, commonTypesV1_0 } from "./meta/a2ui-v1_0.js";
|
|
24
|
+
import { COMMON_TYPES_V1_0 } from "../transform/emit-v1_0.js";
|
|
20
25
|
import type { A2uiCatalog, A2uiVersion, Json } from "../types.js";
|
|
21
26
|
|
|
22
27
|
export interface GateResult {
|
|
@@ -24,6 +29,25 @@ export interface GateResult {
|
|
|
24
29
|
pass: boolean;
|
|
25
30
|
detail: string;
|
|
26
31
|
errors?: string[];
|
|
32
|
+
/**
|
|
33
|
+
* Structured per-instance evidence for gate 3 failures (advanced channel
|
|
34
|
+
* for downstream UIs). The strings in `errors` remain the primary
|
|
35
|
+
* user-facing form; this preserves ajv's keyword/params/schemaPath, which
|
|
36
|
+
* the strings deliberately compress.
|
|
37
|
+
*/
|
|
38
|
+
errorDetails?: Array<{
|
|
39
|
+
/** `component#id` — same prefix the string form carries. */
|
|
40
|
+
instance: string;
|
|
41
|
+
component: string;
|
|
42
|
+
id: string;
|
|
43
|
+
errors: Array<{
|
|
44
|
+
instancePath?: string;
|
|
45
|
+
schemaPath?: string;
|
|
46
|
+
keyword?: string;
|
|
47
|
+
params?: unknown;
|
|
48
|
+
message?: string;
|
|
49
|
+
}>;
|
|
50
|
+
}>;
|
|
27
51
|
}
|
|
28
52
|
|
|
29
53
|
export interface ValidationReport {
|
|
@@ -51,6 +75,35 @@ function externalRefs(node: unknown, acc: string[] = []): string[] {
|
|
|
51
75
|
return acc;
|
|
52
76
|
}
|
|
53
77
|
|
|
78
|
+
/**
|
|
79
|
+
* A2UI 1.0: the one external reference a catalog may make is to a listed standard type in common_types.json, written
|
|
80
|
+
* relative (`common_types.json#/$defs/<Type>`); anything else makes the catalog invalid (specification rule 3).
|
|
81
|
+
*/
|
|
82
|
+
const COMMON_REF = /^common_types\.json#\/\$defs\/([A-Za-z]+)$/;
|
|
83
|
+
function allowedV1_0Ref(ref: string): boolean {
|
|
84
|
+
const m = COMMON_REF.exec(ref);
|
|
85
|
+
return !!m && (COMMON_TYPES_V1_0 as readonly string[]).includes(m[1]!);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The vendored 1.0 common_types, registered where a catalog's relative `common_types.json` reference resolves (beside
|
|
90
|
+
* the catalog's own $id). Its `catalog.json#/$defs/anyFunction` then resolves to that same catalog, offline: the
|
|
91
|
+
* specification forbids fetching either over the network.
|
|
92
|
+
*/
|
|
93
|
+
function commonTypesBeside(catalogId: string): Json {
|
|
94
|
+
return { ...(structuredClone(commonTypesV1_0) as Json), $id: new URL("common_types.json", catalogId).href };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The schemas a consumer must register beside a catalog before compiling it with its own JSON Schema validator.
|
|
99
|
+
* A 0.9.1 catalog is self-contained: none. A 1.0 catalog references the standard types as `common_types.json#/$defs/…`
|
|
100
|
+
* (specification rule 3), so it needs the pinned common_types at the URL that reference resolves to; the specification
|
|
101
|
+
* forbids fetching it. For example, with ajv: `for (const s of catalogCompanionSchemas(catalog)) ajv.addSchema(s)`.
|
|
102
|
+
*/
|
|
103
|
+
export function catalogCompanionSchemas(catalog: A2uiCatalog): Json[] {
|
|
104
|
+
return catalog.protocolVersion === "1.0" ? [commonTypesBeside(catalog.$id)] : [];
|
|
105
|
+
}
|
|
106
|
+
|
|
54
107
|
/**
|
|
55
108
|
* Collect every INTERNAL `$ref` that does not resolve inside the catalog
|
|
56
109
|
* document, with the JSON path of the offending `$ref` keyword.
|
|
@@ -119,13 +172,17 @@ export function validateCatalog(
|
|
|
119
172
|
): ValidationReport {
|
|
120
173
|
const gates: GateResult[] = [];
|
|
121
174
|
|
|
122
|
-
// Gate 1 — schema compile + no external $ref + every internal $ref resolves.
|
|
123
|
-
|
|
175
|
+
// Gate 1 — schema compile + no external $ref + every internal $ref resolves. In 1.0 the listed standard types in
|
|
176
|
+
// common_types.json are the one permitted external target, resolved against the vendored copy.
|
|
177
|
+
const v1 = version === "1.0";
|
|
178
|
+
const exts = externalRefs(catalog).filter((r) => !(v1 && allowedV1_0Ref(r)));
|
|
124
179
|
const dangling = danglingInternalRefs(catalog);
|
|
125
180
|
let compiled = false;
|
|
126
181
|
let compileErr = "";
|
|
127
182
|
try {
|
|
128
|
-
|
|
183
|
+
const ajv = newAjv();
|
|
184
|
+
if (v1) ajv.addSchema(commonTypesBeside(catalog.$id));
|
|
185
|
+
ajv.compile(catalog as unknown as Json);
|
|
129
186
|
compiled = true;
|
|
130
187
|
} catch (e) {
|
|
131
188
|
compileErr = e instanceof Error ? e.message : String(e);
|
|
@@ -135,7 +192,9 @@ export function validateCatalog(
|
|
|
135
192
|
name: "schema-compile + no-external-ref",
|
|
136
193
|
pass: selfContained,
|
|
137
194
|
detail: selfContained
|
|
138
|
-
?
|
|
195
|
+
? v1
|
|
196
|
+
? "Catalog compiles as a draft-2020-12 JSON Schema; every $ref is internal or a listed common_types.json type, and resolves."
|
|
197
|
+
: "Catalog compiles as a draft-2020-12 JSON Schema; every $ref is internal and resolves."
|
|
139
198
|
: !compiled
|
|
140
199
|
? `ajv failed to compile the catalog as a schema: ${compileErr}`
|
|
141
200
|
: exts.length
|
|
@@ -144,17 +203,32 @@ export function validateCatalog(
|
|
|
144
203
|
errors: exts.length ? [...new Set(exts)] : dangling.length ? dangling : undefined,
|
|
145
204
|
});
|
|
146
205
|
|
|
147
|
-
// Gate 2 — catalog shape.
|
|
206
|
+
// Gate 2 — catalog shape. For 1.0 that is the official catalog definition (vendored, pinned) as well as dspack's own
|
|
207
|
+
// invariants; for 0.9.1, the derived meta-schema as before.
|
|
148
208
|
const meta = catalogMetaSchemas[version];
|
|
149
209
|
const validateShape = newAjv().compile(meta);
|
|
150
|
-
|
|
210
|
+
let shapeOk = validateShape(catalog) as boolean;
|
|
211
|
+
const shapeErrors = shapeOk ? [] : (validateShape.errors ?? []).map(fmtErr);
|
|
212
|
+
if (v1) {
|
|
213
|
+
const official = newAjv();
|
|
214
|
+
official.addSchema(commonTypesV1_0);
|
|
215
|
+
const validateOfficial = official.compile(catalogDefinitionV1_0);
|
|
216
|
+
if (!validateOfficial(catalog)) {
|
|
217
|
+
shapeOk = false;
|
|
218
|
+
shapeErrors.push(...(validateOfficial.errors ?? []).map((e) => `catalog_definition: ${fmtErr(e)}`));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
151
221
|
gates.push({
|
|
152
222
|
name: "catalog-shape",
|
|
153
223
|
pass: shapeOk,
|
|
154
224
|
detail: shapeOk
|
|
155
|
-
?
|
|
156
|
-
|
|
157
|
-
|
|
225
|
+
? v1
|
|
226
|
+
? "Catalog satisfies the official A2UI v1.0 catalog definition and dspack's v1.0 invariants."
|
|
227
|
+
: `Catalog satisfies the A2UI v${version} catalog-shape meta-schema.`
|
|
228
|
+
: v1
|
|
229
|
+
? "Catalog violates the official A2UI v1.0 catalog definition or dspack's v1.0 invariants."
|
|
230
|
+
: `Catalog violates the v${version} catalog-shape meta-schema.`,
|
|
231
|
+
errors: shapeOk ? undefined : shapeErrors,
|
|
158
232
|
});
|
|
159
233
|
|
|
160
234
|
// Gate 3 — instances (only if the catalog is usable and a surface was supplied).
|
|
@@ -171,20 +245,73 @@ export function validateCatalog(
|
|
|
171
245
|
// Belt and braces: gate 1 has already proven every ref resolves, but a
|
|
172
246
|
// raw ajv throw must never escape this function regardless.
|
|
173
247
|
const failures: string[] = [];
|
|
248
|
+
const errorDetails: NonNullable<GateResult["errorDetails"]> = [];
|
|
174
249
|
let instances: Json[] = [];
|
|
175
250
|
try {
|
|
176
251
|
const ajv = newAjv();
|
|
252
|
+
const commonTypes = v1 ? commonTypesBeside(catalog.$id) : undefined;
|
|
253
|
+
if (commonTypes) ajv.addSchema(commonTypes);
|
|
177
254
|
ajv.addSchema(catalog as unknown as Json, catalog.$id);
|
|
178
|
-
const validateAny = ajv.getSchema(`${catalog.$id}#/$defs/anyComponent`);
|
|
179
255
|
instances = extractInstances(surface);
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
256
|
+
// Manual discrimination instead of the flat `#/$defs/anyComponent`
|
|
257
|
+
// oneOf. Semantically equivalent: every branch of the emitted
|
|
258
|
+
// anyComponent is `{$ref: "#/components/<name>"}` and each component
|
|
259
|
+
// schema pins `component: {const: <name>}` inside its allOf (and
|
|
260
|
+
// requires it), so an instance tagged component X can only ever match
|
|
261
|
+
// branch X — validating the instance against ITS OWN branch accepts
|
|
262
|
+
// and rejects exactly the same instances the oneOf does. What changes
|
|
263
|
+
// is the report: allErrors WITHIN the matching branch (rigor
|
|
264
|
+
// preserved, every genuine error surfaced) without echoing every
|
|
265
|
+
// other branch's const/required/unevaluatedProperties noise.
|
|
266
|
+
const admitted = Object.keys(catalog.components ?? {});
|
|
267
|
+
for (const inst of instances) {
|
|
268
|
+
const comp = String(inst.component);
|
|
269
|
+
const id = String(inst.id);
|
|
270
|
+
const where = `${comp}#${id}`;
|
|
271
|
+
if (!admitted.includes(comp)) {
|
|
272
|
+
failures.push(
|
|
273
|
+
`${where}: component '${comp}' is not in this catalog (${admitted.length} components admitted)`,
|
|
274
|
+
);
|
|
275
|
+
errorDetails.push({
|
|
276
|
+
instance: where,
|
|
277
|
+
component: comp,
|
|
278
|
+
id,
|
|
279
|
+
errors: [
|
|
280
|
+
{
|
|
281
|
+
message: `component '${comp}' is not in this catalog`,
|
|
282
|
+
params: { admittedComponents: admitted },
|
|
283
|
+
},
|
|
284
|
+
],
|
|
285
|
+
});
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
// A 1.0 component is validated the way the envelope composes it (agent_to_renderer.json `Component`):
|
|
289
|
+
// ComponentCommon plus the catalog component, with no other property. A 0.9.1 component carries its own.
|
|
290
|
+
const validateBranch = commonTypes
|
|
291
|
+
? ajv.compile({
|
|
292
|
+
allOf: [{ $ref: `${commonTypes.$id}#/$defs/ComponentCommon` }, { $ref: `${catalog.$id}#/components/${comp}` }],
|
|
293
|
+
unevaluatedProperties: false,
|
|
294
|
+
})
|
|
295
|
+
: ajv.getSchema(`${catalog.$id}#/components/${comp}`);
|
|
296
|
+
if (!validateBranch) {
|
|
297
|
+
failures.push(`${where}: could not resolve #/components/${comp} from the catalog.`);
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
if (!validateBranch(inst)) {
|
|
301
|
+
const errs = validateBranch.errors ?? [];
|
|
302
|
+
for (const e of errs) failures.push(`${where}: ${fmtErr(e)}`);
|
|
303
|
+
errorDetails.push({
|
|
304
|
+
instance: where,
|
|
305
|
+
component: comp,
|
|
306
|
+
id,
|
|
307
|
+
errors: errs.map((e) => ({
|
|
308
|
+
instancePath: e.instancePath,
|
|
309
|
+
schemaPath: e.schemaPath,
|
|
310
|
+
keyword: e.keyword,
|
|
311
|
+
params: e.params as unknown,
|
|
312
|
+
message: e.message,
|
|
313
|
+
})),
|
|
314
|
+
});
|
|
188
315
|
}
|
|
189
316
|
}
|
|
190
317
|
} catch (e) {
|
|
@@ -195,9 +322,12 @@ export function validateCatalog(
|
|
|
195
322
|
pass: failures.length === 0,
|
|
196
323
|
detail:
|
|
197
324
|
failures.length === 0
|
|
198
|
-
?
|
|
325
|
+
? version === "1.0"
|
|
326
|
+
? `All ${instances.length} surface component instance(s) validate as the 1.0 envelope composes them: common_types.json ComponentCommon with the catalog component, closed.`
|
|
327
|
+
: `All ${instances.length} surface component instance(s) validate against #/$defs/anyComponent.`
|
|
199
328
|
: `${failures.length} instance validation error(s).`,
|
|
200
329
|
errors: failures.length ? failures : undefined,
|
|
330
|
+
...(errorDetails.length ? { errorDetails } : {}),
|
|
201
331
|
});
|
|
202
332
|
}
|
|
203
333
|
}
|
|
@@ -205,6 +335,23 @@ export function validateCatalog(
|
|
|
205
335
|
return { version, pass: gates.every((g) => g.pass), gates };
|
|
206
336
|
}
|
|
207
337
|
|
|
208
|
-
|
|
209
|
-
|
|
338
|
+
/**
|
|
339
|
+
* One ajv error as a user-facing line: instancePath + message, enriched from
|
|
340
|
+
* the error's params where they carry the actionable detail ajv's message
|
|
341
|
+
* omits — the allowed enum values, the expected const, or the name of the
|
|
342
|
+
* offending unevaluated/additional property.
|
|
343
|
+
*/
|
|
344
|
+
function fmtErr(e: { instancePath?: string; keyword?: string; params?: unknown; message?: string }): string {
|
|
345
|
+
const params = (e.params ?? {}) as Record<string, unknown>;
|
|
346
|
+
let out = `${e.instancePath || "(root)"} ${e.message ?? ""}`.trim();
|
|
347
|
+
if (e.keyword === "enum" && Array.isArray(params.allowedValues)) {
|
|
348
|
+
out += ` (allowed: ${(params.allowedValues as unknown[]).map((v) => String(v)).join(", ")})`;
|
|
349
|
+
} else if (e.keyword === "const" && "allowedValue" in params) {
|
|
350
|
+
out += ` (expected: ${JSON.stringify(params.allowedValue)})`;
|
|
351
|
+
} else if (e.keyword === "unevaluatedProperties" && typeof params.unevaluatedProperty === "string") {
|
|
352
|
+
out += ` ('${params.unevaluatedProperty}')`;
|
|
353
|
+
} else if (e.keyword === "additionalProperties" && typeof params.additionalProperty === "string") {
|
|
354
|
+
out += ` ('${params.additionalProperty}')`;
|
|
355
|
+
}
|
|
356
|
+
return out;
|
|
210
357
|
}
|
|
@@ -2,32 +2,86 @@
|
|
|
2
2
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
3
|
"$id": "https://rdombrowski.dev/meta/a2ui-catalog.v1_0.json",
|
|
4
4
|
"title": "A2UI catalog shape (v1.0)",
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "dspack's own invariants for an A2UI v1.0 catalog, checked alongside the official catalog definition (fixtures/a2ui/catalog_definition.v1_0.json, pinned in fixtures/a2ui/PIN.md): it declares protocolVersion 1.0, carries neither the 0.9 theme nor the pre-release surfaceProperties, and never wraps a component in ComponentCommon, which the envelope composes.",
|
|
6
6
|
"type": "object",
|
|
7
|
-
"required": [
|
|
7
|
+
"required": [
|
|
8
|
+
"$schema",
|
|
9
|
+
"$id",
|
|
10
|
+
"protocolVersion",
|
|
11
|
+
"catalogId",
|
|
12
|
+
"title",
|
|
13
|
+
"description",
|
|
14
|
+
"components",
|
|
15
|
+
"$defs"
|
|
16
|
+
],
|
|
8
17
|
"properties": {
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
18
|
+
"protocolVersion": {
|
|
19
|
+
"const": "1.0"
|
|
20
|
+
},
|
|
21
|
+
"catalogId": {
|
|
22
|
+
"type": "string",
|
|
23
|
+
"format": "uri"
|
|
24
|
+
},
|
|
25
|
+
"$id": {
|
|
26
|
+
"type": "string",
|
|
27
|
+
"format": "uri"
|
|
28
|
+
},
|
|
29
|
+
"instructions": {
|
|
30
|
+
"type": "string"
|
|
31
|
+
},
|
|
12
32
|
"components": {
|
|
13
33
|
"type": "object",
|
|
14
34
|
"minProperties": 1,
|
|
15
35
|
"additionalProperties": {
|
|
16
36
|
"type": "object",
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
37
|
+
"not": {
|
|
38
|
+
"required": [
|
|
39
|
+
"allOf"
|
|
40
|
+
],
|
|
41
|
+
"properties": {
|
|
42
|
+
"allOf": {
|
|
43
|
+
"contains": {
|
|
44
|
+
"required": [
|
|
45
|
+
"$ref"
|
|
46
|
+
],
|
|
47
|
+
"properties": {
|
|
48
|
+
"$ref": {
|
|
49
|
+
"pattern": "ComponentCommon$"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
21
55
|
}
|
|
22
56
|
}
|
|
23
57
|
},
|
|
24
58
|
"$defs": {
|
|
25
59
|
"type": "object",
|
|
26
|
-
"required": [
|
|
27
|
-
|
|
60
|
+
"required": [
|
|
61
|
+
"anyComponent",
|
|
62
|
+
"anyFunction"
|
|
63
|
+
],
|
|
64
|
+
"not": {
|
|
65
|
+
"anyOf": [
|
|
66
|
+
{
|
|
67
|
+
"required": [
|
|
68
|
+
"theme"
|
|
69
|
+
]
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
"required": [
|
|
73
|
+
"surfaceProperties"
|
|
74
|
+
]
|
|
75
|
+
}
|
|
76
|
+
]
|
|
77
|
+
},
|
|
28
78
|
"properties": {
|
|
29
|
-
"anyComponent": {
|
|
30
|
-
|
|
79
|
+
"anyComponent": {
|
|
80
|
+
"type": "object",
|
|
81
|
+
"required": [
|
|
82
|
+
"oneOf"
|
|
83
|
+
]
|
|
84
|
+
}
|
|
31
85
|
}
|
|
32
86
|
}
|
|
33
87
|
}
|