JuliusBrussee/caveman · error · Error

schema failed to compile

Error message

schema failed to compile: ${schema.$id}

What it means

After adding all schemas, the script verifies that each $id resolves to a compiled validation function via ajv.getSchema. Ajv compiles lazily and in strict mode can silently skip/defer broken schemas; if the $id is missing from the compiled set, the script throws with the schema's $id. This catches strict-mode compile rejections or registration mismatches.

Solutions

  1. Temporarily call ajv.compile or inspect Ajv's emitted warnings to see the strict-mode error for that $id
  2. Fix the schema's unsupported keyword, $ref target, or unknown format that blocks compilation
  3. Align $ref targets with exact $id strings of the other registered schemas
  4. As a last resort add a registered format or strict:false only with justification — prefer fixing the schema

Example fix

// before
{ "$id": "https://caveman.so/schemas/span.schema.json", "properties": { "startedAt": { "type": "string", "format": "date-time-x" } } }
// after
{ "$id": "https://caveman.so/schemas/span.schema.json", "properties": { "startedAt": { "type": "string", "format": "date-time" } } }
Defensive patterns

Strategy: try-catch

Validate before calling

try { ajv.addSchema(schemas); } catch (e) { console.error('Ajv compile failure:', e.message); process.exitCode = 1; }
// or proactively:
for (const s of schemas) ajv.compile(s); // throws with the strict-mode reason

Try / catch

try { for (const s of schemas) if (!ajv.getSchema(s.$id)) throw new Error(`schema failed to compile: ${s.$id}`); } catch (e) { console.error('Strict-mode compile rejected a schema:', e.message); process.exitCode = 1; }

Prevention

When it happens

Trigger: Running scripts/validate-schemas.mjs when a schema referenced by $id fails AJv strict-mode compilation (e.g. unsupported keywords like $recursiveAnchor misuse, unknown formats without strict:false, invalid $ref targets) so ajv.getSchema($id) returns undefined.

Common situations: A schema uses a non-standard keyword or an unregistered format string and strict mode rejects it; a schema $refs another schema by an $id that doesn't match exactly (case, trailing slash); duplicate $ids causing registration to drop a schema.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/cbe40c62738d862c. Report an issue: GitHub.

Appendix: source

Thrown at packages/shared/contracts/scripts/validate-schemas.mjs:32

}

const schemas = [];
for (const file of files) {
  const schema = JSON.parse(await readFile(path.join(schemaRoot, file), "utf8"));
  if (schema.$schema !== "https://json-schema.org/draft/2020-12/schema") {
    throw new Error(`${file}: unsupported or missing $schema`);
  }
  if (typeof schema.$id !== "string" || schema.$id.length === 0) {
    throw new Error(`${file}: missing $id`);
  }
  schemas.push(schema);
}

const ajv = new Ajv2020({ allErrors: true, strict: true });
ajv.addSchema(schemas);
for (const schema of schemas) {
  const validate = ajv.getSchema(schema.$id);
  if (!validate) throw new Error(`schema failed to compile: ${schema.$id}`);
}

const agentFixtureRoot = path.join(packageRoot, "fixtures", "agent");
const fixtureFiles = (await readdir(agentFixtureRoot))
  .filter((name) => name.endsWith(".json"))
  .sort();

if (fixtureFiles.length !== 2) {
  throw new Error(`expected exactly 2 agent conformance fixtures, found ${fixtureFiles.length}`);
}

const fixtures = await Promise.all(
  fixtureFiles.map(async (file) => JSON.parse(await readFile(path.join(agentFixtureRoot, file), "utf8"))),
);
const validateAdapterConformance = ajv.getSchema(
  "https://caveman.so/schemas/adapter-conformance.schema.json",
);
if (!validateAdapterConformance) {

View on GitHub (pinned to 3ee70a1026)