This code is still in BETA. Proceed with caution.
This compares OpenAPI schemas and Pact contracts to determine if they are compatible. It is inspired by swagger-mock-validator and aims to retain a mostly compatible interface.
swagger-mock-validator has aged, and is very difficult to extend and improve on.
It is also very slow primarily due to inefficient use of ajv; schemas are unnecessarily recompiled everytime instead of being cached.
Schemas are compiled once, and reused where possible. In practical terms, this gives us an improvement exceeding 10x in real-world comparisons.
Referenced schemas are NOT inlined. They are kept as references to keep the size of complex schemas down. We need to do this anyway to support circular references. Further, unused references are skipped to speed up schema compilation in AJV.
Multiple pacts can be compared in one invocation to maximise schema reuse. Instead of perforing the comparison per pair of OAS + Pact, we can reuse the compiled OAS schemas across multiple Pacts.
A fast HTTP router is used to match provider routes. Instead of iterating through an array of routes, we use Radix Tree search using find-my-way This allows large providers to be traversed quickly.
Computation is broken up to small units using async generators. This leads to low event loop delays, suitable for high concurrency servers.
With error handling omitted for brevity:
import { Comparator } from "openapi-pact-comparator";
// openapi is object from JSON.parse() or yaml.load()
const comparator = new Comparator(openapi);
await comparator.validate();
// pacts is array of objects the same way
for (const pact of pacts) {
for await (const result of comparator.compare(pact)) {
console.log(result);
}
}