Skip to content

Commit 67c9929

Browse files
CLOUDP-306571: Add rule xgen-IPA-117-description-should-not-use-inline-links
1 parent c596360 commit 67c9929

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-117-description-should-not-use-inline-links', [
5+
{
6+
name: 'valid description',
7+
document: {
8+
components: {
9+
schemas: {
10+
Schema: {
11+
properties: {
12+
valid: {
13+
description: 'Description.',
14+
},
15+
validWithParentheses: {
16+
description: 'Description (with parentheses).',
17+
},
18+
validWithBrackets: {
19+
description: 'Description [with brackets].',
20+
},
21+
validMixed: {
22+
description: 'Something [with brackets]. Description (with parentheses).',
23+
},
24+
validChars: {
25+
description: 'Something can only be "a-zA-Z[]():,".',
26+
},
27+
},
28+
},
29+
},
30+
},
31+
},
32+
errors: [],
33+
},
34+
{
35+
name: 'invalid descriptions',
36+
document: {
37+
components: {
38+
schemas: {
39+
Schema: {
40+
properties: {
41+
invalidLink: {
42+
description: 'Hello [world](https://www.mongodb.com).',
43+
},
44+
invalidMultipleLinks: {
45+
description: 'Hello [world](https://www.mongodb.com). And [MongoDB website](https://www.mongodb.com)',
46+
},
47+
},
48+
},
49+
},
50+
},
51+
},
52+
errors: [
53+
{
54+
code: 'xgen-IPA-117-description-should-not-use-inline-links',
55+
message:
56+
'Descriptions should not include inline links. Use the externalDocumentation property instead, see https://swagger.io/specification/#external-documentation-object.',
57+
path: ['components', 'schemas', 'Schema', 'properties', 'invalidLink'],
58+
severity: DiagnosticSeverity.Warning,
59+
},
60+
{
61+
code: 'xgen-IPA-117-description-should-not-use-inline-links',
62+
message:
63+
'Descriptions should not include inline links. Use the externalDocumentation property instead, see https://swagger.io/specification/#external-documentation-object.',
64+
path: ['components', 'schemas', 'Schema', 'properties', 'invalidMultipleLinks'],
65+
severity: DiagnosticSeverity.Warning,
66+
},
67+
],
68+
},
69+
{
70+
name: 'invalid descriptions with exceptions',
71+
document: {
72+
components: {
73+
schemas: {
74+
Schema: {
75+
properties: {
76+
invalidLink: {
77+
description: 'Hello [world](https://www.mongodb.com).',
78+
'x-xgen-IPA-exception': {
79+
'xgen-IPA-117-description-should-not-use-inline-links': 'reason',
80+
},
81+
},
82+
invalidMultipleLinks: {
83+
description: 'Hello [world](https://www.mongodb.com). And [MongoDB website](https://www.mongodb.com)',
84+
'x-xgen-IPA-exception': {
85+
'xgen-IPA-117-description-should-not-use-inline-links': 'reason',
86+
},
87+
},
88+
},
89+
},
90+
},
91+
},
92+
},
93+
errors: [],
94+
},
95+
]);

tools/spectral/ipa/rulesets/IPA-117.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ functions:
77
- IPA117DescriptionEndsWithPeriod
88
- IPA117DescriptionMustNotUseHtml
99
- IPA117DescriptionShouldNotUseTables
10+
- IPA117DescriptionShouldNotUseLinks
1011

1112
rules:
1213
xgen-IPA-117-description:
@@ -133,3 +134,26 @@ rules:
133134
- '$.components.parameters[*]'
134135
then:
135136
function: 'IPA117DescriptionShouldNotUseTables'
137+
xgen-IPA-117-description-should-not-use-inline-links:
138+
description: |
139+
Descriptions should not include inline links.
140+
141+
##### Implementation details
142+
Rule checks the format of the descriptions for components:
143+
- Tags
144+
- Operation objects
145+
- Inline schema properties for operation object requests and responses
146+
- Parameter objects (in operations and components)
147+
- Schema properties
148+
The rule validates that the description content does not include inline markdown links. The ignores HTML `a` links, this is covered by `xgen-IPA-117-description-must-not-use-html`.
149+
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-117-description-should-not-use-inline-links'
150+
severity: warn
151+
given:
152+
- '$.tags[*]'
153+
- '$.paths[*][get,put,post,delete,options,head,patch,trace]'
154+
- '$.paths[*][get,put,post,delete,options,head,patch,trace].parameters[*]'
155+
- '$.paths[*][get,put,post,delete,options,head,patch,trace]..content..properties[*]'
156+
- '$.components.schemas..properties[*]'
157+
- '$.components.parameters[*]'
158+
then:
159+
function: 'IPA117DescriptionShouldNotUseLinks'

tools/spectral/ipa/rulesets/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,20 @@ Rule checks the format of the descriptions for components:
660660
- Schema properties
661661
The rule validates that the description content does not include inline markdown tables.
662662

663+
#### xgen-IPA-117-description-should-not-use-inline-links
664+
665+
![warn](https://img.shields.io/badge/warning-yellow)
666+
Descriptions should not include inline links.
667+
668+
##### Implementation details
669+
Rule checks the format of the descriptions for components:
670+
- Tags
671+
- Operation objects
672+
- Inline schema properties for operation object requests and responses
673+
- Parameter objects (in operations and components)
674+
- Schema properties
675+
The rule validates that the description content does not include inline markdown links. The ignores HTML `a` links, this is covered by `xgen-IPA-117-description-must-not-use-html`.
676+
663677

664678

665679
### IPA-123
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { hasException } from './utils/exceptions.js';
2+
import {
3+
collectAdoption,
4+
collectAndReturnViolation,
5+
collectException,
6+
handleInternalError,
7+
} from './utils/collectionUtils.js';
8+
9+
const RULE_NAME = 'xgen-IPA-117-description-should-not-use-inline-links';
10+
const ERROR_MESSAGE =
11+
'Descriptions should not include inline links. Use the externalDocumentation property instead, see https://swagger.io/specification/#external-documentation-object.';
12+
13+
export default (input, opts, { path }) => {
14+
// Ignore missing descriptions
15+
if (!input['description']) {
16+
return;
17+
}
18+
19+
if (hasException(input, RULE_NAME)) {
20+
collectException(input, RULE_NAME, path);
21+
return;
22+
}
23+
24+
const errors = checkViolationsAndReturnErrors(input['description'], path);
25+
if (errors.length !== 0) {
26+
return collectAndReturnViolation(path, RULE_NAME, errors);
27+
}
28+
collectAdoption(path, RULE_NAME);
29+
};
30+
31+
function checkViolationsAndReturnErrors(description, path) {
32+
const markdownLinkPattern = new RegExp(`\\[.+]\\(.+\\)`);
33+
34+
try {
35+
if (markdownLinkPattern.test(description)) {
36+
console.log(description, '\n\n');
37+
return [{ path, message: ERROR_MESSAGE }];
38+
}
39+
return [];
40+
} catch (e) {
41+
handleInternalError(RULE_NAME, path, e);
42+
}
43+
}

0 commit comments

Comments
 (0)