@telorun/analyzer 0.64.0 → 0.66.0
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/analyzer.d.ts +1 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/analyzer.js +175 -65
- package/dist/call-graph.d.ts.map +1 -1
- package/dist/call-graph.js +7 -1
- package/dist/cel-environment.d.ts +12 -0
- package/dist/cel-environment.d.ts.map +1 -1
- package/dist/cel-environment.js +35 -19
- package/dist/cel-scope.d.ts.map +1 -1
- package/dist/cel-scope.js +24 -4
- package/dist/definition-registry.d.ts +47 -6
- package/dist/definition-registry.d.ts.map +1 -1
- package/dist/definition-registry.js +82 -13
- package/dist/eval-paths.d.ts +68 -0
- package/dist/eval-paths.d.ts.map +1 -1
- package/dist/eval-paths.js +138 -0
- package/dist/flatten-for-analyzer.d.ts +1 -1
- package/dist/flatten-for-analyzer.js +1 -1
- package/dist/import-resolution-diagnostics.d.ts +1 -1
- package/dist/import-resolution-diagnostics.js +1 -1
- package/dist/index.d.ts +13 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -6
- package/dist/manifest-visitor.d.ts.map +1 -1
- package/dist/manifest-visitor.js +8 -1
- package/dist/migrations/entries/index.d.ts.map +1 -1
- package/dist/migrations/entries/index.js +2 -0
- package/dist/migrations/entries/schema-prepare-bucket.json +27 -0
- package/dist/peer-binding.d.ts +232 -0
- package/dist/peer-binding.d.ts.map +1 -0
- package/dist/peer-binding.js +418 -0
- package/dist/ref-slot.d.ts +13 -0
- package/dist/ref-slot.d.ts.map +1 -1
- package/dist/ref-slot.js +26 -0
- package/dist/reference-field-map.d.ts +28 -0
- package/dist/reference-field-map.d.ts.map +1 -1
- package/dist/reference-field-map.js +25 -0
- package/dist/referrer-rule.d.ts +17 -0
- package/dist/referrer-rule.d.ts.map +1 -1
- package/dist/referrer-rule.js +4 -1
- package/dist/release/index.d.ts +1 -1
- package/dist/release/index.js +1 -1
- package/dist/resource-rule.d.ts +40 -27
- package/dist/resource-rule.d.ts.map +1 -1
- package/dist/resource-rule.js +61 -7
- package/dist/rule-condition.d.ts +11 -0
- package/dist/rule-condition.d.ts.map +1 -1
- package/dist/rule-condition.js +13 -0
- package/dist/schema-compat.d.ts +59 -22
- package/dist/schema-compat.d.ts.map +1 -1
- package/dist/schema-compat.js +60 -75
- package/dist/schema-error-report.d.ts +68 -0
- package/dist/schema-error-report.d.ts.map +1 -0
- package/dist/schema-error-report.js +356 -0
- package/dist/schema-projection.d.ts +106 -4
- package/dist/schema-projection.d.ts.map +1 -1
- package/dist/schema-projection.js +206 -18
- package/dist/telo-version.d.ts +1 -1
- package/dist/telo-version.js +1 -1
- package/dist/validate-cel-context.d.ts +1 -17
- package/dist/validate-cel-context.d.ts.map +1 -1
- package/dist/validate-cel-context.js +5 -70
- package/dist/validate-nested-inline.d.ts +22 -1
- package/dist/validate-nested-inline.d.ts.map +1 -1
- package/dist/validate-nested-inline.js +17 -9
- package/dist/validate-reference-forms.d.ts +10 -0
- package/dist/validate-reference-forms.d.ts.map +1 -1
- package/dist/validate-reference-forms.js +23 -0
- package/dist/validate-references.d.ts.map +1 -1
- package/dist/validate-references.js +9 -2
- package/dist/validate-referrer-rules.d.ts +38 -4
- package/dist/validate-referrer-rules.d.ts.map +1 -1
- package/dist/validate-referrer-rules.js +139 -31
- package/dist/validate-resource-rules.d.ts +11 -2
- package/dist/validate-resource-rules.d.ts.map +1 -1
- package/dist/validate-resource-rules.js +18 -12
- package/dist/validate-schema-projection.d.ts.map +1 -1
- package/dist/validate-schema-projection.js +33 -2
- package/dist/validate-step-inputs.js +11 -3
- package/dist/with-synthetic-positions.d.ts +1 -1
- package/dist/with-synthetic-positions.js +1 -1
- package/package.json +3 -3
- package/src/analyzer.ts +211 -68
- package/src/call-graph.ts +7 -1
- package/src/cel-environment.ts +48 -19
- package/src/cel-scope.ts +24 -6
- package/src/definition-registry.ts +90 -11
- package/src/eval-paths.ts +152 -0
- package/src/flatten-for-analyzer.ts +1 -1
- package/src/import-resolution-diagnostics.ts +1 -1
- package/src/index.ts +55 -6
- package/src/manifest-visitor.ts +8 -1
- package/src/migrations/entries/index.ts +2 -0
- package/src/migrations/entries/schema-prepare-bucket.json +27 -0
- package/src/peer-binding.ts +641 -0
- package/src/ref-slot.ts +36 -0
- package/src/reference-field-map.ts +42 -0
- package/src/referrer-rule.ts +20 -1
- package/src/release/index.ts +1 -1
- package/src/resource-rule.ts +69 -7
- package/src/rule-condition.ts +15 -0
- package/src/schema-compat.ts +92 -79
- package/src/schema-error-report.ts +417 -0
- package/src/schema-projection.ts +283 -20
- package/src/telo-version.ts +1 -1
- package/src/validate-cel-context.ts +6 -71
- package/src/validate-nested-inline.ts +35 -14
- package/src/validate-reference-forms.ts +22 -0
- package/src/validate-references.ts +15 -2
- package/src/validate-referrer-rules.ts +215 -28
- package/src/validate-resource-rules.ts +26 -12
- package/src/validate-schema-projection.ts +50 -1
- package/src/validate-step-inputs.ts +10 -4
- package/src/with-synthetic-positions.ts +1 -1
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one renderer for AJV validation failures.
|
|
3
|
+
*
|
|
4
|
+
* Browser-safe and re-imported by the kernel — the split `buildEvalPaths` and
|
|
5
|
+
* the redaction path parser already use — so a failure is phrased identically
|
|
6
|
+
* under `telo check` and at runtime. Three implementations used to answer this
|
|
7
|
+
* (the analyzer's keyword prose, the kernel's raw `instancePath + message`
|
|
8
|
+
* join, and observed state's own inline variant), so a developer who fixed what
|
|
9
|
+
* the analyzer told them met a different sentence describing the same thing.
|
|
10
|
+
*
|
|
11
|
+
* UNION REDUCTION is the second half. A union must attempt every branch, and
|
|
12
|
+
* AJV cannot know which one was intended — `discriminator: true` works only
|
|
13
|
+
* against an explicit OpenAPI-style discriminator property, which would mean
|
|
14
|
+
* changing what every module's authors write. So branch selection is a
|
|
15
|
+
* reporting concern and lives here.
|
|
16
|
+
*
|
|
17
|
+
* It narrows the error SET, never just the sentence: every consumer maps the
|
|
18
|
+
* surviving errors to manifest paths to anchor a diagnostic, so reducing at the
|
|
19
|
+
* prose layer alone would move the soup out of the message and into the
|
|
20
|
+
* problems list, one entry per branch on a different line.
|
|
21
|
+
*
|
|
22
|
+
* Selection is made from the ERRORS ALONE, never from the schema. A branch
|
|
23
|
+
* whose discriminating key is present emits no complaint at the union's own
|
|
24
|
+
* instancePath; one whose key is absent says `required`, and one that forbids a
|
|
25
|
+
* key the value carries says `additionalProperties`. That is the whole signal,
|
|
26
|
+
* and reading it off the errors is what lets reduction work across a `$ref`
|
|
27
|
+
* into another registered schema, where navigating to the branch subschema
|
|
28
|
+
* would mean re-implementing AJV's resolution.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/** An AJV error object. Structurally typed — the analyzer and the kernel hand
|
|
32
|
+
* over errors from their own AJV instances. */
|
|
33
|
+
export interface AjvErrorLike {
|
|
34
|
+
keyword?: string;
|
|
35
|
+
instancePath?: string;
|
|
36
|
+
schemaPath?: string;
|
|
37
|
+
message?: string;
|
|
38
|
+
params?: Record<string, any>;
|
|
39
|
+
data?: unknown;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** A schema validation issue with a dotted-path pointer to the offending field. */
|
|
43
|
+
export interface SchemaIssue {
|
|
44
|
+
message: string;
|
|
45
|
+
/** Dotted path to the field (e.g. "config.handler"). Empty string means root. */
|
|
46
|
+
path: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const UNION_KEYWORDS = new Set(["anyOf", "oneOf"]);
|
|
50
|
+
|
|
51
|
+
/** Keywords a branch raises at the union's OWN instancePath when the value is
|
|
52
|
+
* not of that branch's shape at all — as opposed to being that shape and wrong
|
|
53
|
+
* further in. These are what make a branch implausible. */
|
|
54
|
+
const SHAPE_KEYWORDS = new Set(["required", "type", "additionalProperties", "enum", "const"]);
|
|
55
|
+
|
|
56
|
+
/* ------------------------------------------------------------------ prose */
|
|
57
|
+
|
|
58
|
+
export function formatSingleError(err: AjvErrorLike): string {
|
|
59
|
+
const p = err.instancePath || "/";
|
|
60
|
+
const params = err.params ?? {};
|
|
61
|
+
switch (err.keyword) {
|
|
62
|
+
case "additionalProperties":
|
|
63
|
+
return `${p} must NOT have additional properties ('${params.additionalProperty}' is not allowed)`;
|
|
64
|
+
case "required":
|
|
65
|
+
return `${p} is missing required property '${params.missingProperty}'`;
|
|
66
|
+
case "enum":
|
|
67
|
+
return `${p} ${err.message ?? "is invalid"} (${(params.allowedValues as unknown[])?.join(" | ")})`;
|
|
68
|
+
case "type":
|
|
69
|
+
return `${p} must be ${params.type}${describeActual(err)}`;
|
|
70
|
+
default:
|
|
71
|
+
return `${p} ${err.message ?? "is invalid"}`;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** ` (got string)`, or nothing when the value is not in hand. AJV carries
|
|
76
|
+
* `data` only under `verbose`, and a reducer that navigated the root value
|
|
77
|
+
* would have to be given it at every call site; an absent actual type is worth
|
|
78
|
+
* less than a wrong one. */
|
|
79
|
+
function describeActual(err: AjvErrorLike): string {
|
|
80
|
+
if (!("data" in err)) return "";
|
|
81
|
+
const d = err.data;
|
|
82
|
+
if (d === null) return " (got null)";
|
|
83
|
+
if (Array.isArray(d)) return " (got array)";
|
|
84
|
+
return ` (got ${typeof d})`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/* -------------------------------------------------------------- reduction */
|
|
88
|
+
|
|
89
|
+
/** The branch index a `schemaPath` sits under, for a union whose own schemaPath
|
|
90
|
+
* is `unionPath` (`…/anyOf`): a child is `…/anyOf/<i>/…` and nothing else can
|
|
91
|
+
* collide with it. */
|
|
92
|
+
function branchIndexUnder(unionPath: string, schemaPath: string | undefined): number | undefined {
|
|
93
|
+
if (!schemaPath || !schemaPath.startsWith(unionPath + "/")) return undefined;
|
|
94
|
+
const rest = schemaPath.slice(unionPath.length + 1);
|
|
95
|
+
const slash = rest.indexOf("/");
|
|
96
|
+
const head = slash === -1 ? rest : rest.slice(0, slash);
|
|
97
|
+
const index = Number(head);
|
|
98
|
+
return Number.isInteger(index) ? index : undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function instanceDepth(path: string | undefined): number {
|
|
102
|
+
if (!path) return 0;
|
|
103
|
+
return path.split("/").filter((s) => s !== "").length;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* How the alternatives at a union node are described, one phrase each.
|
|
108
|
+
*
|
|
109
|
+
* Read off the complaints made at the union's own node, and deliberately ONE
|
|
110
|
+
* PHRASE PER MISSING KEY rather than one per error group. AJV inlines most
|
|
111
|
+
* `$ref` branches and reports them all under the same bare `schemaPath`, so
|
|
112
|
+
* several branches are genuinely indistinguishable in the error set — joining
|
|
113
|
+
* their keys into a single phrase would read as one alternative demanding all
|
|
114
|
+
* of them, which is a claim about the schema that is simply false. Listing them
|
|
115
|
+
* separately under-specifies a branch that requires two keys at once, and each
|
|
116
|
+
* clause is still a true necessary condition; asserting a conjunction that does
|
|
117
|
+
* not exist is not.
|
|
118
|
+
*/
|
|
119
|
+
function describeAlternatives(errors: AjvErrorLike[], unionInstancePath: string): string[] {
|
|
120
|
+
const own = errors.filter((e) => (e.instancePath || "") === unionInstancePath);
|
|
121
|
+
const phrases: string[] = [];
|
|
122
|
+
for (const e of own) {
|
|
123
|
+
if (e.keyword === "required") phrases.push(`one with '${e.params?.missingProperty}'`);
|
|
124
|
+
else if (e.keyword === "type") phrases.push(`a ${e.params?.type}`);
|
|
125
|
+
else if (e.keyword === "enum") {
|
|
126
|
+
phrases.push(`one of ${(e.params?.allowedValues as unknown[])?.join(" | ")}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return phrases.length > 0 ? phrases : ["another shape"];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Is this branch a plausible reading of the value — does it accept the value's
|
|
133
|
+
* shape at the union node itself, and only disagree further in? */
|
|
134
|
+
function isPlausible(errors: AjvErrorLike[], unionInstancePath: string): boolean {
|
|
135
|
+
return !errors.some(
|
|
136
|
+
(e) => (e.instancePath || "") === unionInstancePath && SHAPE_KEYWORDS.has(e.keyword ?? ""),
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* A union OCCURRENCE — one union node reached at one place in the value.
|
|
142
|
+
*
|
|
143
|
+
* `schemaPath` alone does not identify one. A self-recursive shape (`$ref` back
|
|
144
|
+
* to the carrier root, which is how a container node holds children) reaches the
|
|
145
|
+
* SAME union schema at every depth, so every level's errors carry the identical
|
|
146
|
+
* `#/anyOf/<i>/…`. What separates them is `instancePath`, and an occurrence is
|
|
147
|
+
* therefore the pair.
|
|
148
|
+
*/
|
|
149
|
+
interface Occurrence {
|
|
150
|
+
error: AjvErrorLike;
|
|
151
|
+
schemaPath: string;
|
|
152
|
+
instancePath: string;
|
|
153
|
+
/** Errors this occurrence's own branches raised. */
|
|
154
|
+
owned: AjvErrorLike[];
|
|
155
|
+
/** Occurrences reached THROUGH one of this one's branches. */
|
|
156
|
+
children: Occurrence[];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function isUnder(child: string, parent: string): boolean {
|
|
160
|
+
return parent === "" ? child !== "" : child.startsWith(parent + "/");
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** The value path one level up, or undefined at the root. `""` is the root, so
|
|
164
|
+
* a non-empty path with no separator has the root as its parent. */
|
|
165
|
+
function parentPath(path: string): string | undefined {
|
|
166
|
+
if (path === "") return undefined;
|
|
167
|
+
const cut = path.lastIndexOf("/");
|
|
168
|
+
return cut <= 0 ? "" : path.slice(0, cut);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Replace each failing union with the errors of the branch the author plainly
|
|
173
|
+
* meant, recursively, outside in.
|
|
174
|
+
*
|
|
175
|
+
* Attribution runs to the DEEPEST occurrence that could own an error, which is
|
|
176
|
+
* what keeps a container's own complaint apart from its child's when both carry
|
|
177
|
+
* the same `schemaPath`. An occurrence reached through a branch becomes a
|
|
178
|
+
* candidate branch of its own — it raised nothing at the parent's node, so it is
|
|
179
|
+
* plausible exactly when the value really did take that shape and fail further
|
|
180
|
+
* in, and reducing it recursively is what stops an inner union's alternatives
|
|
181
|
+
* from surviving inside the outer one's selection.
|
|
182
|
+
*/
|
|
183
|
+
export function reduceSchemaErrors(errors: AjvErrorLike[] | null | undefined): AjvErrorLike[] {
|
|
184
|
+
if (!errors || errors.length === 0) return [];
|
|
185
|
+
|
|
186
|
+
const occurrences: Occurrence[] = errors
|
|
187
|
+
.filter((e) => UNION_KEYWORDS.has(e.keyword ?? "") && typeof e.schemaPath === "string")
|
|
188
|
+
.map((e) => ({
|
|
189
|
+
error: e,
|
|
190
|
+
schemaPath: e.schemaPath!,
|
|
191
|
+
instancePath: e.instancePath || "",
|
|
192
|
+
owned: [],
|
|
193
|
+
children: [],
|
|
194
|
+
}));
|
|
195
|
+
if (occurrences.length === 0) return errors;
|
|
196
|
+
|
|
197
|
+
// The VALUE NODE is the claim, not the schemaPath. A branch written as a
|
|
198
|
+
// `$ref` is reported by AJV under the TARGET's schemaPath — and AJV inlines
|
|
199
|
+
// some of them, reporting several branches under one identical path — so
|
|
200
|
+
// nothing in such an error points back at the union that dispatched to it. A
|
|
201
|
+
// large union is written exactly that way, a branch per `$defs` entry, so
|
|
202
|
+
// claiming by schemaPath alone would leave the biggest unions unreduced.
|
|
203
|
+
//
|
|
204
|
+
// Indexed by instancePath rather than scanned: an error is claimed by the
|
|
205
|
+
// DEEPEST occurrence enclosing it, which is found by walking that error's own
|
|
206
|
+
// path upwards — bounded by the path's depth instead of by the number of
|
|
207
|
+
// unions. The scan this replaced was O(errors × occurrences), and both grow
|
|
208
|
+
// with nesting depth on a recursive shape, on a path the editor runs per
|
|
209
|
+
// keystroke.
|
|
210
|
+
const byPath = new Map<string, Occurrence>();
|
|
211
|
+
const isOccurrence = new Set<AjvErrorLike>();
|
|
212
|
+
for (const o of occurrences) {
|
|
213
|
+
isOccurrence.add(o.error);
|
|
214
|
+
// Several unions can occur at ONE value node (a union inside a union
|
|
215
|
+
// branch); the first is kept, and the rest nest under it below.
|
|
216
|
+
if (!byPath.has(o.instancePath)) byPath.set(o.instancePath, o);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** The nearest occurrence at or above `path`, excluding `path` itself when
|
|
220
|
+
* `strict` — which is how an occurrence finds its parent rather than itself. */
|
|
221
|
+
const enclosing = (path: string, strict: boolean): Occurrence | undefined => {
|
|
222
|
+
let current = strict ? parentPath(path) : path;
|
|
223
|
+
while (current !== undefined) {
|
|
224
|
+
const hit = byPath.get(current);
|
|
225
|
+
if (hit) return hit;
|
|
226
|
+
current = parentPath(current);
|
|
227
|
+
}
|
|
228
|
+
return undefined;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const owner = new Map<AjvErrorLike, Occurrence>();
|
|
232
|
+
for (const err of errors) {
|
|
233
|
+
if (isOccurrence.has(err)) continue;
|
|
234
|
+
const best = enclosing(err.instancePath || "", false);
|
|
235
|
+
if (best) {
|
|
236
|
+
best.owned.push(err);
|
|
237
|
+
owner.set(err, best);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Nest occurrences the same way: an occurrence deeper in the value was reached
|
|
242
|
+
// through some branch of the nearest one enclosing it.
|
|
243
|
+
const roots: Occurrence[] = [];
|
|
244
|
+
for (const o of occurrences) {
|
|
245
|
+
const parent = o === byPath.get(o.instancePath)
|
|
246
|
+
? enclosing(o.instancePath, true)
|
|
247
|
+
: byPath.get(o.instancePath);
|
|
248
|
+
if (parent && parent !== o) parent.children.push(o);
|
|
249
|
+
else roots.push(o);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const replaced = new Map<AjvErrorLike, AjvErrorLike[]>();
|
|
253
|
+
for (const root of roots) replaced.set(root.error, resolveOccurrence(root));
|
|
254
|
+
|
|
255
|
+
const out: AjvErrorLike[] = [];
|
|
256
|
+
for (const err of errors) {
|
|
257
|
+
const replacement = replaced.get(err);
|
|
258
|
+
if (replacement) {
|
|
259
|
+
out.push(...replacement);
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
// Everything an occurrence owns is spoken for by whichever branch survived,
|
|
263
|
+
// and a nested occurrence is carried inside its parent's selection.
|
|
264
|
+
if (owner.has(err)) continue;
|
|
265
|
+
if (occurrences.some((o) => o.error === err)) continue;
|
|
266
|
+
out.push(err);
|
|
267
|
+
}
|
|
268
|
+
return out;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
interface Branch {
|
|
272
|
+
/** Declaration order, or `Infinity` for a nested occurrence, which has none. */
|
|
273
|
+
index: number;
|
|
274
|
+
errors: AjvErrorLike[];
|
|
275
|
+
/** True when this candidate is a nested occurrence rather than a branch that
|
|
276
|
+
* complained here — it is already reduced, so it is not reduced again. */
|
|
277
|
+
nested: boolean;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Groups one union's complaints into candidate readings of the value.
|
|
281
|
+
*
|
|
282
|
+
* The branch INDEX is used wherever the error carries it. It does not when the
|
|
283
|
+
* branch is a `$ref` — AJV reports under the target's schemaPath — so the
|
|
284
|
+
* fallback groups by the VALUE NODE each complaint is about: everything said
|
|
285
|
+
* about the union node itself is one candidate (those are the branches that
|
|
286
|
+
* rejected the value's shape outright), and each child node complained about is
|
|
287
|
+
* its own. That is the same question asked of the data instead of the schema,
|
|
288
|
+
* and it is what the ordering below actually reads. */
|
|
289
|
+
function groupCandidates(occurrence: Occurrence): Branch[] {
|
|
290
|
+
const byIndex = new Map<number, AjvErrorLike[]>();
|
|
291
|
+
const byNode = new Map<string, AjvErrorLike[]>();
|
|
292
|
+
for (const err of occurrence.owned) {
|
|
293
|
+
const index = branchIndexUnder(occurrence.schemaPath, err.schemaPath);
|
|
294
|
+
const bucket =
|
|
295
|
+
index === undefined
|
|
296
|
+
? mapBucket(byNode, childSegment(err.instancePath || "", occurrence.instancePath))
|
|
297
|
+
: mapBucket(byIndex, index);
|
|
298
|
+
bucket.push(err);
|
|
299
|
+
}
|
|
300
|
+
return [...byIndex]
|
|
301
|
+
.map(([index, errs]) => ({ index, errors: errs, nested: false }))
|
|
302
|
+
.concat([...byNode].map(([, errs]) => ({ index: Number.MAX_SAFE_INTEGER, errors: errs, nested: false })))
|
|
303
|
+
.concat(
|
|
304
|
+
occurrence.children.map((child) => ({
|
|
305
|
+
index: Number.POSITIVE_INFINITY,
|
|
306
|
+
errors: resolveOccurrence(child),
|
|
307
|
+
nested: true,
|
|
308
|
+
})),
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function mapBucket<K>(map: Map<K, AjvErrorLike[]>, key: K): AjvErrorLike[] {
|
|
313
|
+
const existing = map.get(key);
|
|
314
|
+
if (existing) return existing;
|
|
315
|
+
const created: AjvErrorLike[] = [];
|
|
316
|
+
map.set(key, created);
|
|
317
|
+
return created;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** The first value-path segment below `parent`, or "" for the node itself. */
|
|
321
|
+
function childSegment(instancePath: string, parent: string): string {
|
|
322
|
+
if (!isUnder(instancePath, parent)) return "";
|
|
323
|
+
const rest = instancePath.slice(parent.length + 1);
|
|
324
|
+
const slash = rest.indexOf("/");
|
|
325
|
+
return slash === -1 ? rest : rest.slice(0, slash);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function resolveOccurrence(occurrence: Occurrence): AjvErrorLike[] {
|
|
329
|
+
const candidates = groupCandidates(occurrence);
|
|
330
|
+
|
|
331
|
+
// `oneOf` matching SEVERAL branches emits the union error with no branch
|
|
332
|
+
// errors at all — nothing was rejected, so there is no branch to select.
|
|
333
|
+
if (candidates.length === 0) return [occurrence.error];
|
|
334
|
+
|
|
335
|
+
const plausible = candidates.filter(
|
|
336
|
+
(c) => c.nested || isPlausible(c.errors, occurrence.instancePath),
|
|
337
|
+
);
|
|
338
|
+
if (plausible.length === 0) {
|
|
339
|
+
return [alternativesError(candidates, occurrence)];
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Deepest agreement first — a branch that matched further into the value is
|
|
343
|
+
// the one the author was writing — then the fewest complaints, then the
|
|
344
|
+
// declaration order, so the choice is stable.
|
|
345
|
+
plausible.sort((a, b) => {
|
|
346
|
+
const depth = maxDepth(b.errors) - maxDepth(a.errors);
|
|
347
|
+
if (depth !== 0) return depth;
|
|
348
|
+
if (a.errors.length !== b.errors.length) return a.errors.length - b.errors.length;
|
|
349
|
+
return a.index - b.index;
|
|
350
|
+
});
|
|
351
|
+
const winner = plausible[0];
|
|
352
|
+
return winner.nested ? winner.errors : reduceSchemaErrors(winner.errors);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function maxDepth(errors: AjvErrorLike[]): number {
|
|
356
|
+
let max = 0;
|
|
357
|
+
for (const e of errors) max = Math.max(max, instanceDepth(e.instancePath));
|
|
358
|
+
return max;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** One error anchored at the union node, listing what could have gone there.
|
|
362
|
+
* The honest fallback: a confident wrong message is worse than the
|
|
363
|
+
* concatenation this replaces, so when no branch is a plausible reading the
|
|
364
|
+
* reader is told what the alternatives are rather than shown one branch's
|
|
365
|
+
* complaints as if it were the intended one. */
|
|
366
|
+
function alternativesError(candidates: Branch[], occurrence: Occurrence): AjvErrorLike {
|
|
367
|
+
const seen = new Set<string>();
|
|
368
|
+
const described: string[] = [];
|
|
369
|
+
for (const text of describeAlternatives(
|
|
370
|
+
candidates.flatMap((c) => c.errors),
|
|
371
|
+
occurrence.instancePath,
|
|
372
|
+
)) {
|
|
373
|
+
if (seen.has(text)) continue;
|
|
374
|
+
seen.add(text);
|
|
375
|
+
described.push(text);
|
|
376
|
+
}
|
|
377
|
+
return {
|
|
378
|
+
...occurrence.error,
|
|
379
|
+
instancePath: occurrence.instancePath,
|
|
380
|
+
message: `matches no alternative — expected ${described.join(", or ")}`,
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/* --------------------------------------------------------------- rendering */
|
|
385
|
+
|
|
386
|
+
/** Converts an AJV error to a dotted path compatible with PositionIndex keys.
|
|
387
|
+
* e.g. instancePath "/config/routes/0/handler" → "config.routes[0].handler"
|
|
388
|
+
* For "required" keyword errors, appends the missing property to the parent path. */
|
|
389
|
+
export function ajvErrorToPath(err: AjvErrorLike): string {
|
|
390
|
+
const instancePath = err.instancePath ?? "";
|
|
391
|
+
const parts = instancePath.split("/").filter((p) => p !== "");
|
|
392
|
+
let result = "";
|
|
393
|
+
for (const part of parts) {
|
|
394
|
+
if (/^\d+$/.test(part)) result += `[${part}]`;
|
|
395
|
+
else result += result ? `.${part}` : part;
|
|
396
|
+
}
|
|
397
|
+
if (err.keyword === "required" && err.params?.missingProperty) {
|
|
398
|
+
const missing = err.params.missingProperty as string;
|
|
399
|
+
result += result ? `.${missing}` : missing;
|
|
400
|
+
}
|
|
401
|
+
return result;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** Reduced, path-anchored issues — what a diagnostic list is built from. */
|
|
405
|
+
export function schemaIssues(errors: AjvErrorLike[] | null | undefined): SchemaIssue[] {
|
|
406
|
+
return reduceSchemaErrors(errors).map((err) => ({
|
|
407
|
+
message: formatSingleError(err),
|
|
408
|
+
path: ajvErrorToPath(err),
|
|
409
|
+
}));
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** Reduced, rendered as one sentence — what a thrown runtime error carries. */
|
|
413
|
+
export function formatAjvErrors(errors: AjvErrorLike[] | null | undefined): string {
|
|
414
|
+
const reduced = reduceSchemaErrors(errors);
|
|
415
|
+
if (reduced.length === 0) return "Unknown schema error";
|
|
416
|
+
return reduced.map(formatSingleError).join("; ");
|
|
417
|
+
}
|