Skip to content

Commit 96d33c6

Browse files
CLOUDP-304967: Ignore inline table ending in separate rule
1 parent 92d6480 commit 96d33c6

File tree

7 files changed

+241
-118
lines changed

7 files changed

+241
-118
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-117-description-ends-with-period', [
5+
{
6+
name: 'valid description',
7+
document: {
8+
components: {
9+
schemas: {
10+
Schema: {
11+
properties: {
12+
id: {
13+
description: 'Description.',
14+
},
15+
},
16+
},
17+
},
18+
},
19+
},
20+
errors: [],
21+
},
22+
{
23+
name: 'invalid descriptions',
24+
document: {
25+
components: {
26+
schemas: {
27+
Schema: {
28+
properties: {
29+
noPeriod: {
30+
description: 'Description',
31+
},
32+
},
33+
},
34+
},
35+
},
36+
},
37+
errors: [
38+
{
39+
code: 'xgen-IPA-117-description-ends-with-period',
40+
message: 'Descriptions must end with a full stop(.).',
41+
path: ['components', 'schemas', 'Schema', 'properties', 'noPeriod'],
42+
severity: DiagnosticSeverity.Warning,
43+
},
44+
],
45+
},
46+
{
47+
name: 'ignores descriptions ending with table',
48+
document: {
49+
components: {
50+
schemas: {
51+
Schema: {
52+
properties: {
53+
noPeriod: {
54+
description: 'Description\n| Table |',
55+
},
56+
},
57+
},
58+
},
59+
},
60+
},
61+
errors: [],
62+
},
63+
{
64+
name: 'invalid components with exceptions',
65+
document: {
66+
components: {
67+
schemas: {
68+
Schema: {
69+
properties: {
70+
noPeriod: {
71+
description: 'Description',
72+
'x-xgen-IPA-exception': {
73+
'xgen-IPA-117-description-ends-with-period': 'reason',
74+
},
75+
},
76+
},
77+
},
78+
},
79+
},
80+
},
81+
errors: [],
82+
},
83+
]);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-117-description-starts-with-uppercase', [
5+
{
6+
name: 'valid description',
7+
document: {
8+
components: {
9+
schemas: {
10+
Schema: {
11+
properties: {
12+
id: {
13+
description: 'Description',
14+
},
15+
},
16+
},
17+
},
18+
},
19+
},
20+
errors: [],
21+
},
22+
{
23+
name: 'invalid descriptions',
24+
document: {
25+
components: {
26+
schemas: {
27+
Schema: {
28+
properties: {
29+
noUpperCase: {
30+
description: 'description',
31+
},
32+
},
33+
},
34+
},
35+
},
36+
},
37+
errors: [
38+
{
39+
code: 'xgen-IPA-117-description-starts-with-uppercase',
40+
message: 'Descriptions must start with Uppercase.',
41+
path: ['components', 'schemas', 'Schema', 'properties', 'noUpperCase'],
42+
severity: DiagnosticSeverity.Warning,
43+
},
44+
],
45+
},
46+
{
47+
name: 'invalid components with exceptions',
48+
document: {
49+
components: {
50+
schemas: {
51+
Schema: {
52+
properties: {
53+
noUpperCase: {
54+
description: 'description',
55+
'x-xgen-IPA-exception': {
56+
'xgen-IPA-117-description-starts-with-uppercase': 'reason',
57+
},
58+
},
59+
},
60+
},
61+
},
62+
},
63+
},
64+
errors: [],
65+
},
66+
]);

tools/spectral/ipa/__tests__/IPA117DescriptionUpperCasePeriod.test.js

Lines changed: 0 additions & 102 deletions
This file was deleted.

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
functions:
55
- IPA117HasDescription
6-
- IPA117DescriptionUpperCasePeriod
6+
- IPA117DescriptionStartsWithUpperCase
7+
- IPA117DescriptionEndsWithPeriod
78

89
rules:
910
xgen-IPA-117-description:
@@ -31,9 +32,9 @@ rules:
3132
- '$.components.parameters[*]'
3233
then:
3334
function: 'IPA117HasDescription'
34-
xgen-IPA-117-description-uppercase-period:
35+
xgen-IPA-117-description-starts-with-uppercase:
3536
description: |
36-
Descriptions must start with Uppercase and end with a full stop(.)
37+
Descriptions must start with Uppercase.
3738
3839
##### Implementation details
3940
Rule checks the format of the description property in the following components:
@@ -43,7 +44,7 @@ rules:
4344
- Inline schema properties for operation object requests and responses
4445
- Parameter objects (in operations and components)
4546
- Schema properties
46-
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-117-description-uppercase-period'
47+
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-117-description-starts-with-uppercase'
4748
severity: warn
4849
given:
4950
- '$.info'
@@ -54,4 +55,29 @@ rules:
5455
- '$.components.schemas..properties[*]'
5556
- '$.components.parameters[*]'
5657
then:
57-
function: 'IPA117DescriptionUpperCasePeriod'
58+
function: 'IPA117DescriptionStartsWithUpperCase'
59+
xgen-IPA-117-description-ends-with-period:
60+
description: |
61+
Descriptions must end with a full stop(.).
62+
63+
##### Implementation details
64+
Rule checks the format of the description property in the following components:
65+
- Info object
66+
- Tags
67+
- Operation objects
68+
- Inline schema properties for operation object requests and responses
69+
- Parameter objects (in operations and components)
70+
- Schema properties
71+
The rule ignores descriptions that end with `|`, i.e. inline markdown tables
72+
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-117-description-ends-with-period'
73+
severity: warn
74+
given:
75+
- '$.info'
76+
- '$.tags[*]'
77+
- '$.paths[*][get,put,post,delete,options,head,patch,trace]'
78+
- '$.paths[*][get,put,post,delete,options,head,patch,trace].parameters[*]'
79+
- '$.paths[*][get,put,post,delete,options,head,patch,trace]..content..properties[*]'
80+
- '$.components.schemas..properties[*]'
81+
- '$.components.parameters[*]'
82+
then:
83+
function: 'IPA117DescriptionEndsWithPeriod'

tools/spectral/ipa/rulesets/README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,10 @@ Rule checks for description property in the following components:
535535
- Schema properties
536536
The rule also fails if the description is an empty string.
537537

538-
#### xgen-IPA-117-description-uppercase-period
538+
#### xgen-IPA-117-description-starts-with-uppercase
539539

540540
![warn](https://img.shields.io/badge/warning-yellow)
541-
Descriptions must start with Uppercase and end with a full stop(.)
541+
Descriptions must start with Uppercase.
542542

543543
##### Implementation details
544544
Rule checks the format of the description property in the following components:
@@ -549,6 +549,21 @@ Rule checks the format of the description property in the following components:
549549
- Parameter objects (in operations and components)
550550
- Schema properties
551551

552+
#### xgen-IPA-117-description-ends-with-period
553+
554+
![warn](https://img.shields.io/badge/warning-yellow)
555+
Descriptions must end with a full stop(.).
556+
557+
##### Implementation details
558+
Rule checks the format of the description property in the following components:
559+
- Info object
560+
- Tags
561+
- Operation objects
562+
- Inline schema properties for operation object requests and responses
563+
- Parameter objects (in operations and components)
564+
- Schema properties
565+
The rule ignores descriptions that end with `|`, i.e. inline markdown tables
566+
552567

553568

554569
### IPA-123
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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-ends-with-period';
10+
const ERROR_MESSAGE_PERIOD = 'Descriptions must end with a full stop(.).';
11+
12+
export default (input, opts, { path }) => {
13+
// Ignore missing descriptions or descriptions that ends with an inline table
14+
if (!input['description'] || input['description'].endsWith('|')) {
15+
return;
16+
}
17+
18+
if (hasException(input, RULE_NAME)) {
19+
collectException(input, RULE_NAME, path);
20+
return;
21+
}
22+
23+
const errors = checkViolationsAndReturnErrors(input['description'], path);
24+
if (errors.length !== 0) {
25+
return collectAndReturnViolation(path, RULE_NAME, errors);
26+
}
27+
collectAdoption(path, RULE_NAME);
28+
};
29+
30+
function checkViolationsAndReturnErrors(description, path) {
31+
const periodEnd = new RegExp(`[.]$`);
32+
33+
try {
34+
if (!periodEnd.test(description)) {
35+
return [{ path, message: ERROR_MESSAGE_PERIOD }];
36+
}
37+
return [];
38+
} catch (e) {
39+
handleInternalError(RULE_NAME, path, e);
40+
}
41+
}

0 commit comments

Comments
 (0)