contractdiff/testing
Every export of contractdiff/testing, with its signature and doc comment: ConformanceError, conformance, RULES, PluginError, plus 18 types.
import { ConformanceError, conformance, RULES, … } from 'contractdiff/testing';Classes
ConformanceError
Every rule a plugin broke. Its message is the list, one failure and its fix: line per entry.
class ConformanceError extends Error {
readonly failures: readonly Failure[];
constructor(plugin: string, failures: readonly Failure[]);
}PluginError
A refusal says what is wrong and what to do about it: the family's one vocabulary.
class PluginError extends Error {
readonly code: PluginErrorCode;
readonly fix: string;
constructor(code: PluginErrorCode, message: string, fix: string);
}Constants
conformance
Run the conformance suite on plugin. Resolves when it passes every rule; rejects with a
ConformanceError naming every rule it breaks. A plugin host.validate refuses is reported
under schema alone, since no other rule can run on it.
const conformance: (plugin: unknown, options?: ConformanceOptions) => Promise<void>;RULES
const RULES: readonly ["schema", "sample", "deterministic", "regeneration", "roundtrip", "fix-lines", "paths", "no-io"];Interfaces
Adapter
An input format: a parsed document in, the model out.
interface Adapter {
readonly description: string;
readonly detect: (document: unknown) => boolean;
readonly read: (document: unknown) => Contract;
}ConformanceOptions
interface ConformanceOptions {
/** More documents to run every generator on: each must give the same bytes twice, and safe paths. */
readonly corpus?: readonly unknown[];
/**
* The sample document after a spec change, for the `regeneration` rule. The suite makes this
* itself for an OpenAPI or Swagger sample (one operation and one property more); any other
* format needs it.
*/
readonly mutate?: (document: unknown) => unknown;
/** The extract half, for the `roundtrip` rule: run with its sample's options plus `files`, what the generator wrote. */
readonly extractor?: Extractor;
/** The generator `extractor` pairs with. Every generator of the plugin, when absent. */
readonly generator?: string;
}Extractor
Code → spec (spec.md R-TE2): reads the user's code, named by its options, and returns a document, or { document, findings }.
interface Extractor {
readonly description: string;
readonly options?: Readonly<Record<string, unknown>>;
readonly sample: ExtractorSample;
readonly run: (options: Options) => unknown;
}Failure
One rule broken in one place, and what to do about it.
interface Failure {
readonly rule: Rule;
readonly where: string;
readonly message: string;
readonly fix: string;
}GeneratedFile
interface GeneratedFile {
/** Relative to the output folder, with `/` separators. */
readonly path: string;
/** The body: the writer adds the generated header, so a generator never writes one. */
readonly content: string;
readonly owner: Owner;
/** The symbols this file declares per operation. Optional: a file without them is still mapped by its path. */
readonly symbols?: readonly GeneratedSymbol[];
}Generator
Spec → code: data plus one function (R-PL2).
interface Generator {
readonly description: string;
/** Names of the input formats it accepts, e.g. `openapi`. */
readonly inputs: readonly string[];
/** burgee option declarations, keyed by option name. */
readonly options?: Readonly<Record<string, unknown>>;
readonly sample: Sample;
readonly run: (contract: Contract, options: Options) => readonly GeneratedFile[] | GeneratorResult | Promise<readonly GeneratedFile[] | GeneratorResult>;
}GeneratorResult
What a generator may return when it has something to report besides files.
interface GeneratorResult {
readonly files: readonly GeneratedFile[];
readonly findings: readonly ContractFinding[];
}LintNode
interface LintNode {
/** A JSON Pointer into the source document, or a logical one for an inline payload schema. */
readonly pointer: string;
readonly value: Readonly<Record<string, unknown>>;
/** The operation the node belongs to; absent for the document. */
readonly operation?: Operation;
/** A property's or parameter's name. */
readonly name?: string;
/** What a `path` selected, as it is: a `then` with no `field` reads this, not `value`. */
readonly matched?: unknown;
}LintRule
interface Rule {
readonly description: string;
readonly given: Given;
/** What must hold, by the built-in functions; every entry must. */
readonly then?: Then | readonly Then[];
/** What must hold, when no built-in function says it: the problems on one node, each a message. Pure. */
readonly check?: (node: LintNode, context: LintContext) => readonly string[];
readonly fix: string;
/**
* A document and exactly what the rule finds on it (R4): the rule's own test, which the
* conformance suite runs. Required of a rule a plugin contributes.
*/
readonly sample?: RuleSample;
}Plugin
The keys contractdiff reads. Any object with these fields is a plugin here, whatever else it carries.
interface Plugin {
readonly name: string;
readonly contract?: number;
readonly adapters?: Readonly<Record<string, Adapter>>;
readonly generators?: Readonly<Record<string, Generator>>;
readonly extractors?: Readonly<Record<string, Extractor>>;
/** `lint` rules (apicontract-lint R4): each with the sample that is its test. */
readonly rules?: Readonly<Record<string, Rule>>;
}RuleSample
interface RuleSample {
readonly document: unknown;
readonly findings: readonly SampleFinding[];
}SampleFinding
Where a rule finds a problem on its sample, and what it says.
interface SampleFinding {
readonly pointer: string;
readonly message: string;
}Types
Options
type Options = Readonly<Record<string, unknown>>;Rule
type Rule = (typeof RULES)[number];Re-exported
Documented on the page of the entry point that declares them.
| Export | Kind | Documented in |
|---|---|---|
Contract | interface | contractdiff/core |
ContractFinding | interface | contractdiff/core |
Operation | interface | contractdiff/core |
Payload | interface | contractdiff/core |
contractdiff/openapi-typescript
Every export of contractdiff/openapi-typescript, with its signature and doc comment: openapiTS, astToString, COMMENT_HEADER, createRef, isEnumSchema, makeApiPathsEnum and 12 more, plus 2 types.
FAQ
Short answers about contractdiff: what a breaking change means here, why a decided change still fails, git refs, openapi-typescript compatibility, AsyncAPI and MCP, generated files, validators, agents and exit codes, each linking where to read more.