Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): consider message when de-duplicating results #2052

Merged
merged 3 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ testRule('duplicated-entry-in-enum', [
path: ['definitions', 'Test', 'enum'],
severity: DiagnosticSeverity.Warning,
},
{
message: '"enum" property must not be valid',
path: [ 'definitions', 'Test', 'enum' ],
severity: DiagnosticSeverity.Warning,
},
{
message: '"enum" property must match exactly one schema in oneOf',
path: [ 'definitions', 'Test', 'enum' ],
severity: DiagnosticSeverity.Warning,
}
P0lip marked this conversation as resolved.
Show resolved Hide resolved
],
},

Expand Down Expand Up @@ -103,6 +113,16 @@ testRule('duplicated-entry-in-enum', [
path: ['components', 'schemas', 'Test', 'enum'],
severity: DiagnosticSeverity.Warning,
},
{
message: '"enum" property must not be valid',
path: [ 'components', 'schemas', 'Test', 'enum' ],
severity: DiagnosticSeverity.Warning,
},
{
message: '"enum" property must match exactly one schema in oneOf',
path: [ 'components', 'schemas', 'Test', 'enum' ],
severity: DiagnosticSeverity.Warning,
}
P0lip marked this conversation as resolved.
Show resolved Hide resolved
],
},
]);