JuliusBrussee/caveman · error · Error

: missing $id

Error message

${file}: missing $id

What it means

Each schema must carry a non-empty string $id because the script registers all schemas in Ajv by $id and later resolves them via ajv.getSchema($id). A missing, non-string, or empty $id would make the schema unreferenceable, so the script throws naming the file.

Solutions

  1. Add a unique absolute URI $id to the named schema, matching the convention https://caveman.so/schemas/<name>.schema.json
  2. Ensure $id is a JSON string, not a nested object or number
  3. Confirm no two schemas share the same $id (Ajv duplicate-id registration will then fail instead)

Example fix

// before
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object"
}
// after
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://caveman.so/schemas/adapter-conformance.schema.json",
  "type": "object"
}
Defensive patterns

Strategy: validation

Validate before calling

for (const f of files) {
  const s = JSON.parse(await readFile(path.join(schemaRoot, f), 'utf8'));
  if (typeof s.$id !== 'string' || s.$id.length === 0) throw new Error(`${f}: $id must be a non-empty string`);
}
const ids = schemas.map(s => s.$id);
if (new Set(ids).size !== ids.length) throw new Error('duplicate $id values across schemas');

Type guard

const hasId = (s) => typeof s?.$id === 'string' && s.$id.length > 0;

Try / catch

try { loadSchemas(); } catch (e) { if (e.message.includes('missing $id')) { console.error('Add canonical $id to:', e.message); process.exitCode = 1; } else throw e; }

Prevention

When it happens

Trigger: Running scripts/validate-schemas.mjs when a schema file omits $id, sets $id to a non-string (e.g. number or object), or sets it to "".

Common situations: New schema added without following the existing files' $id convention; a tool stripped $id during transformation; $id accidentally renamed to $ref or id.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

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

const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const schemaRoot = path.join(packageRoot, "schemas");
const files = (await readdir(schemaRoot))
  .filter((name) => name.endsWith(".schema.json"))
  .sort();

if (files.length === 0) {
  throw new Error("no JSON schemas found");
}

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}`);

View on GitHub (pinned to 3ee70a1026)