Skip to content

Commit 0e70c41

Browse files
CLOUDP-306582: IPA-126: Top-Level API Names
1 parent a69ec80 commit 0e70c41

File tree

5 files changed

+235
-0
lines changed

5 files changed

+235
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
import testRule from './__helpers__/testRule.js';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-126-tag-names-should-use-title-case', [
5+
{
6+
name: 'valid Title Case tag names',
7+
document: {
8+
tags: [
9+
{ name: 'User Management' },
10+
{ name: 'Resource Groups' },
11+
{ name: 'Atlas' },
12+
{ name: 'User Profiles' },
13+
{ name: 'Api' },
14+
{ name: 'Users' },
15+
{ name: 'Resources' },
16+
{ name: 'Projects' },
17+
],
18+
},
19+
errors: [],
20+
},
21+
{
22+
name: 'invalid camelCase instead of Title Case',
23+
document: {
24+
tags: [{ name: 'userManagement' }],
25+
},
26+
errors: [
27+
{
28+
code: 'xgen-IPA-126-tag-names-should-use-title-case',
29+
message: 'Tag name should use Title Case, found: "userManagement".',
30+
path: ['tags', '0'],
31+
severity: DiagnosticSeverity.Warning,
32+
},
33+
],
34+
},
35+
{
36+
name: 'invalid kebab-case instead of Title Case',
37+
document: {
38+
tags: [{ name: 'user-management' }],
39+
},
40+
errors: [
41+
{
42+
code: 'xgen-IPA-126-tag-names-should-use-title-case',
43+
message: 'Tag name should use Title Case, found: "user-management".',
44+
path: ['tags', '0'],
45+
severity: DiagnosticSeverity.Warning,
46+
},
47+
],
48+
},
49+
{
50+
name: 'invalid snake_case instead of Title Case',
51+
document: {
52+
tags: [{ name: 'user_management' }],
53+
},
54+
errors: [
55+
{
56+
code: 'xgen-IPA-126-tag-names-should-use-title-case',
57+
message: 'Tag name should use Title Case, found: "user_management".',
58+
path: ['tags', '0'],
59+
severity: DiagnosticSeverity.Warning,
60+
},
61+
],
62+
},
63+
{
64+
name: 'invalid all lowercase instead of Title Case',
65+
document: {
66+
tags: [{ name: 'user management' }],
67+
},
68+
errors: [
69+
{
70+
code: 'xgen-IPA-126-tag-names-should-use-title-case',
71+
message: 'Tag name should use Title Case, found: "user management".',
72+
path: ['tags', '0'],
73+
severity: DiagnosticSeverity.Warning,
74+
},
75+
],
76+
},
77+
{
78+
name: 'invalid ALL UPPERCASE instead of Title Case',
79+
document: {
80+
tags: [{ name: 'USER MANAGEMENT' }],
81+
},
82+
errors: [
83+
{
84+
code: 'xgen-IPA-126-tag-names-should-use-title-case',
85+
message: 'Tag name should use Title Case, found: "USER MANAGEMENT".',
86+
path: ['tags', '0'],
87+
severity: DiagnosticSeverity.Warning,
88+
},
89+
],
90+
},
91+
{
92+
name: 'mixed cases in multiple tags',
93+
document: {
94+
tags: [{ name: 'User Management' }, { name: 'resourceGroups' }, { name: 'API ENDPOINTS' }],
95+
},
96+
errors: [
97+
{
98+
code: 'xgen-IPA-126-tag-names-should-use-title-case',
99+
message: 'Tag name should use Title Case, found: "resourceGroups".',
100+
path: ['tags', '1'],
101+
severity: DiagnosticSeverity.Warning,
102+
},
103+
{
104+
code: 'xgen-IPA-126-tag-names-should-use-title-case',
105+
message: 'Tag name should use Title Case, found: "API ENDPOINTS".',
106+
path: ['tags', '2'],
107+
severity: DiagnosticSeverity.Warning,
108+
},
109+
],
110+
},
111+
{
112+
name: 'valid with exception',
113+
document: {
114+
tags: [
115+
{
116+
name: 'legacy_tag',
117+
'x-xgen-IPA-exception': {
118+
'xgen-IPA-126-tag-names-should-use-title-case': 'Legacy tag that cannot be changed',
119+
},
120+
},
121+
],
122+
},
123+
errors: [],
124+
},
125+
{
126+
name: 'invalid tag names',
127+
document: {
128+
tags: [
129+
{ name: 'Api V1' },
130+
{ name: 'Version 2 Resources' },
131+
{ name: 'Push-Based Log Export' },
132+
{ name: 'AWS Clusters DNS' },
133+
{ name: 'Encryption at Rest using Customer Key Management' },
134+
],
135+
},
136+
errors: [
137+
{
138+
code: 'xgen-IPA-126-tag-names-should-use-title-case',
139+
message: 'Tag name should use Title Case, found: "Api V1".',
140+
path: ['tags', '0'],
141+
severity: DiagnosticSeverity.Warning,
142+
},
143+
{
144+
code: 'xgen-IPA-126-tag-names-should-use-title-case',
145+
message: 'Tag name should use Title Case, found: "Version 2 Resources".',
146+
path: ['tags', '1'],
147+
severity: DiagnosticSeverity.Warning,
148+
},
149+
{
150+
code: 'xgen-IPA-126-tag-names-should-use-title-case',
151+
message: 'Tag name should use Title Case, found: "Push-Based Log Export".',
152+
path: ['tags', '2'],
153+
severity: DiagnosticSeverity.Warning,
154+
},
155+
{
156+
code: 'xgen-IPA-126-tag-names-should-use-title-case',
157+
message: 'Tag name should use Title Case, found: "AWS Clusters DNS".',
158+
path: ['tags', '3'],
159+
severity: DiagnosticSeverity.Warning,
160+
},
161+
{
162+
code: 'xgen-IPA-126-tag-names-should-use-title-case',
163+
message: 'Tag name should use Title Case, found: "Encryption at Rest using Customer Key Management".',
164+
path: ['tags', '4'],
165+
severity: DiagnosticSeverity.Warning,
166+
},
167+
],
168+
},
169+
]);

tools/spectral/ipa/ipa-spectral.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extends:
1818
- ./rulesets/IPA-123.yaml
1919
- ./rulesets/IPA-124.yaml
2020
- ./rulesets/IPA-125.yaml
21+
- ./rulesets/IPA-126.yaml
2122

2223
overrides:
2324
- files:
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# IPA-126: Top-Level API Names
2+
# http://go/ipa/126
3+
4+
functions:
5+
- IPA126TagNamesShouldUseTitleCase
6+
rules:
7+
xgen-IPA-126-tag-names-should-use-title-case:
8+
description: |
9+
Tag names in the OpenAPI specification should use Title Case.
10+
11+
##### Implementation details
12+
Rule checks for the following conditions:
13+
- All tag names defined in the OpenAPI tags object should use Title Case
14+
- Title Case means each word starts with an uppercase letter, and the rest are lowercase
15+
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-126-tag-names-should-use-title-case'
16+
severity: warn
17+
given: $.tags[*]
18+
then:
19+
function: 'IPA126TagNamesShouldUseTitleCase'

tools/spectral/ipa/rulesets/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,4 +920,20 @@ object types with clear discriminators.
920920

921921

922922

923+
### IPA-126
924+
925+
Rules are based on [http://go/ipa/IPA-126](http://go/ipa/IPA-126).
926+
927+
#### xgen-IPA-126-tag-names-should-use-title-case
928+
929+
![warn](https://img.shields.io/badge/warning-yellow)
930+
Tag names in the OpenAPI specification should use Title Case.
931+
932+
##### Implementation details
933+
Rule checks for the following conditions:
934+
- All tag names defined in the OpenAPI tags object should use Title Case
935+
- Title Case means each word starts with an uppercase letter, and the rest are lowercase
936+
937+
938+
923939

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { hasException } from './utils/exceptions.js';
2+
import { collectAdoption, collectAndReturnViolation, collectException } from './utils/collectionUtils.js';
3+
import { casing } from '@stoplight/spectral-functions';
4+
5+
const RULE_NAME = 'xgen-IPA-126-tag-names-should-use-title-case';
6+
7+
export default (input, options, { path }) => {
8+
const tagName = input.name;
9+
if (!tagName || tagName.trim().length === 0) {
10+
return;
11+
}
12+
13+
if (hasException(input, RULE_NAME)) {
14+
collectException(input, RULE_NAME, path);
15+
return;
16+
}
17+
18+
// Check if the tag name uses Title Case
19+
if (casing(tagName, { type: 'pascal', disallowDigits: true, separator: { char: ' ' } })) {
20+
return collectAndReturnViolation(path, RULE_NAME, [
21+
{
22+
path,
23+
message: `Tag name should use Title Case, found: "${tagName}".`,
24+
},
25+
]);
26+
}
27+
28+
// Tag name uses Title Case
29+
collectAdoption(path, RULE_NAME);
30+
};

0 commit comments

Comments
 (0)