contractdiff
Recipes

Write a plugin

Scaffold a contractdiff generator, extractor, input adapter or lint rule with contractdiff plugin new, prove it with the conformance suite from contractdiff/testing, and load it from contractdiff.config.json.

Every built-in target is a plugin, written against the same Plugin type yours is. A plugin is one plain object that contributes generators (contract to code), extractors (code to contract), adapters (an input format) or lint rules.

Scaffold one

Output of npx contractdiff plugin new my-target
write plugin-my-target.ts
write plugin-my-target.test.ts
write README.md

next: node --test plugin-my-target.test.ts

It wrote three files, all yours: plugin-my-target.ts with a one-operation sample, a plugin-my-target.test.ts that runs the conformance suite, and a README.md. A second run writes nothing and never overwrites a file you changed:

Output of npx contractdiff plugin new my-target
unchanged plugin-my-target.ts
unchanged plugin-my-target.test.ts
unchanged README.md

next: node --test plugin-my-target.test.ts

--kind extractor, --kind adapter and --kind rules scaffold the other kinds; --out <dir> writes elsewhere. --kind rules --from finding.json seeds a lint rule from one finding of contractdiff lint --json, with that finding as its sample.

Prove it

import { test } from "node:test";
import { conformance } from "contractdiff/testing";
import { plugin } from "./plugin-my-target.js";

test("conforms", async () => {
  await conformance(plugin);
});

conformance(plugin) throws a ConformanceError naming every rule the plugin breaks, each with a fix: line. It runs under node:test, Vitest and Jest alike.

rulewhat it asks
schemathe plugin validates, and contributes a generator, an extractor, an adapter or a rule
samplerun on sample.document returns exactly sample.files
deterministictwo runs give the same bytes
regenerationafter a contract change, every owner: 'user' file is byte for byte what it was
roundtripwith { extractor }: extracting what the generator wrote gives back the sample contract
fix-linesevery refusal and every finding says what to do
pathsevery path stays inside the output folder, even for an operation id with ../ in it
no-ioa rule reads only the node and the contract it is given

Load it

The files are ES modules; keep them in a package with "type": "module". Name the plugin in the config, by package name or relative path (nothing else is loaded), and use its generator in a target:

{
  "input": "openapi.yaml",
  "plugins": ["./plugins/plugin-my-target.js"],
  "targets": { "ids": { "generator": "my-target", "out": "src/generated" } }
}

The types (Plugin, Contract, GeneratedFile) and the conformance suite come from contractdiff/testing; the contract model the plugin receives is contractdiff/core. Both are in the API reference.

On this page