Skip to content

Commit

Permalink
Merge pull request #325 from lbulanti-ms/issue-315
Browse files Browse the repository at this point in the history
issue-315 - sorting validate command output to print errors first
  • Loading branch information
lbulanti-ms authored Aug 2, 2024
2 parents 6810bcb + cdc2f5f commit 37c6705
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
34 changes: 33 additions & 1 deletion cli/src/commands/validate/validate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,41 @@ describe('validate', () => {

const {
formatSpectralOutput,
formatJsonSchemaOutput
formatJsonSchemaOutput,
sortSpectralIssueBySeverity
} = exportedForTesting;

describe('sortSpectralIssueBySeverity', () => {

it('should sort the spectral issues based on the severity', () => {
const givenFirstError = buildISpectralDiagnostic('error-code-1', 'This is the first error', 0);
const givenFirstWarning = buildISpectralDiagnostic('warning-code-1', 'This is the first warning', 1);
const givenSecondWarning = buildISpectralDiagnostic('warning-code-2', 'This is the second warning', 1);
const givenSecondError = buildISpectralDiagnostic('error-code-2', 'This is the second error', 0);
const givenNotSortedSpectralIssues: ISpectralDiagnostic[] = [ givenFirstError, givenFirstWarning, givenSecondWarning, givenSecondError];
sortSpectralIssueBySeverity(givenNotSortedSpectralIssues);
const expectedSortedSpectralIssue: ISpectralDiagnostic[] = [givenFirstError, givenSecondError, givenFirstWarning, givenSecondWarning];
expect(givenNotSortedSpectralIssues).toStrictEqual(expectedSortedSpectralIssue);
});
});

function buildISpectralDiagnostic(code: string, message: string, severity: number): ISpectralDiagnostic{
return {
code: code,
message: message,
severity: severity,
path: [
'relationships',
'0',
'relationship-type',
'connects',
'destination',
'interface'
],
range: { start: { line: 1, character: 1 }, end: { line: 2, character: 1 } }
};
}

describe('formatSpectralOutput', () => {

it('should convert the spectral output to the ValidationOutput format', () => {
Expand Down
13 changes: 11 additions & 2 deletions cli/src/commands/validate/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default async function validate(
logger.debug(`JSON Schema validation raw output: ${prettifyJson(validateSchema.errors)}`);
errors = true;
jsonSchemaValidations = formatJsonSchemaOutput(validateSchema.errors);
validations = validations.concat(jsonSchemaValidations);
validations = jsonSchemaValidations.concat(validations);
}

const validationsOutput = getFormattedOutput(validations, format, spectralRulesetForInstantiation, spectralRulesetForPattern, jsonSchemaValidations, spectralResult);
Expand Down Expand Up @@ -158,6 +158,7 @@ async function runSpectralValidations(

if (issues && issues.length > 0) {
logger.debug(`Spectral raw output: ${prettifyJson(issues)}`);
sortSpectralIssueBySeverity(issues);
spectralIssues = formatSpectralOutput(issues);
if (issues.filter(issue => issue.severity === 0).length > 0) {
logger.debug('Spectral output contains errors');
Expand All @@ -171,6 +172,13 @@ async function runSpectralValidations(
return new SpectralResult(warnings, errors, spectralIssues);
}

function sortSpectralIssueBySeverity(issues: ISpectralDiagnostic[]): void{
logger.debug('Sorting the spectral issues by the severity');
issues.sort((issue1: ISpectralDiagnostic, issue2: ISpectralDiagnostic) =>
issue1.severity.valueOf() - issue2.severity.valueOf()
);
}

function formatJsonSchemaOutput(jsonSchemaIssues: ErrorObject[]): ValidationOutput[]{
const validationOutput : ValidationOutput[] = [];

Expand Down Expand Up @@ -265,5 +273,6 @@ function stripRefs(obj: object) : string {
export const exportedForTesting = {
formatSpectralOutput,
formatJsonSchemaOutput,
stripRefs
stripRefs,
sortSpectralIssueBySeverity
};

0 comments on commit 37c6705

Please sign in to comment.