Skip to content

Commit

Permalink
fix(core): more accurate ruleset error paths
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Apr 25, 2023
1 parent de16b4c commit 66b3ca7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
23 changes: 20 additions & 3 deletions packages/core/src/ruleset/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,27 @@ export class RulesetFunctionValidationError extends RulesetValidationError {
super(
'invalid-function-options',
RulesetFunctionValidationError.printMessage(fn, error),
error.instancePath.slice(1).split('/'),
RulesetFunctionValidationError.getPath(error),
);
}

private static getPath(error: ErrorObject): string[] {
const path: string[] = [
'functionOptions',
...(error.instancePath === '' ? [] : error.instancePath.slice(1).split('/')),
];

switch (error.keyword) {
case 'additionalProperties': {
const additionalProperty = (error as AdditionalPropertiesError).params.additionalProperty;
path.push(additionalProperty);
break;
}
}

return path;
}

private static printMessage(fn: string, error: ErrorObject): string {
switch (error.keyword) {
case 'type': {
Expand Down Expand Up @@ -157,7 +174,7 @@ export function createRulesetFunction<I, O>(
throw new RulesetValidationError(
'invalid-function-options',
`"${fn.name || '<unknown>'}" function does not accept any options`,
[],
['functionOptions'],
);
} else if (
'errors' in validateOptions &&
Expand All @@ -171,7 +188,7 @@ export function createRulesetFunction<I, O>(
throw new RulesetValidationError(
'invalid-function-options',
`"functionOptions" of "${fn.name || '<unknown>'}" function must be valid`,
[],
['functionOptions'],
);
}
};
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/ruleset/ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ export class Ruleset {
if (isPlainObject(maybeDefinition) && 'extends' in maybeDefinition) {
const { extends: _, ...def } = maybeDefinition;
// we don't want to validate extends - this is going to happen later on (line 29)
assertValidRuleset({ extends: [], ...def });
assertValidRuleset({ extends: [], ...def }, 'js');
definition = maybeDefinition as RulesetDefinition;
} else {
assertValidRuleset(maybeDefinition);
assertValidRuleset(maybeDefinition, 'js');
definition = maybeDefinition;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,12 @@ describe('JS Ruleset Validation', () => {
}),
).toThrowAggregateError(
new AggregateError([
new RulesetValidationError('undefined-function', 'Function is not defined', ['rules', 'rule', 'then']),
new RulesetValidationError('undefined-function', 'Function is not defined', [
'rules',
'rule',
'then',
'function',
]),
]),
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/ruleset/validation/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type RulesetValidationErrorCode =
| 'undefined-alias';

interface IRulesetValidationSingleError extends Pick<IDiagnostic, 'message' | 'path'> {
code: RulesetValidationErrorCode;
readonly code: RulesetValidationErrorCode;
}

export class RulesetValidationError extends Error implements IRulesetValidationSingleError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function validateFunction(
validator(opts);
} catch (ex) {
if (ex instanceof ReferenceError) {
return new RulesetValidationError('undefined-function', ex.message, toParsedPath(path));
return new RulesetValidationError('undefined-function', ex.message, [...toParsedPath(path), 'function']);
}

return wrapError(ex, path);
Expand Down

0 comments on commit 66b3ca7

Please sign in to comment.