contractdiff 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -9
- package/dist/diff-normalise.js +1 -1
- package/dist/lint-functions.js +2 -2
- package/dist/plugin-extract-openapi.js +2 -2
- package/dist/plugin-extract-standard-schema.js +2 -2
- package/dist/plugin-validator-code.js +3 -3
- package/dist/props-generate.js +2 -1
- package/dist/props-judge.js +42 -11
- package/dist/props-run.js +11 -5
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
<a href="https://app.codecov.io/gh/ofri-peretz/burgee/components"><img src="https://img.shields.io/codecov/c/github/ofri-peretz/burgee/main?component=contractdiff&style=flat-square" alt="contractdiff line coverage: its Codecov component" /></a>
|
|
19
19
|
<a href="https://scorecard.dev/viewer/?uri=github.com/ofri-peretz/burgee"><img src="https://img.shields.io/ossf-scorecard/github.com/ofri-peretz/burgee?style=flat-square&label=OpenSSF%20Scorecard" alt="OpenSSF Scorecard for the repository" /></a>
|
|
20
20
|
<a href="https://www.npmjs.com/package/contractdiff?activeTab=code"><img src="https://img.shields.io/npm/unpacked-size/contractdiff?style=flat-square" alt="Unpacked size of the latest contractdiff release on npm" /></a>
|
|
21
|
-
<a href="https://github.com/ofri-peretz/burgee/blob/main/packages/contractdiff/package.json"><img src="https://img.shields.io/badge/dependencies-4%20in%20family%2C%200%20outside-0a6b47?style=flat-square" alt="Four dependencies, all in the burgee family (bellpull, burgee,
|
|
21
|
+
<a href="https://github.com/ofri-peretz/burgee/blob/main/packages/contractdiff/package.json"><img src="https://img.shields.io/badge/dependencies-4%20in%20family%2C%200%20outside-0a6b47?style=flat-square" alt="Four dependencies, all in the burgee family (bellpull, burgee, seniority, truefit), none outside it" /></a>
|
|
22
22
|
<a href="https://github.com/ofri-peretz/burgee/blob/main/packages/contractdiff/package.json"><img src="https://img.shields.io/badge/types-included-blue?style=flat-square" alt="TypeScript types included for every entry point" /></a>
|
|
23
23
|
<a href="https://github.com/ofri-peretz/burgee/blob/main/packages/contractdiff/package.json"><img src="https://img.shields.io/badge/Node.js-22.13%2B-green?style=flat-square" alt="Node.js 22.13+" /></a>
|
|
24
24
|
<a href="https://github.com/ofri-peretz/burgee/blob/main/packages/contractdiff/LICENSE"><img src="https://img.shields.io/badge/License-MIT-blue?style=flat-square" alt="License: MIT" /></a>
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
**contractdiff** reads an API contract (OpenAPI, Swagger 2.0, JSON Schema, AsyncAPI or an MCP tool
|
|
34
34
|
list) and does four things with it. `diff` says what a change breaks and for whom, under the
|
|
35
35
|
compatibility mode you pick. `lint` checks the contract's form with rules, Spectral and Redocly
|
|
36
|
-
rulesets included. `gen` writes code from it, one plugin per target: TypeScript types,
|
|
36
|
+
rulesets included. `gen` writes code from it, one plugin per target: TypeScript types, truefit
|
|
37
37
|
validators, fetch and TanStack Query clients, MSW handlers and NestJS, Express, Fastify and Hono
|
|
38
38
|
servers. `extract` writes the contract back from the code. Every finding names its fix, as the same
|
|
39
39
|
record for a person, for `--json` and for a coding agent.
|
|
@@ -52,7 +52,7 @@ bun add contractdiff
|
|
|
52
52
|
```
|
|
53
53
|
|
|
54
54
|
Install it as a dev dependency (`-D`) of the project that owns the contract. The generated
|
|
55
|
-
validators import `
|
|
55
|
+
validators import `truefit`, which comes with it.
|
|
56
56
|
|
|
57
57
|
## Quick start
|
|
58
58
|
|
|
@@ -174,18 +174,18 @@ code and for `extract` from TypeScript sources.
|
|
|
174
174
|
|
|
175
175
|
It hosts no plugin key of its own.
|
|
176
176
|
|
|
177
|
-
Nothing in this family builds on it yet, and it builds on `bellpull`, `burgee`, `
|
|
177
|
+
Nothing in this family builds on it yet, and it builds on `bellpull`, `burgee`, `seniority`, `truefit`.
|
|
178
178
|
|
|
179
179
|
## The family
|
|
180
180
|
|
|
181
181
|
14 packages, one repository, one release pipeline. Each installs and works on its own, and each owns
|
|
182
|
-
one job. Seven of them — `bellpull`, `closeout`, `linegauge`, `
|
|
183
|
-
`
|
|
182
|
+
one job. Seven of them — `bellpull`, `closeout`, `linegauge`, `paratext`, `roundel`, `seniority`,
|
|
183
|
+
`truefit` — depend on nothing; the others depend only on packages in this table, and every
|
|
184
184
|
dependency points one way, down the [layers](https://burgee.interlace.tools/docs/concepts/family).
|
|
185
185
|
|
|
186
186
|
No package declares a dependency from outside the family; `react` and `react-reconciler` are
|
|
187
187
|
optional peers of `controlroom`, which npm does not install; `vitest` is an optional peer of
|
|
188
|
-
`oxmutate`, which npm does not install; `eslint` is an optional peer of `
|
|
188
|
+
`oxmutate`, which npm does not install; `eslint` is an optional peer of `truepass`, which npm does
|
|
189
189
|
not install.
|
|
190
190
|
[`package-shape-lock`](https://github.com/ofri-peretz/burgee/blob/main/scripts/package-shape-lock.test.ts)
|
|
191
191
|
holds that for every manifest, and
|
|
@@ -210,9 +210,9 @@ keeps both true.
|
|
|
210
210
|
| [bellpull](https://bellpull.interlace.tools/docs) | Subprocesses, and which executable actually ran | cross-spawn and which |
|
|
211
211
|
| [controlroom](https://controlroom.interlace.tools/docs) | Keyboard-driven terminal screens, inline or full-screen | ink |
|
|
212
212
|
| [oxmutate](https://oxmutate.interlace.tools/docs) | Mutation testing on the oxc engine, inside your own Vitest | Stryker |
|
|
213
|
-
| [
|
|
213
|
+
| [truepass](https://burgee.interlace.tools/docs/packages/truepass) | Lint rules and a pre-push check that keep a test suite honest | @vitest/eslint-plugin |
|
|
214
214
|
| **contractdiff** (this package) | API contracts: breaking-change diff, lint, and code from the spec | openapi-typescript |
|
|
215
|
-
| [
|
|
215
|
+
| [truefit](https://burgee.interlace.tools/docs/packages/truefit) | Validation whose schemas are data, with no eval | zod |
|
|
216
216
|
|
|
217
217
|
Every migration guide, and the family-wide [compatibility](https://burgee.interlace.tools/docs/compatibility)
|
|
218
218
|
and [benchmarks](https://burgee.interlace.tools/docs/benchmarks) pages, are on
|
package/dist/diff-normalise.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { date, datetime, duration, email, hostname, ipv4, ipv6, time, uuid } from '
|
|
1
|
+
import { date, datetime, duration, email, hostname, ipv4, ipv6, time, uuid } from 'truefit/formats';
|
|
2
2
|
export const isDict = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
3
3
|
export const has = (value, key) => Object.hasOwn(value, key);
|
|
4
4
|
export const canonical = (value) => JSON.stringify(value, (_key, node) => (isDict(node) ? Object.fromEntries(Object.keys(node).sort().map((key) => [key, node[key]])) : node));
|
package/dist/lint-functions.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { check } from '
|
|
2
|
-
import { fromJsonSchema } from '
|
|
1
|
+
import { check } from 'truefit';
|
|
2
|
+
import { fromJsonSchema } from 'truefit/json-schema';
|
|
3
3
|
export const CASINGS = ['camel', 'pascal', 'kebab', 'snake', 'macro', 'flat'];
|
|
4
4
|
const CASE = {
|
|
5
5
|
camel: /^[a-z][a-zA-Z\d]*$/u,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { STATUS_CODES } from 'node:http';
|
|
2
|
-
import { string } from '
|
|
3
|
-
import { standardJson } from '
|
|
2
|
+
import { string } from 'truefit';
|
|
3
|
+
import { standardJson } from 'truefit/json-schema';
|
|
4
4
|
import { stable } from './extract-normalise.js';
|
|
5
5
|
import { escape, finding, isRecord, isStandard, loadModule, placeAt, relocate, sideOf, toJsonSchema } from './extract-standard.js';
|
|
6
6
|
const BASE = '#/components/schemas/';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { optional, number, object, string } from '
|
|
2
|
-
import { standardJson } from '
|
|
1
|
+
import { optional, number, object, string } from 'truefit';
|
|
2
|
+
import { standardJson } from 'truefit/json-schema';
|
|
3
3
|
import { definitions, escape, isStandard, loadModule, placeAt, relocate, sideOf, toJsonSchema } from './extract-standard.js';
|
|
4
4
|
export const DIALECT = 'https://json-schema.org/draft/2020-12/schema';
|
|
5
5
|
export const exportsOf = (namespace, wanted) => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { emit, emitWith, typeOf } from '
|
|
2
|
-
import { fromJsonSchema } from '
|
|
3
|
-
export const DOOR_IMPORTS = "import { check } from '
|
|
1
|
+
import { emit, emitWith, typeOf } from 'truefit/emit';
|
|
2
|
+
import { fromJsonSchema } from 'truefit/json-schema';
|
|
3
|
+
export const DOOR_IMPORTS = "import { check } from 'truefit';\nimport { fromJsonSchema } from 'truefit/json-schema';";
|
|
4
4
|
const guard = (name) => `(value: unknown) => value is ${name}`;
|
|
5
5
|
const declared = (name, schema) => {
|
|
6
6
|
const { text, aliases } = typeOf(schema, name);
|
package/dist/props-generate.js
CHANGED
|
@@ -244,13 +244,14 @@ const FORMAT = {
|
|
|
244
244
|
regex: (draw) => draw.random.pick(PATTERNS),
|
|
245
245
|
byte: (draw) => Buffer.from(Array.from({ length: within(draw, BYTES) }, () => within(draw, BYTE))).toString('base64'),
|
|
246
246
|
};
|
|
247
|
+
const WRITERS = new Map(Object.entries(FORMAT));
|
|
247
248
|
export const isFormat = (name) => FORMATS.includes(name);
|
|
248
249
|
const string = (draw, schema, pointer) => {
|
|
249
250
|
reportPresent(draw, schema, REPORTED_STRING, pointer);
|
|
250
251
|
if (isFormat(schema.format)) {
|
|
251
252
|
if (has(schema, 'minLength') || has(schema, 'maxLength'))
|
|
252
253
|
report(draw, 'format-length', pointer);
|
|
253
|
-
const write =
|
|
254
|
+
const write = WRITERS.get(schema.format);
|
|
254
255
|
return { value: write(draw) };
|
|
255
256
|
}
|
|
256
257
|
if (has(schema, 'format'))
|
package/dist/props-judge.js
CHANGED
|
@@ -1,15 +1,46 @@
|
|
|
1
|
-
import { check, parse } from '
|
|
2
|
-
import { fromJsonSchema } from '
|
|
1
|
+
import { check, parse } from 'truefit';
|
|
2
|
+
import { fromJsonSchema, jsonSchemaReader } from 'truefit/json-schema';
|
|
3
3
|
import { isDict } from './diff-normalise.js';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
const judgeOf = (read) => ({
|
|
5
|
+
accepts: (value) => check(read, value),
|
|
6
|
+
paths: (value) => {
|
|
7
|
+
const parsed = parse(read, value);
|
|
8
|
+
return parsed.ok ? [] : parsed.issues.map((issue) => issue.path);
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
export const validatorJudge = (document, draft) => judgeOf(fromJsonSchema(document, { draft }).schema);
|
|
12
|
+
const readers = new WeakMap();
|
|
13
|
+
const readerFor = (document, draft) => {
|
|
14
|
+
const byDraft = readers.get(document) ?? new Map();
|
|
15
|
+
readers.set(document, byDraft);
|
|
16
|
+
const known = byDraft.get(draft);
|
|
17
|
+
if (known !== undefined)
|
|
18
|
+
return known;
|
|
19
|
+
const reader = jsonSchemaReader(rooted({}, document), { draft });
|
|
20
|
+
byDraft.set(draft, reader);
|
|
21
|
+
return reader;
|
|
22
|
+
};
|
|
23
|
+
const intoPayload = (ref) => {
|
|
24
|
+
if (typeof ref !== 'string')
|
|
25
|
+
return false;
|
|
26
|
+
if (ref === '#')
|
|
27
|
+
return true;
|
|
28
|
+
if (!ref.startsWith('#/'))
|
|
29
|
+
return false;
|
|
30
|
+
return !DEFINITIONS.includes(decodeURIComponent(ref.slice(2).split('/')[0]));
|
|
31
|
+
};
|
|
32
|
+
const refersInside = (schema) => {
|
|
33
|
+
if (Array.isArray(schema))
|
|
34
|
+
return schema.some(refersInside);
|
|
35
|
+
if (!isDict(schema))
|
|
36
|
+
return false;
|
|
37
|
+
return intoPayload(schema.$ref) || Object.values(schema).some(refersInside);
|
|
38
|
+
};
|
|
39
|
+
const selfReferent = (schema) => DEFINITIONS.some((key) => key in schema) || refersInside(schema);
|
|
40
|
+
export const payloadJudge = (schema, document, draft) => {
|
|
41
|
+
if (!isDict(document) || (isDict(schema) && selfReferent(schema)))
|
|
42
|
+
return validatorJudge(rooted(schema, document), draft);
|
|
43
|
+
return judgeOf(readerFor(document, draft).read(rooted(schema, null)));
|
|
13
44
|
};
|
|
14
45
|
export const draftOf = (contract) => {
|
|
15
46
|
const document = isDict(contract.document) ? contract.document : {};
|
package/dist/props-run.js
CHANGED
|
@@ -2,15 +2,15 @@ import { payloadRules } from './diff-classify.js';
|
|
|
2
2
|
import { compareContracts, pair } from './diff-contract.js';
|
|
3
3
|
import { canonical, isDict, normalise } from './diff-normalise.js';
|
|
4
4
|
import { drawFrom, make } from './props-generate.js';
|
|
5
|
-
import { draftOf,
|
|
5
|
+
import { draftOf, payloadJudge } from './props-judge.js';
|
|
6
6
|
import { mutate } from './props-mutate.js';
|
|
7
7
|
import { randomFrom } from './props-random.js';
|
|
8
8
|
import { shrink } from './props-shrink.js';
|
|
9
9
|
export const PROPERTIES = ['agreement', 'round-trip', 'normalise', 'soundness'];
|
|
10
|
-
export const SUBJECT = { validator:
|
|
10
|
+
export const SUBJECT = { validator: payloadJudge, normalise, encode: (value) => JSON.stringify(value), decode: (text) => JSON.parse(text), compare: compareContracts };
|
|
11
11
|
const judge = (subject, schema, document, draft) => {
|
|
12
12
|
try {
|
|
13
|
-
return subject.validator(
|
|
13
|
+
return subject.validator(schema, document, draft);
|
|
14
14
|
}
|
|
15
15
|
catch (error) {
|
|
16
16
|
return error.message;
|
|
@@ -27,8 +27,12 @@ const fail = (run, counterexample) => {
|
|
|
27
27
|
};
|
|
28
28
|
const drawIn = (run, contract) => drawFrom(run.random, contract.document, draftOf(contract));
|
|
29
29
|
const gather = (into, from) => {
|
|
30
|
-
for (const [keyword, at] of from)
|
|
31
|
-
|
|
30
|
+
for (const [keyword, at] of from) {
|
|
31
|
+
const known = into.get(keyword) ?? new Set();
|
|
32
|
+
into.set(keyword, known);
|
|
33
|
+
for (const place of at)
|
|
34
|
+
known.add(place);
|
|
35
|
+
}
|
|
32
36
|
};
|
|
33
37
|
const agreement = (run, check, draw, value) => {
|
|
34
38
|
const { where, own } = check;
|
|
@@ -60,6 +64,8 @@ const roundTrip = (run, check, value) => {
|
|
|
60
64
|
tally(run, 'round-trip').checked += 1;
|
|
61
65
|
const broken = (candidate) => {
|
|
62
66
|
const back = subject.decode(subject.encode(candidate));
|
|
67
|
+
if (JSON.stringify(back) === JSON.stringify(candidate))
|
|
68
|
+
return false;
|
|
63
69
|
return canonical(back) !== canonical(candidate);
|
|
64
70
|
};
|
|
65
71
|
if (!broken(value))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "contractdiff",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Breaking-change diff, lint and code generation for API contracts (OpenAPI, JSON Schema, AsyncAPI, MCP), and the spec back from the code. No dependency outside the burgee family. An openapi-typescript migration path.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
}
|
|
68
68
|
},
|
|
69
69
|
"bin": {
|
|
70
|
-
"contractdiff": "
|
|
70
|
+
"contractdiff": "dist/bin.js"
|
|
71
71
|
},
|
|
72
72
|
"files": [
|
|
73
73
|
"dist",
|
|
@@ -83,9 +83,9 @@
|
|
|
83
83
|
},
|
|
84
84
|
"dependencies": {
|
|
85
85
|
"bellpull": "^2.0.3",
|
|
86
|
-
"burgee": "^2.2.
|
|
86
|
+
"burgee": "^2.2.1",
|
|
87
87
|
"seniority": "^2.0.0",
|
|
88
|
-
"
|
|
88
|
+
"truefit": "^0.2.0"
|
|
89
89
|
},
|
|
90
90
|
"devDependencies": {
|
|
91
91
|
"vitest": "^5.0.1"
|