@cplieger/deadset-ts 8.0.0-dev.7 → 8.0.0-dev.8
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/dist/bin/node-host.js +21 -3
- package/dist/src/analysis.js +13 -14
- package/dist/src/config.js +6 -83
- package/dist/src/configured-declarations.js +15 -20
- package/dist/src/consumers.js +2 -2
- package/dist/src/contract/config.generated.js +544 -0
- package/dist/src/contract/report.generated.js +3 -2
- package/dist/src/discover.js +1 -1
- package/dist/src/edges.js +36 -112
- package/dist/src/exempt-classes.js +0 -2
- package/dist/src/exempt.js +1 -1
- package/dist/src/findings/completion.js +5 -4
- package/dist/src/findings/emitters.js +1 -1
- package/dist/src/findings/intra-function.js +1 -1
- package/dist/src/findings/non-code-artifacts.js +2 -2
- package/dist/src/findings/unused-declarations.js +1 -1
- package/dist/src/findings/visibility-narrowing.js +2 -2
- package/dist/src/findings-pass.js +1 -1
- package/dist/src/framework-lifecycle.js +7 -7
- package/dist/src/fs/document.js +59 -0
- package/dist/src/injection-container.js +1 -1
- package/dist/src/json-document.js +194 -67
- package/dist/src/kinds.js +0 -7
- package/dist/src/print.js +10 -79
- package/dist/src/report-schema.js +3 -224
- package/dist/src/resolve.js +245 -715
- package/dist/src/run.js +48 -55
- package/dist/src/schema.js +320 -344
- package/dist/src/scope.js +33 -101
- package/dist/src/serialization-contract.js +1 -1
- package/dist/src/suppress-file.js +15 -17
- package/dist/src/suppress.js +3 -3
- package/dist/src/template-field.js +4 -4
- package/dist/src/usage.js +10 -0
- package/dist/src/verbs/analyze.js +12 -15
- package/dist/src/verbs/explain.js +3 -3
- package/dist/src/verbs/print-projects.js +2 -2
- package/jsr.json +1 -1
- package/package.json +1 -1
- package/src/analysis.ts +30 -26
- package/src/config.ts +31 -298
- package/src/configured-declarations.ts +18 -23
- package/src/consumers.ts +4 -4
- package/src/contract/config.generated.ts +798 -0
- package/src/contract/report.generated.ts +3 -2
- package/src/discover.ts +1 -1
- package/src/edges.ts +39 -124
- package/src/exempt-classes.ts +0 -5
- package/src/exempt.ts +4 -4
- package/src/findings/completion.ts +5 -6
- package/src/findings/emitter.ts +2 -2
- package/src/findings/emitters.ts +1 -1
- package/src/findings/intra-function.ts +1 -1
- package/src/findings/non-code-artifacts.ts +4 -4
- package/src/findings/unused-declarations.ts +1 -1
- package/src/findings/visibility-narrowing.ts +2 -2
- package/src/findings-pass.ts +3 -3
- package/src/framework-lifecycle.ts +7 -7
- package/src/fs/document.ts +74 -0
- package/src/host.ts +12 -5
- package/src/injection-container.ts +1 -1
- package/src/json-document.ts +236 -76
- package/src/kinds.ts +0 -8
- package/src/print.ts +11 -92
- package/src/report-schema.ts +3 -299
- package/src/resolve.ts +281 -1105
- package/src/run.ts +50 -63
- package/src/schema.ts +391 -389
- package/src/scope.ts +39 -119
- package/src/serialization-contract.ts +1 -1
- package/src/suppress-file.ts +15 -20
- package/src/suppress.ts +3 -3
- package/src/template-field.ts +10 -7
- package/src/usage.ts +10 -0
- package/src/verbs/analyze.ts +12 -23
- package/src/verbs/explain.ts +3 -7
- package/src/verbs/print-projects.ts +2 -2
- package/src/verbs/verb.ts +2 -2
package/src/schema.ts
CHANGED
|
@@ -1,467 +1,469 @@
|
|
|
1
|
-
import { ConfigError } from "./config.ts";
|
|
2
|
-
import { bytewise } from "./text/compare.ts";
|
|
3
|
-
|
|
4
1
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* array of objects holding every admitted shape's members; `object`, one setting
|
|
9
|
-
* written as an object of declared members and supplied whole. Which shape an entry or
|
|
10
|
-
* a value takes, where the key list admits several, is the decoder's refusal.
|
|
2
|
+
* Holds a value to a JSON Schema the generator copies from the Contract. It evaluates the
|
|
3
|
+
* keywords the Contract's schemas use; the schemas' patterns are written to match alike in
|
|
4
|
+
* RE2 and in ECMAScript.
|
|
11
5
|
*/
|
|
12
|
-
|
|
6
|
+
|
|
7
|
+
import { isRecord, pointerOf, pointerSegments } from "./json-document.ts";
|
|
8
|
+
import { bytewise } from "./text/compare.ts";
|
|
13
9
|
|
|
14
10
|
/**
|
|
15
|
-
* One
|
|
16
|
-
*
|
|
17
|
-
*
|
|
11
|
+
* One value a schema refuses: its JSON Pointer, the keyword, and why. `member` names the
|
|
12
|
+
* member a `required`, `additionalProperties` or `propertyNames` refusal is about, the
|
|
13
|
+
* object being at `pointer`, and is empty for every other keyword.
|
|
18
14
|
*/
|
|
19
|
-
export interface
|
|
20
|
-
readonly
|
|
21
|
-
readonly
|
|
22
|
-
readonly
|
|
15
|
+
export interface SchemaRefusal {
|
|
16
|
+
readonly pointer: string;
|
|
17
|
+
readonly keyword: string;
|
|
18
|
+
readonly reason: string;
|
|
19
|
+
readonly member: string;
|
|
23
20
|
}
|
|
24
21
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
/** A list of declarations, each entry in one of the three shapes the `ts` section states. */
|
|
29
|
-
const declarations: KeyNode = {
|
|
30
|
-
kind: "list",
|
|
31
|
-
members: { symbol: leaf, module: leaf, name: leaf, global: leaf },
|
|
32
|
-
};
|
|
22
|
+
export type Schema = Readonly<Record<string, unknown>>;
|
|
23
|
+
type Documents = Readonly<Record<string, Schema>>;
|
|
33
24
|
|
|
34
25
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* adds a key fails there rather than being silently unimplemented.
|
|
26
|
+
* Every keyword the validator evaluates or knows to carry no assertion. A schema naming
|
|
27
|
+
* another fails every check rather than passing values the keyword would refuse.
|
|
38
28
|
*/
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
},
|
|
47
|
-
analysis: {
|
|
48
|
-
kind: "section",
|
|
49
|
-
members: {
|
|
50
|
-
languages: leaf,
|
|
51
|
-
min_confidence: leaf,
|
|
52
|
-
generated_files: leaf,
|
|
53
|
-
consumer_tests: leaf,
|
|
54
|
-
configurations: {
|
|
55
|
-
kind: "list",
|
|
56
|
-
members: { id: leaf, os: leaf, arch: leaf, tags: leaf, project: leaf },
|
|
57
|
-
},
|
|
58
|
-
matrix: { kind: "section", members: { complete: leaf } },
|
|
59
|
-
template_dirs: leaf,
|
|
60
|
-
template_delimiters: {
|
|
61
|
-
kind: "object",
|
|
62
|
-
members: { left: leaf, right: leaf },
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
consumers: { kind: "section", members: { complete: leaf } },
|
|
67
|
-
roots: { kind: "section", members: { patterns: leaf } },
|
|
68
|
-
severity: { kind: "map" },
|
|
69
|
-
exemptions: { kind: "section", members: { disabled: leaf } },
|
|
70
|
-
reporters: {
|
|
71
|
-
kind: "section",
|
|
72
|
-
members: {
|
|
73
|
-
formats: leaf,
|
|
74
|
-
sort: leaf,
|
|
75
|
-
cascade: leaf,
|
|
76
|
-
max_findings: integer,
|
|
77
|
-
fail_on: leaf,
|
|
78
|
-
},
|
|
79
|
-
},
|
|
80
|
-
providers: {
|
|
81
|
-
kind: "section",
|
|
82
|
-
members: {
|
|
83
|
-
analyzers: {
|
|
84
|
-
kind: "list",
|
|
85
|
-
members: {
|
|
86
|
-
name: leaf,
|
|
87
|
-
languages: leaf,
|
|
88
|
-
command: leaf,
|
|
89
|
-
source: leaf,
|
|
90
|
-
version: leaf,
|
|
91
|
-
digest: leaf,
|
|
92
|
-
},
|
|
93
|
-
},
|
|
94
|
-
},
|
|
95
|
-
},
|
|
96
|
-
go: { kind: "section", members: {} },
|
|
97
|
-
ts: {
|
|
98
|
-
kind: "section",
|
|
99
|
-
members: {
|
|
100
|
-
test_files: leaf,
|
|
101
|
-
entry_files: leaf,
|
|
102
|
-
component_extensions: leaf,
|
|
103
|
-
disabled_conventions: leaf,
|
|
104
|
-
injection_registrations: declarations,
|
|
105
|
-
lifecycle_contracts: {
|
|
106
|
-
kind: "list",
|
|
107
|
-
members: { components: declarations, bases: declarations, members: leaf },
|
|
108
|
-
},
|
|
109
|
-
serializers: declarations,
|
|
110
|
-
roots_complete: leaf,
|
|
111
|
-
},
|
|
112
|
-
},
|
|
113
|
-
provenance: { kind: "map" },
|
|
114
|
-
},
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
/** The name of the severity object, whose member names the key list leaves open. */
|
|
118
|
-
export const SEVERITY_SECTION = "severity";
|
|
29
|
+
const KEYWORDS: ReadonlySet<string> = new Set([
|
|
30
|
+
...["$schema", "$id", "$comment", "$defs", "title", "description", "default", "examples"],
|
|
31
|
+
...["$ref", "type", "enum", "const", "pattern", "minLength", "minimum", "maximum"],
|
|
32
|
+
...["properties", "additionalProperties", "propertyNames", "required", "minProperties"],
|
|
33
|
+
...["items", "minItems", "maxItems", "uniqueItems"],
|
|
34
|
+
...["allOf", "anyOf", "oneOf", "not", "if", "then", "else"],
|
|
35
|
+
]);
|
|
119
36
|
|
|
120
|
-
|
|
121
|
-
* The node one member name resolves to, and whether the key list declares it. A
|
|
122
|
-
* member of an open object, and any member below a setting, resolves to a leaf,
|
|
123
|
-
* so the walk descends far enough to find a repeated member at any depth.
|
|
124
|
-
*/
|
|
125
|
-
function memberOf(node: KeyNode, name: string): KeyNode | undefined {
|
|
126
|
-
if (node.kind === "section" || node.kind === "list" || node.kind === "object") {
|
|
127
|
-
const members = node.members ?? {};
|
|
128
|
-
return Object.hasOwn(members, name) ? members[name] : undefined;
|
|
129
|
-
}
|
|
130
|
-
return leaf;
|
|
131
|
-
}
|
|
37
|
+
const patterns = new Map<string, RegExp>();
|
|
132
38
|
|
|
133
|
-
function
|
|
134
|
-
|
|
39
|
+
function pattern(source: string): RegExp {
|
|
40
|
+
let held = patterns.get(source);
|
|
41
|
+
if (held === undefined) {
|
|
42
|
+
held = new RegExp(source, "u");
|
|
43
|
+
patterns.set(source, held);
|
|
44
|
+
}
|
|
45
|
+
return held;
|
|
135
46
|
}
|
|
136
47
|
|
|
137
|
-
function
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
if (child.kind === "leaf" || child.kind === "list" || child.kind === "object") {
|
|
146
|
-
keys.push(path);
|
|
147
|
-
continue;
|
|
148
|
-
}
|
|
149
|
-
collect(child, path, keys, settingsOnly);
|
|
48
|
+
function typeOf(value: unknown): string {
|
|
49
|
+
if (value === null) {
|
|
50
|
+
return "null";
|
|
51
|
+
}
|
|
52
|
+
if (Array.isArray(value)) {
|
|
53
|
+
return "array";
|
|
150
54
|
}
|
|
55
|
+
if (typeof value === "number") {
|
|
56
|
+
return Number.isInteger(value) ? "integer" : "number";
|
|
57
|
+
}
|
|
58
|
+
return typeof value;
|
|
151
59
|
}
|
|
152
60
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
* are spelled under the list's own path followed by empty brackets.
|
|
157
|
-
*/
|
|
158
|
-
export function declaredKeys(): string[] {
|
|
159
|
-
const keys: string[] = [];
|
|
160
|
-
collect(SCHEMA_ROOT, "", keys, false);
|
|
161
|
-
return keys.sort(bytewise);
|
|
61
|
+
function hasType(value: unknown, name: string): boolean {
|
|
62
|
+
const held = typeOf(value);
|
|
63
|
+
return held === name || (name === "number" && held === "integer");
|
|
162
64
|
}
|
|
163
65
|
|
|
164
|
-
/**
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
const paths: string[] = [];
|
|
172
|
-
collect(SCHEMA_ROOT, "", paths, true);
|
|
173
|
-
return paths.sort(bytewise);
|
|
66
|
+
/** The types a schema names, none where it names no type. */
|
|
67
|
+
function typesOf(schema: Schema): readonly string[] {
|
|
68
|
+
const types = schema["type"];
|
|
69
|
+
if (types === undefined) {
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
return (Array.isArray(types) ? types : [types]).filter((one) => typeof one === "string");
|
|
174
73
|
}
|
|
175
74
|
|
|
176
|
-
/**
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
* through, so a flag that would be refused as an unimplemented key is a failing
|
|
181
|
-
* test rather than a flag nothing reads.
|
|
182
|
-
*
|
|
183
|
-
* A section holds no value of its own, so `analysis` is false. A setting written
|
|
184
|
-
* as an object is supplied whole, so `analysis.template_delimiters` is true and
|
|
185
|
-
* `analysis.template_delimiters.left` is false. A severity code is a declared
|
|
186
|
-
* setting whatever it names, so `severity.DS9999` is true here and refused by
|
|
187
|
-
* resolution: this answers the shape of a path, and resolution answers the
|
|
188
|
-
* vocabulary.
|
|
189
|
-
*/
|
|
190
|
-
export function declaresSetting(path: string): boolean {
|
|
191
|
-
if (settingPaths().includes(path)) {
|
|
192
|
-
return true;
|
|
75
|
+
/** One value's encoding with object members sorted, so two equal values encode alike. */
|
|
76
|
+
function canonical(value: unknown): string {
|
|
77
|
+
if (Array.isArray(value)) {
|
|
78
|
+
return `[${value.map(canonical).join(",")}]`;
|
|
193
79
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
80
|
+
if (isRecord(value)) {
|
|
81
|
+
return `{${Object.keys(value)
|
|
82
|
+
.sort(bytewise)
|
|
83
|
+
.map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`)
|
|
84
|
+
.join(",")}}`;
|
|
197
85
|
}
|
|
198
|
-
|
|
199
|
-
return path.slice(0, dot) === SEVERITY_SECTION && code !== "" && !code.includes(".");
|
|
86
|
+
return JSON.stringify(value);
|
|
200
87
|
}
|
|
201
88
|
|
|
202
|
-
/**
|
|
203
|
-
function
|
|
204
|
-
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
let current = new Array<number>(to.length + 1).fill(0);
|
|
208
|
-
for (let i = 1; i <= from.length; i += 1) {
|
|
209
|
-
current[0] = i;
|
|
210
|
-
for (let j = 1; j <= to.length; j += 1) {
|
|
211
|
-
const substitution = (previous[j - 1] ?? 0) + (from[i - 1] === to[j - 1] ? 0 : 1);
|
|
212
|
-
current[j] = Math.min(substitution, (previous[j] ?? 0) + 1, (current[j - 1] ?? 0) + 1);
|
|
213
|
-
}
|
|
214
|
-
[previous, current] = [current, previous];
|
|
89
|
+
/** A string's length in code points, which JSON Schema's minLength counts. */
|
|
90
|
+
function codePoints(value: string): number {
|
|
91
|
+
let counted = 0;
|
|
92
|
+
for (const _point of value) {
|
|
93
|
+
counted += 1;
|
|
215
94
|
}
|
|
216
|
-
return
|
|
95
|
+
return counted;
|
|
217
96
|
}
|
|
218
97
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
224
|
-
return shared;
|
|
98
|
+
/** The schemas of a list keyword, none where the schema does not write it. */
|
|
99
|
+
function listOf(schema: Schema, keyword: string): readonly Schema[] {
|
|
100
|
+
const held = schema[keyword];
|
|
101
|
+
return Array.isArray(held) ? (held as Schema[]) : [];
|
|
225
102
|
}
|
|
226
103
|
|
|
227
|
-
/**
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
*/
|
|
232
|
-
export function nearestKey(key: string): string {
|
|
233
|
-
const segments = key.split(".");
|
|
234
|
-
let nearest = "";
|
|
235
|
-
let bestShared = -1;
|
|
236
|
-
let bestDistance = 0;
|
|
237
|
-
for (const candidate of declaredKeys()) {
|
|
238
|
-
const shared = sharedSegments(segments, candidate.split("."));
|
|
239
|
-
const distance = editDistance(key, candidate);
|
|
240
|
-
if (shared < bestShared || (shared === bestShared && distance >= bestDistance)) {
|
|
241
|
-
continue;
|
|
242
|
-
}
|
|
243
|
-
nearest = candidate;
|
|
244
|
-
bestShared = shared;
|
|
245
|
-
bestDistance = distance;
|
|
104
|
+
/** Whether a reader fixes refusal `a` before `b`: an object's before one inside it, then by kind. */
|
|
105
|
+
function before(a: SchemaRefusal, b: SchemaRefusal): boolean {
|
|
106
|
+
if (a.pointer === b.pointer) {
|
|
107
|
+
return rank(a) < rank(b);
|
|
246
108
|
}
|
|
247
|
-
return
|
|
109
|
+
return b.pointer.startsWith(`${a.pointer}/`);
|
|
248
110
|
}
|
|
249
111
|
|
|
250
|
-
/**
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
112
|
+
/** The order of the refusals of one place: an undeclared member, a missing one, any other. */
|
|
113
|
+
function rank(refusal: SchemaRefusal): number {
|
|
114
|
+
if (refusal.keyword === "additionalProperties" || refusal.keyword === "propertyNames") {
|
|
115
|
+
return 0;
|
|
116
|
+
}
|
|
117
|
+
return refusal.keyword === "required" ? 1 : 2;
|
|
254
118
|
}
|
|
255
119
|
|
|
256
|
-
/**
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
function unimplementedKey(label: string, path: string): ConfigError {
|
|
261
|
-
const nearest = nearestKey(path);
|
|
262
|
-
let hint = "";
|
|
263
|
-
if (nearest === path) {
|
|
264
|
-
hint = `. ${JSON.stringify(path)} is one setting written as nested objects, not as one key`;
|
|
265
|
-
} else if (nearest !== "") {
|
|
266
|
-
hint = `. The nearest implemented key is ${JSON.stringify(nearest)}`;
|
|
120
|
+
/** The value one JSON Pointer segment names inside `value`, or undefined. */
|
|
121
|
+
function valueAt(value: unknown, segment: string): unknown {
|
|
122
|
+
if (Array.isArray(value)) {
|
|
123
|
+
return /^[0-9]+$/u.test(segment) ? value[Number(segment)] : undefined;
|
|
267
124
|
}
|
|
268
|
-
return
|
|
269
|
-
"unimplemented-key",
|
|
270
|
-
path,
|
|
271
|
-
`${label}: key ${JSON.stringify(path)} is not implemented${hint}`,
|
|
272
|
-
);
|
|
125
|
+
return isRecord(value) && Object.hasOwn(value, segment) ? value[segment] : undefined;
|
|
273
126
|
}
|
|
274
127
|
|
|
275
128
|
/**
|
|
276
|
-
* The
|
|
277
|
-
*
|
|
278
|
-
* asks: a member written twice at one level is a refusal, and a parse keeps the
|
|
279
|
-
* last value, so the document reads as though it named the member once. Nothing
|
|
280
|
-
* here produces a value; the value is the parse's.
|
|
129
|
+
* The schema `properties` declares for the member `name`, or undefined: a member named as an
|
|
130
|
+
* `Object.prototype` property, such as `__proto__`, is declared only where it is written.
|
|
281
131
|
*/
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
at: number;
|
|
132
|
+
function declaredMember(properties: Readonly<Record<string, unknown>>, name: string): unknown {
|
|
133
|
+
return Object.hasOwn(properties, name) ? properties[name] : undefined;
|
|
285
134
|
}
|
|
286
135
|
|
|
287
|
-
|
|
136
|
+
/** A schema with the document it is read from, so a relative `$ref` resolves there. */
|
|
137
|
+
type Located = readonly [string, Schema];
|
|
288
138
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
}
|
|
293
|
-
}
|
|
139
|
+
/** The keyword checks over one set of schema documents, each named as a `$ref` names it. */
|
|
140
|
+
class Checker {
|
|
141
|
+
private readonly documents: Documents;
|
|
294
142
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
* finds the literal's extent and the parse reads it, so an escaped member name
|
|
298
|
-
* compares equal to the name it spells.
|
|
299
|
-
*/
|
|
300
|
-
function readName(cursor: Cursor, at: string, label: string): string {
|
|
301
|
-
const start = cursor.at;
|
|
302
|
-
cursor.at += 1;
|
|
303
|
-
while (cursor.at < cursor.text.length) {
|
|
304
|
-
const ch = cursor.text.charCodeAt(cursor.at);
|
|
305
|
-
if (ch === 0x5c) {
|
|
306
|
-
cursor.at += 2;
|
|
307
|
-
continue;
|
|
308
|
-
}
|
|
309
|
-
cursor.at += 1;
|
|
310
|
-
if (ch === 0x22) {
|
|
311
|
-
return JSON.parse(cursor.text.slice(start, cursor.at)) as string;
|
|
312
|
-
}
|
|
143
|
+
constructor(documents: Documents) {
|
|
144
|
+
this.documents = documents;
|
|
313
145
|
}
|
|
314
|
-
throw malformed(label, at, "a member name is not terminated");
|
|
315
|
-
}
|
|
316
146
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
const
|
|
320
|
-
|
|
321
|
-
|
|
147
|
+
resolve(doc: string, ref: string): Located {
|
|
148
|
+
const [file = "", pointer = ""] = ref.split("#");
|
|
149
|
+
const target = file === "" ? doc : file;
|
|
150
|
+
let schema: unknown = this.documents[target];
|
|
151
|
+
for (const token of pointerSegments(pointer)) {
|
|
152
|
+
schema = isRecord(schema) ? schema[token] : undefined;
|
|
153
|
+
}
|
|
154
|
+
if (!isRecord(schema)) {
|
|
155
|
+
throw new Error(`schema: ${ref} names no schema`);
|
|
322
156
|
}
|
|
323
|
-
|
|
157
|
+
return [target, schema];
|
|
324
158
|
}
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
/** A number the document writes as an optional minus sign and digits alone. */
|
|
328
|
-
const INTEGER_TEXT = /^-?[0-9]+$/u;
|
|
329
159
|
|
|
330
|
-
|
|
331
|
-
const
|
|
160
|
+
refusals(doc: string, schema: Schema, value: unknown, at: string): readonly SchemaRefusal[] {
|
|
161
|
+
const found: SchemaRefusal[] = [];
|
|
162
|
+
this.check(doc, schema, value, at, found);
|
|
163
|
+
return found;
|
|
164
|
+
}
|
|
332
165
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
* declares an integer. A parse reads `100.0` and `1e2` as the integer 100, so only
|
|
336
|
-
* the text can tell them apart. A scalar that is no number is the decoder's to
|
|
337
|
-
* refuse by its type.
|
|
338
|
-
*/
|
|
339
|
-
function checkInteger(text: string, at: string, label: string): void {
|
|
340
|
-
if (NUMBER_START.test(text) && !INTEGER_TEXT.test(text)) {
|
|
341
|
-
throw malformed(
|
|
342
|
-
label,
|
|
343
|
-
at,
|
|
344
|
-
`${text} is written with a fraction or an exponent, and an integer is digits alone`,
|
|
345
|
-
);
|
|
166
|
+
matches(doc: string, schema: Schema, value: unknown, at: string): boolean {
|
|
167
|
+
return this.refusals(doc, schema, value, at).length === 0;
|
|
346
168
|
}
|
|
347
|
-
}
|
|
348
169
|
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
170
|
+
check(doc: string, schema: Schema, value: unknown, at: string, found: SchemaRefusal[]): void {
|
|
171
|
+
for (const keyword of Object.keys(schema)) {
|
|
172
|
+
if (!KEYWORDS.has(keyword) && !keyword.startsWith("x-")) {
|
|
173
|
+
throw new Error(`schema: the keyword ${keyword} is not one this validator evaluates`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const refuse = (keyword: string, reason: string, member = ""): void => {
|
|
177
|
+
found.push({ pointer: at, keyword, reason, member });
|
|
178
|
+
};
|
|
179
|
+
if (typeof schema["$ref"] === "string") {
|
|
180
|
+
const [target, referenced] = this.resolve(doc, schema["$ref"]);
|
|
181
|
+
this.check(target, referenced, value, at, found);
|
|
182
|
+
}
|
|
183
|
+
const named = typesOf(schema);
|
|
184
|
+
if (named.length > 0 && !named.some((one) => hasType(value, one))) {
|
|
185
|
+
refuse("type", `the value is ${typeOf(value)}, want ${named.join(", ")}`);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
this.checkValue(schema, value, refuse);
|
|
189
|
+
if (Array.isArray(value)) {
|
|
190
|
+
this.checkArray(doc, schema, value, at, found, refuse);
|
|
191
|
+
} else if (isRecord(value)) {
|
|
192
|
+
this.checkObject(doc, schema, value, at, found, refuse);
|
|
193
|
+
}
|
|
194
|
+
this.checkApplicators(doc, schema, value, at, found, refuse);
|
|
356
195
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
196
|
+
|
|
197
|
+
private checkValue(
|
|
198
|
+
schema: Schema,
|
|
199
|
+
value: unknown,
|
|
200
|
+
refuse: (keyword: string, reason: string) => void,
|
|
201
|
+
): void {
|
|
202
|
+
const values = schema["enum"];
|
|
203
|
+
if (Array.isArray(values) && !values.some((one) => canonical(one) === canonical(value))) {
|
|
204
|
+
refuse("enum", `${canonical(value)} is not one of ${values.map(canonical).join(", ")}`);
|
|
205
|
+
}
|
|
206
|
+
if (schema["const"] !== undefined && canonical(schema["const"]) !== canonical(value)) {
|
|
207
|
+
refuse("const", `${canonical(value)} is not ${canonical(schema["const"])}`);
|
|
208
|
+
}
|
|
209
|
+
if (typeof value === "string") {
|
|
210
|
+
if (typeof schema["pattern"] === "string" && !pattern(schema["pattern"]).test(value)) {
|
|
211
|
+
refuse("pattern", `${JSON.stringify(value)} does not match ${schema["pattern"]}`);
|
|
212
|
+
}
|
|
213
|
+
if (typeof schema["minLength"] === "number" && codePoints(value) < schema["minLength"]) {
|
|
214
|
+
refuse(
|
|
215
|
+
"minLength",
|
|
216
|
+
`${JSON.stringify(value)} is shorter than ${String(schema["minLength"])} characters`,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (typeof value === "number") {
|
|
221
|
+
if (typeof schema["minimum"] === "number" && value < schema["minimum"]) {
|
|
222
|
+
refuse("minimum", `${String(value)} is below ${String(schema["minimum"])}`);
|
|
223
|
+
}
|
|
224
|
+
if (typeof schema["maximum"] === "number" && value > schema["maximum"]) {
|
|
225
|
+
refuse("maximum", `${String(value)} is above ${String(schema["maximum"])}`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
361
228
|
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
229
|
+
|
|
230
|
+
private checkObject(
|
|
231
|
+
doc: string,
|
|
232
|
+
schema: Schema,
|
|
233
|
+
value: Readonly<Record<string, unknown>>,
|
|
234
|
+
at: string,
|
|
235
|
+
found: SchemaRefusal[],
|
|
236
|
+
refuse: (keyword: string, reason: string, member?: string) => void,
|
|
237
|
+
): void {
|
|
238
|
+
const properties = isRecord(schema["properties"]) ? schema["properties"] : {};
|
|
239
|
+
const additional = schema["additionalProperties"];
|
|
240
|
+
const names = schema["propertyNames"];
|
|
241
|
+
for (const name of Object.keys(value).sort(bytewise)) {
|
|
242
|
+
const [refusedName] = isRecord(names) ? this.refusals(doc, names, name, at) : [];
|
|
243
|
+
if (refusedName !== undefined) {
|
|
244
|
+
refuse(
|
|
245
|
+
"propertyNames",
|
|
246
|
+
`the member name ${JSON.stringify(name)}: ${refusedName.reason}`,
|
|
247
|
+
name,
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
const member = `${at}${pointerOf([name])}`;
|
|
251
|
+
const declared = declaredMember(properties, name);
|
|
252
|
+
if (isRecord(declared)) {
|
|
253
|
+
this.check(doc, declared, value[name], member, found);
|
|
254
|
+
} else if (additional === false) {
|
|
255
|
+
refuse(
|
|
256
|
+
"additionalProperties",
|
|
257
|
+
`the member ${JSON.stringify(name)} is not one the schema declares`,
|
|
258
|
+
name,
|
|
259
|
+
);
|
|
260
|
+
} else if (isRecord(additional)) {
|
|
261
|
+
this.check(doc, additional, value[name], member, found);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
for (const name of Array.isArray(schema["required"]) ? (schema["required"] as string[]) : []) {
|
|
265
|
+
if (!Object.hasOwn(value, name)) {
|
|
266
|
+
refuse("required", `the member ${JSON.stringify(name)} is absent`, name);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
const fewest = schema["minProperties"];
|
|
270
|
+
if (typeof fewest === "number" && Object.keys(value).length < fewest) {
|
|
271
|
+
refuse(
|
|
272
|
+
"minProperties",
|
|
273
|
+
`the object holds ${String(Object.keys(value).length)} members, fewer than ${String(fewest)}`,
|
|
274
|
+
);
|
|
275
|
+
}
|
|
365
276
|
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
277
|
+
|
|
278
|
+
private checkArray(
|
|
279
|
+
doc: string,
|
|
280
|
+
schema: Schema,
|
|
281
|
+
value: readonly unknown[],
|
|
282
|
+
at: string,
|
|
283
|
+
found: SchemaRefusal[],
|
|
284
|
+
refuse: (keyword: string, reason: string) => void,
|
|
285
|
+
): void {
|
|
286
|
+
const items = schema["items"];
|
|
287
|
+
if (isRecord(items)) {
|
|
288
|
+
value.forEach((item, i) => {
|
|
289
|
+
this.check(doc, items, item, `${at}/${String(i)}`, found);
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
if (typeof schema["minItems"] === "number" && value.length < schema["minItems"]) {
|
|
293
|
+
refuse(
|
|
294
|
+
"minItems",
|
|
295
|
+
`the array holds ${String(value.length)} items, fewer than ${String(schema["minItems"])}`,
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
if (typeof schema["maxItems"] === "number" && value.length > schema["maxItems"]) {
|
|
299
|
+
refuse(
|
|
300
|
+
"maxItems",
|
|
301
|
+
`the array holds ${String(value.length)} items, more than ${String(schema["maxItems"])}`,
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
if (schema["uniqueItems"] === true && new Set(value.map(canonical)).size !== value.length) {
|
|
305
|
+
refuse("uniqueItems", "the array lists one value twice");
|
|
306
|
+
}
|
|
370
307
|
}
|
|
371
|
-
}
|
|
372
308
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
309
|
+
private checkApplicators(
|
|
310
|
+
doc: string,
|
|
311
|
+
schema: Schema,
|
|
312
|
+
value: unknown,
|
|
313
|
+
at: string,
|
|
314
|
+
found: SchemaRefusal[],
|
|
315
|
+
refuse: (keyword: string, reason: string) => void,
|
|
316
|
+
): void {
|
|
317
|
+
for (const each of listOf(schema, "allOf")) {
|
|
318
|
+
this.check(doc, each, value, at, found);
|
|
319
|
+
}
|
|
320
|
+
const matching = (keyword: string): number =>
|
|
321
|
+
listOf(schema, keyword).filter((each) => this.matches(doc, each, value, at)).length;
|
|
322
|
+
if (schema["anyOf"] !== undefined && matching("anyOf") === 0) {
|
|
323
|
+
refuse("anyOf", "the value matches none of the alternatives");
|
|
324
|
+
}
|
|
325
|
+
if (schema["oneOf"] !== undefined && matching("oneOf") !== 1) {
|
|
326
|
+
refuse(
|
|
327
|
+
"oneOf",
|
|
328
|
+
`the value matches ${String(matching("oneOf"))} of the alternatives, want exactly one`,
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
if (isRecord(schema["not"]) && this.matches(doc, schema["not"], value, at)) {
|
|
332
|
+
refuse("not", "the value matches a schema it must not match");
|
|
333
|
+
}
|
|
334
|
+
if (isRecord(schema["if"])) {
|
|
335
|
+
const branch = this.matches(doc, schema["if"], value, at) ? schema["then"] : schema["else"];
|
|
336
|
+
if (isRecord(branch)) {
|
|
337
|
+
this.check(doc, branch, value, at, found);
|
|
338
|
+
}
|
|
381
339
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* The refusal of `value` a reader fixes first, none for an instance: an object's
|
|
344
|
+
* refusal before any inside it, at one place an undeclared member, then a missing one,
|
|
345
|
+
* then any other, else the order the check finds them in. A oneOf no alternative
|
|
346
|
+
* matches is explained by the first refusal of the alternative the value was written
|
|
347
|
+
* for: the one whose type admits it, or of several, the one declaring the most of an
|
|
348
|
+
* object's members, then the fewest. Where two remain, the refusal is the oneOf's own.
|
|
349
|
+
*/
|
|
350
|
+
first(doc: string, schema: Schema, value: unknown, at: string): SchemaRefusal | undefined {
|
|
351
|
+
const found: SchemaRefusal[] = [];
|
|
352
|
+
this.check(doc, schema, value, at, found);
|
|
353
|
+
let first = found[0];
|
|
354
|
+
for (const other of found.slice(1)) {
|
|
355
|
+
if (first !== undefined && before(other, first)) {
|
|
356
|
+
first = other;
|
|
357
|
+
}
|
|
385
358
|
}
|
|
386
|
-
if (
|
|
387
|
-
|
|
359
|
+
if (first?.keyword !== "oneOf") {
|
|
360
|
+
return first;
|
|
388
361
|
}
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
path,
|
|
395
|
-
`member ${JSON.stringify(name)} is written twice, so neither value is chosen`,
|
|
362
|
+
let held = value;
|
|
363
|
+
let holders: readonly Located[] = [[doc, schema]];
|
|
364
|
+
for (const segment of pointerSegments(first.pointer.slice(at.length))) {
|
|
365
|
+
holders = holders.flatMap(([d, s]) =>
|
|
366
|
+
this.applied(d, s, held).flatMap(([d2, s2]) => this.member(d2, s2, held, segment)),
|
|
396
367
|
);
|
|
368
|
+
held = valueAt(held, segment);
|
|
397
369
|
}
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
370
|
+
for (const [d, holder] of holders) {
|
|
371
|
+
const alternatives = listOf(holder, "oneOf");
|
|
372
|
+
if (alternatives.some((each) => this.matches(d, each, held, first.pointer))) {
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
const alternative = closest(alternatives, held);
|
|
376
|
+
const inner = alternative && this.first(d, alternative, held, first.pointer);
|
|
377
|
+
if (inner !== undefined) {
|
|
378
|
+
return inner;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return first;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** The schemas checked at one place when `schema` is: it, its `$ref`, its allOf and its if branch. */
|
|
385
|
+
private applied(doc: string, schema: Schema, value: unknown): readonly Located[] {
|
|
386
|
+
const held: Located[] = [[doc, schema]];
|
|
387
|
+
if (typeof schema["$ref"] === "string") {
|
|
388
|
+
const [target, referenced] = this.resolve(doc, schema["$ref"]);
|
|
389
|
+
held.push(...this.applied(target, referenced, value));
|
|
402
390
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
throw malformed(label, path, "a member name is followed by a colon");
|
|
391
|
+
for (const each of listOf(schema, "allOf")) {
|
|
392
|
+
held.push(...this.applied(doc, each, value));
|
|
406
393
|
}
|
|
407
|
-
|
|
408
|
-
|
|
394
|
+
if (isRecord(schema["if"])) {
|
|
395
|
+
const branch = this.matches(doc, schema["if"], value, "") ? schema["then"] : schema["else"];
|
|
396
|
+
if (isRecord(branch)) {
|
|
397
|
+
held.push(...this.applied(doc, branch, value));
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
return held;
|
|
409
401
|
}
|
|
410
|
-
}
|
|
411
402
|
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
for (;;) {
|
|
417
|
-
skipSpace(cursor);
|
|
418
|
-
const ch = cursor.text.charCodeAt(cursor.at);
|
|
419
|
-
if (ch === 0x5d) {
|
|
420
|
-
cursor.at += 1;
|
|
421
|
-
return;
|
|
403
|
+
/** The schemas `schema` checks the member or the entry `segment` names in `value` with. */
|
|
404
|
+
private member(doc: string, schema: Schema, value: unknown, segment: string): readonly Located[] {
|
|
405
|
+
if (Array.isArray(value)) {
|
|
406
|
+
return isRecord(schema["items"]) ? [[doc, schema["items"]]] : [];
|
|
422
407
|
}
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
408
|
+
const properties = isRecord(schema["properties"]) ? schema["properties"] : {};
|
|
409
|
+
const declared = declaredMember(properties, segment);
|
|
410
|
+
if (isRecord(declared)) {
|
|
411
|
+
return [[doc, declared]];
|
|
412
|
+
}
|
|
413
|
+
const additional = schema["additionalProperties"];
|
|
414
|
+
return isRecord(additional) ? [[doc, additional]] : [];
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** The alternative a value matching none of them was written for, or none where none or two remain. */
|
|
419
|
+
function closest(alternatives: readonly Schema[], value: unknown): Schema | undefined {
|
|
420
|
+
const typed = alternatives.filter((each) => {
|
|
421
|
+
const types = typesOf(each);
|
|
422
|
+
return types.length === 0 || types.some((one) => hasType(value, one));
|
|
423
|
+
});
|
|
424
|
+
if (typed.length === 1 || !isRecord(value)) {
|
|
425
|
+
return typed.length === 1 ? typed[0] : undefined;
|
|
426
|
+
}
|
|
427
|
+
let best: Schema[] = [];
|
|
428
|
+
let most = -1;
|
|
429
|
+
let fewest = 0;
|
|
430
|
+
for (const alternative of typed) {
|
|
431
|
+
const properties = isRecord(alternative["properties"]) ? alternative["properties"] : {};
|
|
432
|
+
const named = Object.keys(value).filter((name) => Object.hasOwn(properties, name)).length;
|
|
433
|
+
const declared = Object.keys(properties).length;
|
|
434
|
+
if (named > most || (named === most && declared < fewest)) {
|
|
435
|
+
[best, most, fewest] = [[alternative], named, declared];
|
|
436
|
+
} else if (named === most && declared === fewest) {
|
|
437
|
+
best.push(alternative);
|
|
426
438
|
}
|
|
427
|
-
walkValue(cursor, `${at}[${index}]`, entry, label);
|
|
428
|
-
index += 1;
|
|
429
439
|
}
|
|
440
|
+
return best.length === 1 ? best[0] : undefined;
|
|
430
441
|
}
|
|
431
442
|
|
|
432
443
|
/**
|
|
433
|
-
*
|
|
434
|
-
*
|
|
435
|
-
* dotted path. The document is already known to parse.
|
|
444
|
+
* A validator over one set of schema documents, each named as a `$ref` names it, rooted
|
|
445
|
+
* at one of them: what it refuses in a value, none for an instance.
|
|
436
446
|
*/
|
|
437
|
-
export function
|
|
438
|
-
|
|
439
|
-
|
|
447
|
+
export function schemaRefusals(
|
|
448
|
+
documents: Documents,
|
|
449
|
+
root: string,
|
|
450
|
+
): (value: unknown) => readonly SchemaRefusal[] {
|
|
451
|
+
const checker = new Checker(documents);
|
|
452
|
+
const top = documents[root];
|
|
453
|
+
if (top === undefined) {
|
|
454
|
+
throw new Error(`the schema set holds no document ${root}`);
|
|
455
|
+
}
|
|
456
|
+
return (value) => {
|
|
457
|
+
const found: SchemaRefusal[] = [];
|
|
458
|
+
checker.check(root, top, value, "", found);
|
|
459
|
+
return found;
|
|
460
|
+
};
|
|
440
461
|
}
|
|
441
462
|
|
|
442
463
|
/**
|
|
443
|
-
* The
|
|
444
|
-
*
|
|
445
|
-
* naming no setting is refused.
|
|
464
|
+
* The refusal of `value` a reader fixes first under one schema, none for an instance. A
|
|
465
|
+
* `$ref` the schema holds names a place in the schema itself.
|
|
446
466
|
*/
|
|
447
|
-
export function
|
|
448
|
-
|
|
449
|
-
for (const path of Object.keys(flat).sort(bytewise)) {
|
|
450
|
-
if (!declaresSetting(path)) {
|
|
451
|
-
throw unimplementedKey(label, path);
|
|
452
|
-
}
|
|
453
|
-
const segments = path.split(".");
|
|
454
|
-
let into = nested;
|
|
455
|
-
for (const segment of segments.slice(0, -1)) {
|
|
456
|
-
const child = into[segment] ?? {};
|
|
457
|
-
if (typeof child !== "object" || Array.isArray(child)) {
|
|
458
|
-
throw malformed(label, path, `the flag document sets ${JSON.stringify(segment)} twice`);
|
|
459
|
-
}
|
|
460
|
-
into[segment] = child;
|
|
461
|
-
into = child as Record<string, unknown>;
|
|
462
|
-
}
|
|
463
|
-
const last = segments[segments.length - 1] ?? "";
|
|
464
|
-
into[last] = flat[path];
|
|
465
|
-
}
|
|
466
|
-
return nested;
|
|
467
|
+
export function firstRefusal(schema: Schema, value: unknown): SchemaRefusal | undefined {
|
|
468
|
+
return new Checker({ "": schema }).first("", schema, value, "");
|
|
467
469
|
}
|