Skip to content

Commit baf91ee

Browse files
kopernic-plclaude
andcommitted
fix(core): replace unsound AggregateError cast with runtime narrowing in flatErrors
The previous cast to (RulesetValidationError | AggregateError)[] was unsound — AggregateError.errors is untyped by spec and can hold arbitrary values. Rewrite flatErrors to accept unknown, narrow with instanceof/ isAggregateError at runtime, and always return RulesetValidationError[]. This eliminates the cast, drops the Array.isArray branch in the caller, and silently discards unexpected entries rather than propagating them typed as RulesetValidationError. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c90f1cb commit baf91ee

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

  • packages/core/src/ruleset/validation

packages/core/src/ruleset/validation/errors.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ export function convertAjvErrors(
9494

9595
return filteredErrors.flatMap(error => {
9696
if (error.keyword === 'x-spectral-runtime') {
97-
const flat = flatErrors(error.params.errors);
98-
const list = Array.isArray(flat) ? flat : [flat];
99-
return list.map(e => enrichWithLocation(e, sourceContext));
97+
return flatErrors(error.params.errors).map(e => enrichWithLocation(e, sourceContext));
10098
}
10199

102100
const path = error.instancePath.slice(1).split('/');
@@ -109,12 +107,16 @@ export function convertAjvErrors(
109107
});
110108
}
111109

112-
function flatErrors(error: RulesetValidationError | AggregateError): RulesetValidationError | RulesetValidationError[] {
110+
function flatErrors(error: unknown): RulesetValidationError[] {
113111
if (isAggregateError(error)) {
114-
return (error.errors as (RulesetValidationError | AggregateError)[]).flatMap(flatErrors);
112+
return error.errors.flatMap(flatErrors);
115113
}
116114

117-
return error;
115+
if (error instanceof RulesetValidationError) {
116+
return [error];
117+
}
118+
119+
return [];
118120
}
119121

120122
function resolveLocation(

0 commit comments

Comments
 (0)