Skip to content

Commit

Permalink
fix(core): consider message when de-duplicating results (#2052)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpopp07 committed Feb 11, 2022
1 parent 8451774 commit b07cc7b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 25 deletions.
25 changes: 0 additions & 25 deletions docs/guides/3-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,28 +132,3 @@ const spectral = new Spectral({ resolver: customFileResolver });
The custom resolver we've just created will resolve all remote file refs relatively to the current working directory.

More on that can be found in the [json-ref-resolver repo](https://github.com/stoplightio/json-ref-resolver).

### Using a Custom De-duplication Strategy

By default, Spectral will de-duplicate results based on the result code and document location. You can customize this
behavior with the `computeFingerprint` option. For example, here is the default fingerprint implementation:

The final reported results are de-duplicated based on their computed fingerprint.

```ts
const spectral = new Spectral({
computeFingerprint: (rule: IRuleResult, hash) => {
let id = String(rule.code);

if (rule.path && rule.path.length) {
id += JSON.stringify(rule.path);
} else if (rule.range) {
id += JSON.stringify(rule.range);
}

if (rule.source) id += rule.source;

return hash(id);
},
});
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[
{
"code": "valid-schema-example-in-content",
"message": "\"schema.example\" property type should be integer",
"path": [
"paths",
"/resource",
"get",
"responses",
"200",
"content",
"application/json",
"schema",
"example"
],
"severity": 0,
"range": {
"start": {
"line": 20,
"character": 15
},
"end": {
"line": 20,
"character": 19
}
}
},
{
"code": "valid-schema-example-in-content",
"message": "\"schema.example\" has some other valid problem",
"path": [
"paths",
"/resource",
"get",
"responses",
"200",
"content",
"application/json",
"schema",
"example"
],
"severity": 0,
"range": {
"start": {
"line": 20,
"character": 15
},
"end": {
"line": 20,
"character": 19
}
}
},
{
"code": "valid-schema-example-in-content",
"message": "\"schema.example\" property type should be integer",
"path": [
"paths",
"/resource",
"get",
"responses",
"200",
"content",
"application/json",
"schema",
"example"
],
"severity": 0,
"range": {
"start": {
"line": 20,
"character": 15
},
"end": {
"line": 20,
"character": 19
}
}
}
]
12 changes: 12 additions & 0 deletions packages/core/src/utils/__tests__/prepareResults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,16 @@ describe('prepareResults util', () => {

expect(prepareResults(onlyDuplicates, defaultComputeResultFingerprint).length).toBe(1);
});

it('deduplicate only exact validation results', () => {
// verifies that results with the same code/path but different messages will not be de-duplicated
expect(prepareResults(duplicateValidationResults, defaultComputeResultFingerprint)).toEqual([
expect.objectContaining({
code: 'valid-example-in-schemas',
}),
expect.objectContaining({
code: 'valid-schema-example-in-content',
}),
]);
});
});
4 changes: 4 additions & 0 deletions packages/core/src/utils/prepareResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export const defaultComputeResultFingerprint: ComputeFingerprintFunc = (rule, ha
id += rule.source;
}

if (rule.message !== void 0) {
id += rule.message;
}

return hash(id);
};

Expand Down

0 comments on commit b07cc7b

Please sign in to comment.