7c4d3ed79f
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
25 lines
1.1 KiB
TypeScript
25 lines
1.1 KiB
TypeScript
/**
|
|
* Schema-conformance helper (F5 SLICE-1, PUC-5): every serialized artifact in
|
|
* the unit suite must validate against the published cross-rung schema —
|
|
* contract drift fails CI (spec §7.4).
|
|
*/
|
|
import { readFileSync } from "node:fs";
|
|
import Ajv from "ajv/dist/2020";
|
|
import { serializeArtifact, type Artifact } from "../../src/model";
|
|
|
|
const schemaUrl = new URL("../../schemas/coauthoring-sidecar.schema.json", import.meta.url);
|
|
const schema = JSON.parse(readFileSync(schemaUrl, "utf8"));
|
|
const ajv = new Ajv({ allErrors: true });
|
|
const validate = ajv.compile(schema);
|
|
|
|
export function validateSidecar(data: unknown): { valid: boolean; errors: string[] } {
|
|
const valid = validate(data) as boolean;
|
|
const errors = (validate.errors ?? []).map((e) => `${e.instancePath || "/"} ${e.message}`);
|
|
return { valid, errors };
|
|
}
|
|
|
|
export function expectValidSidecar(artifact: Artifact): void {
|
|
const { valid, errors } = validateSidecar(JSON.parse(serializeArtifact(artifact)));
|
|
if (!valid) throw new Error(`sidecar failed schema validation:\n${errors.join("\n")}`);
|
|
}
|