Skip to content

Commit ec65191

Browse files
committed
refactor: adjust rule details format in generated markdown report
Rather than generating one big table, with lots of wide columns, this generates one table per violation to be more extendable with new fields. It also ends up being more consistent with how the data is displayed in the CLI output. Signed-off-by: Dustin Popp <[email protected]>
1 parent b5ace72 commit ec65191

File tree

2 files changed

+52
-27
lines changed

2 files changed

+52
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
/**
2-
* Copyright 2024 IBM Corporation.
2+
* Copyright 2024 - 2025 IBM Corporation.
33
* SPDX-License-Identifier: Apache2.0
44
*/
55

66
const MarkdownTable = require('../markdown-table');
77

8-
function getTable({ error, warning }) {
9-
const table = new MarkdownTable(
10-
'Rule',
11-
'Message',
12-
'Path',
13-
'Line',
14-
'Severity'
15-
);
8+
function getTables(violations) {
9+
let tableOutput = '';
10+
for (const severity of ['error', 'warning']) {
11+
for (const { message, path, rule, line } of violations[severity].results) {
12+
const table = new MarkdownTable('Rule', rule);
13+
table.addRow('Message', message);
14+
table.addRow('Path', path.join('.'));
15+
table.addRow('Line', line);
16+
table.addRow('Severity', severity);
1617

17-
error.results.forEach(({ message, path, rule, line }) => {
18-
table.addRow(rule, message, path.join('.'), line, 'error');
19-
});
18+
tableOutput += `${table.render()}\n\n`;
19+
}
20+
}
2021

21-
warning.results.forEach(({ message, path, rule, line }) => {
22-
table.addRow(rule, message, path.join('.'), line, 'warning');
23-
});
24-
25-
return table.render();
22+
// Remove the final newline characters from the string.
23+
return tableOutput.trim();
2624
}
2725

28-
module.exports = getTable;
26+
module.exports = getTables;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2024 IBM Corporation.
2+
* Copyright 2024 - 2025 IBM Corporation.
33
* SPDX-License-Identifier: Apache2.0
44
*/
55

@@ -10,17 +10,44 @@ describe('ruleViolationDetails table tests', function () {
1010
it('should produce a table with all rule violations from the results', function () {
1111
const tableRows = ruleViolationDetails(validatorResults).split('\n');
1212

13-
expect(tableRows).toHaveLength(5);
14-
expect(tableRows[0]).toBe('| Rule | Message | Path | Line | Severity |');
15-
expect(tableRows[1]).toBe('| --- | --- | --- | --- | --- |');
13+
expect(tableRows).toHaveLength(20);
14+
15+
// First rule.
16+
expect(tableRows[0]).toBe(
17+
'| Rule | ibm-no-consecutive-path-parameter-segments |'
18+
);
19+
expect(tableRows[1]).toBe('| --- | --- |');
1620
expect(tableRows[2]).toBe(
17-
'| ibm-no-consecutive-path-parameter-segments | Path contains two or more consecutive path parameter references: /pets/{pet_id}/{id} | paths./pets/{pet_id}/{id} | 84 | error |'
21+
'| Message | Path contains two or more consecutive path parameter references: /pets/{pet_id}/{id} |'
22+
);
23+
expect(tableRows[3]).toBe('| Path | paths./pets/{pet_id}/{id} |');
24+
expect(tableRows[4]).toBe('| Line | 84 |');
25+
expect(tableRows[5]).toBe('| Severity | error |');
26+
expect(tableRows[6]).toBe('');
27+
28+
// Second rule.
29+
expect(tableRows[7]).toBe('| Rule | ibm-integer-attributes |');
30+
expect(tableRows[8]).toBe('| --- | --- |');
31+
expect(tableRows[9]).toBe(
32+
`| Message | Integer schemas should define property 'minimum' |`
33+
);
34+
expect(tableRows[10]).toBe(
35+
'| Path | components.schemas.Pet.properties.id |'
1836
);
19-
expect(tableRows[3]).toBe(
20-
"| ibm-integer-attributes | Integer schemas should define property 'minimum' | components.schemas.Pet.properties.id | 133 | error |"
37+
expect(tableRows[11]).toBe('| Line | 133 |');
38+
expect(tableRows[12]).toBe('| Severity | error |');
39+
expect(tableRows[13]).toBe('');
40+
41+
// Third rule.
42+
expect(tableRows[14]).toBe('| Rule | ibm-anchored-patterns |');
43+
expect(tableRows[15]).toBe('| --- | --- |');
44+
expect(tableRows[16]).toBe(
45+
`| Message | A regular expression used in a 'pattern' attribute should be anchored with ^ and $ |`
2146
);
22-
expect(tableRows[4]).toBe(
23-
"| ibm-anchored-patterns | A regular expression used in a 'pattern' attribute should be anchored with ^ and $ | components.schemas.Error.properties.message.pattern | 233 | warning |"
47+
expect(tableRows[17]).toBe(
48+
'| Path | components.schemas.Error.properties.message.pattern |'
2449
);
50+
expect(tableRows[18]).toBe('| Line | 233 |');
51+
expect(tableRows[19]).toBe('| Severity | warning |');
2552
});
2653
});

0 commit comments

Comments
 (0)