Skip to content

Commit 475abee

Browse files
CLOUDP-272001: IPA-112: Disallow usages of project in the api (#566)
1 parent e2f1076 commit 475abee

File tree

5 files changed

+331
-0
lines changed

5 files changed

+331
-0
lines changed
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-112-avoid-project-field-names', [
5+
{
6+
name: 'valid schema - no project field names',
7+
document: {
8+
components: {
9+
schemas: {
10+
SchemaName: {
11+
properties: {
12+
group: { type: 'string' },
13+
groupId: { type: 'string' },
14+
otherField: { type: 'number' },
15+
},
16+
},
17+
},
18+
},
19+
},
20+
errors: [],
21+
},
22+
{
23+
name: 'invalid schema - with project field name',
24+
document: {
25+
components: {
26+
schemas: {
27+
SchemaName: {
28+
properties: {
29+
project: { type: 'string' },
30+
},
31+
},
32+
},
33+
},
34+
},
35+
errors: [
36+
{
37+
code: 'xgen-IPA-112-avoid-project-field-names',
38+
message: 'Field name "project" should be avoided. Consider using "group" instead.',
39+
path: ['components', 'schemas', 'SchemaName', 'properties', 'project'],
40+
severity: DiagnosticSeverity.Warning,
41+
},
42+
],
43+
},
44+
{
45+
name: 'invalid schema - with projects field name',
46+
document: {
47+
components: {
48+
schemas: {
49+
SchemaName: {
50+
properties: {
51+
projects: { type: 'array' },
52+
},
53+
},
54+
},
55+
},
56+
},
57+
errors: [
58+
{
59+
code: 'xgen-IPA-112-avoid-project-field-names',
60+
message: 'Field name "projects" should be avoided. Consider using "group" instead.',
61+
path: ['components', 'schemas', 'SchemaName', 'properties', 'projects'],
62+
severity: DiagnosticSeverity.Warning,
63+
},
64+
],
65+
},
66+
{
67+
name: 'invalid schema - with projectId field name',
68+
document: {
69+
components: {
70+
schemas: {
71+
SchemaName: {
72+
properties: {
73+
projectId: { type: 'string' },
74+
},
75+
},
76+
},
77+
},
78+
},
79+
errors: [
80+
{
81+
code: 'xgen-IPA-112-avoid-project-field-names',
82+
message: 'Field name "projectId" should be avoided. Consider using "group" instead.',
83+
path: ['components', 'schemas', 'SchemaName', 'properties', 'projectId'],
84+
severity: DiagnosticSeverity.Warning,
85+
},
86+
],
87+
},
88+
{
89+
name: 'invalid schema - with different case variants',
90+
document: {
91+
components: {
92+
schemas: {
93+
SchemaName: {
94+
properties: {
95+
Project: { type: 'string' },
96+
PROJECTID: { type: 'string' },
97+
},
98+
},
99+
},
100+
},
101+
},
102+
errors: [
103+
{
104+
code: 'xgen-IPA-112-avoid-project-field-names',
105+
message: 'Field name "Project" should be avoided. Consider using "group" instead.',
106+
path: ['components', 'schemas', 'SchemaName', 'properties', 'Project'],
107+
severity: DiagnosticSeverity.Warning,
108+
},
109+
{
110+
code: 'xgen-IPA-112-avoid-project-field-names',
111+
message: 'Field name "PROJECTID" should be avoided. Consider using "group" instead.',
112+
path: ['components', 'schemas', 'SchemaName', 'properties', 'PROJECTID'],
113+
severity: DiagnosticSeverity.Warning,
114+
},
115+
],
116+
},
117+
{
118+
name: 'invalid schema with exception - project field name with exception',
119+
document: {
120+
components: {
121+
schemas: {
122+
SchemaName: {
123+
properties: {
124+
project: {
125+
type: 'string',
126+
'x-xgen-IPA-exception': {
127+
'xgen-IPA-112-avoid-project-field-names': 'reason',
128+
},
129+
},
130+
},
131+
},
132+
},
133+
},
134+
},
135+
errors: [],
136+
},
137+
{
138+
name: 'field name containing project substring',
139+
document: {
140+
components: {
141+
schemas: {
142+
SchemaName: {
143+
properties: {
144+
myProjectDetails: { type: 'object' },
145+
},
146+
},
147+
},
148+
},
149+
},
150+
errors: [
151+
{
152+
code: 'xgen-IPA-112-avoid-project-field-names',
153+
message: 'Field name "myProjectDetails" should be avoided. Consider using "group" instead.',
154+
path: ['components', 'schemas', 'SchemaName', 'properties', 'myProjectDetails'],
155+
severity: DiagnosticSeverity.Warning,
156+
},
157+
],
158+
},
159+
{
160+
name: 'exception - field with project substring',
161+
document: {
162+
components: {
163+
schemas: {
164+
SchemaName: {
165+
properties: {
166+
myProjectDetails: {
167+
type: 'object',
168+
'x-xgen-IPA-exception': {
169+
'xgen-IPA-112-avoid-project-field-names': 'Reason',
170+
},
171+
},
172+
},
173+
},
174+
},
175+
},
176+
},
177+
errors: [],
178+
},
179+
{
180+
name: 'exception - multiple project fields',
181+
document: {
182+
components: {
183+
schemas: {
184+
SchemaName: {
185+
properties: {
186+
projectId: {
187+
type: 'string',
188+
'x-xgen-IPA-exception': {
189+
'xgen-IPA-112-avoid-project-field-names': 'Reason',
190+
},
191+
},
192+
projects: {
193+
type: 'array',
194+
'x-xgen-IPA-exception': {
195+
'xgen-IPA-112-avoid-project-field-names': 'Reason',
196+
},
197+
},
198+
},
199+
},
200+
},
201+
},
202+
},
203+
errors: [],
204+
},
205+
{
206+
name: 'mixed valid, invalid, and exception fields',
207+
document: {
208+
components: {
209+
schemas: {
210+
SchemaName: {
211+
properties: {
212+
project: {
213+
type: 'string',
214+
'x-xgen-IPA-exception': {
215+
'xgen-IPA-112-avoid-project-field-names': 'Reason',
216+
},
217+
},
218+
projectId: { type: 'string' },
219+
group: { type: 'string' },
220+
},
221+
},
222+
},
223+
},
224+
},
225+
errors: [
226+
{
227+
code: 'xgen-IPA-112-avoid-project-field-names',
228+
message: 'Field name "projectId" should be avoided. Consider using "group" instead.',
229+
path: ['components', 'schemas', 'SchemaName', 'properties', 'projectId'],
230+
severity: DiagnosticSeverity.Warning,
231+
},
232+
],
233+
},
234+
]);

tools/spectral/ipa/ipa-spectral.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ extends:
77
- ./rulesets/IPA-107.yaml
88
- ./rulesets/IPA-108.yaml
99
- ./rulesets/IPA-109.yaml
10+
- ./rulesets/IPA-112.yaml
1011
- ./rulesets/IPA-113.yaml
1112
- ./rulesets/IPA-123.yaml
1213

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# IPA-112: Field Names
2+
# http://go/ipa/112
3+
4+
functions:
5+
- IPA112AvoidProjectFieldNames
6+
7+
rules:
8+
xgen-IPA-112-avoid-project-field-names:
9+
description: |
10+
Schema field names should avoid using "project", "projects", or "projectId".
11+
12+
##### Implementation details
13+
Rule checks for the following conditions:
14+
- Searches through all schemas in the API definition
15+
- Identifies property names that match "project" (case-insensitive)
16+
- Reports any instances where these field names are used
17+
- Suggests using "group", "groups", or "groupId" as alternatives
18+
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-112-avoid-project-field-names'
19+
severity: warn
20+
given: '$.components.schemas..properties[*]~'
21+
then:
22+
function: 'IPA112AvoidProjectFieldNames'
23+
functionOptions:
24+
prohibitedFieldNames:
25+
- name: 'project'
26+
alternative: ['group']

tools/spectral/ipa/rulesets/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,24 @@ Rule checks for the following conditions:
435435

436436

437437

438+
### IPA-112
439+
440+
Rules are based on [http://go/ipa/IPA-112](http://go/ipa/IPA-112).
441+
442+
#### xgen-IPA-112-avoid-project-field-names
443+
444+
![warn](https://img.shields.io/badge/warning-yellow)
445+
Schema field names should avoid using "project", "projects", or "projectId".
446+
447+
##### Implementation details
448+
Rule checks for the following conditions:
449+
- Searches through all schemas in the API definition
450+
- Identifies property names that match "project" (case-insensitive)
451+
- Reports any instances where these field names are used
452+
- Suggests using "group", "groups", or "groupId" as alternatives
453+
454+
455+
438456
### IPA-113
439457

440458
Rules are based on [http://go/ipa/IPA-113](http://go/ipa/IPA-113).
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { hasException } from './utils/exceptions.js';
2+
import {
3+
collectAdoption,
4+
collectAndReturnViolation,
5+
collectException,
6+
handleInternalError,
7+
} from './utils/collectionUtils.js';
8+
import { resolveObject } from './utils/componentUtils.js';
9+
10+
const RULE_NAME = 'xgen-IPA-112-avoid-project-field-names';
11+
12+
export default (input, options, { path, documentInventory }) => {
13+
const oas = documentInventory.resolved;
14+
const property = resolveObject(oas, path);
15+
16+
if (hasException(property, RULE_NAME)) {
17+
collectException(property, RULE_NAME, path);
18+
return;
19+
}
20+
21+
const errors = checkViolationsAndReturnErrors(input, options, path);
22+
if (errors.length !== 0) {
23+
return collectAndReturnViolation(path, RULE_NAME, errors);
24+
}
25+
collectAdoption(path, RULE_NAME);
26+
};
27+
28+
function checkViolationsAndReturnErrors(input, options, path) {
29+
try {
30+
const prohibitedFieldNames = options?.prohibitedFieldNames || [];
31+
const lowerPropertyName = input.toLowerCase();
32+
33+
// Check if the property name includes any of the prohibited terms
34+
for (const prohibitedItem of prohibitedFieldNames) {
35+
const prohibitedName = prohibitedItem.name || '';
36+
const alternative = prohibitedItem.alternative || '';
37+
38+
if (lowerPropertyName.includes(prohibitedName.toLowerCase())) {
39+
return [
40+
{
41+
path,
42+
message: `Field name "${input}" should be avoided. Consider using ${alternative.map((alt) => `"${alt}"`)} instead.`,
43+
},
44+
];
45+
}
46+
}
47+
48+
return [];
49+
} catch (e) {
50+
handleInternalError(RULE_NAME, path, e);
51+
}
52+
}

0 commit comments

Comments
 (0)