- schemas/: JSON Schema (draft 2020-12) generated from the zod schemas by npm run build (scripts/json-schemas.cjs), exported as @crosscode/contracts/schemas/* - Test: files match the zod schemas, every sample passes them (Ajv) - Version 1.0.0, CHANGELOG and README Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
83 lines
3.4 KiB
TypeScript
83 lines
3.4 KiB
TypeScript
import { readdirSync, readFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import Ajv2020 from "ajv/dist/2020";
|
|
import addFormats from "ajv-formats";
|
|
import * as contracts from "../src";
|
|
import { SAMPLES, sample } from "./helpers";
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
const { buildSchemas, serialise } = require("../scripts/json-schemas.cjs") as {
|
|
buildSchemas: (c: typeof contracts) => Record<string, object>;
|
|
serialise: (json: object) => string;
|
|
};
|
|
|
|
const SCHEMAS_DIR = join(__dirname, "..", "schemas");
|
|
const schemas = buildSchemas(contracts);
|
|
|
|
/** Every sample file and the JSON Schema (file name without .json) it must pass. */
|
|
const SAMPLE_SCHEMA: Record<string, string> = {
|
|
"event.json": "event",
|
|
"event-simple.json": "event",
|
|
"event-feed.json": "event-feed",
|
|
"event-changed.json": "event-changed",
|
|
"church.json": "church",
|
|
"identity-claims.json": "identity-claims",
|
|
"user-updated.json": "user-updated",
|
|
"user-deleted.json": "user-deleted",
|
|
"user-deleted-reply.json": "user-deleted-reply",
|
|
"user-export.json": "user-export",
|
|
"user-export-reply.json": "user-export-reply",
|
|
"hub-register-request.json": "hub-register-request",
|
|
"hub-register-response.json": "hub-register-response",
|
|
"hub-heartbeat-events.json": "hub-heartbeat-request",
|
|
"hub-heartbeat-website.json": "hub-heartbeat-request",
|
|
"hub-heartbeat-app.json": "hub-heartbeat-request",
|
|
"hub-heartbeat-accounts.json": "hub-heartbeat-request",
|
|
"hub-heartbeat-response.json": "hub-heartbeat-response",
|
|
"hub-licence-payload.json": "hub-licence-payload",
|
|
};
|
|
|
|
describe("JSON Schema (schemas/)", () => {
|
|
it("the files in schemas/ match the zod schemas (run npm run build after changing a contract)", () => {
|
|
const files = readdirSync(SCHEMAS_DIR).filter((f) => f.endsWith(".json")).sort();
|
|
expect(files).toEqual(Object.keys(schemas).map((n) => `${n}.json`).sort());
|
|
for (const [name, json] of Object.entries(schemas)) {
|
|
expect(readFileSync(join(SCHEMAS_DIR, `${name}.json`), "utf8").replace(/\r\n/g, "\n")).toBe(serialise(json));
|
|
}
|
|
});
|
|
|
|
const ajv = new Ajv2020({ allErrors: true, strict: true });
|
|
addFormats(ajv);
|
|
for (const json of Object.values(schemas)) ajv.addSchema(json);
|
|
const validatorFor = (name: string) => {
|
|
const validate = ajv.getSchema((schemas[name] as { $id: string }).$id);
|
|
if (!validate) throw new Error(`no JSON Schema "${name}"`);
|
|
return validate;
|
|
};
|
|
|
|
it("every JSON Schema compiles", () => {
|
|
for (const name of Object.keys(schemas)) expect(() => validatorFor(name)).not.toThrow();
|
|
});
|
|
|
|
it("every sample has a JSON Schema", () => {
|
|
const files = readdirSync(SAMPLES).filter((f) => f.endsWith(".json"));
|
|
expect(files.filter((f) => !(f in SAMPLE_SCHEMA))).toEqual([]);
|
|
});
|
|
|
|
it.each(Object.entries(SAMPLE_SCHEMA))("%s passes the JSON Schema %s", (file, name) => {
|
|
const validate = validatorFor(name);
|
|
validate(sample(file));
|
|
expect(validate.errors ?? []).toEqual([]);
|
|
});
|
|
|
|
it("refuses a payload with a wrong schema name", () => {
|
|
const validate = validatorFor("event");
|
|
expect(validate({ ...(sample("event.json") as object), schema: "crosscode.event/2" })).toBe(false);
|
|
});
|
|
|
|
it("allows fields it doesn't know (consumers drop them, never refuse them)", () => {
|
|
const validate = validatorFor("event");
|
|
expect(validate({ ...(sample("event.json") as object), somethingNew: true })).toBe(true);
|
|
});
|
|
});
|