Skip to content

Commit 2ba7eac

Browse files
fixes
1 parent 372fc95 commit 2ba7eac

File tree

3 files changed

+16
-94
lines changed

3 files changed

+16
-94
lines changed

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

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -134,36 +134,6 @@ testRule('xgen-IPA-112-avoid-project-field-names', [
134134
},
135135
errors: []
136136
},
137-
{
138-
name: 'paths with project field name',
139-
document: {
140-
paths: {
141-
'/api/resource': {
142-
post: {
143-
requestBody: {
144-
content: {
145-
'application/json': {
146-
schema: {
147-
properties: {
148-
project: { type: 'string' }
149-
}
150-
}
151-
}
152-
}
153-
}
154-
}
155-
}
156-
}
157-
},
158-
errors: [
159-
{
160-
code: 'xgen-IPA-112-avoid-project-field-names',
161-
message: 'Field name "project" should be avoided. Consider using "group", "groups", or "groupId" instead.',
162-
path: ['paths', '/api/resource', 'post', 'requestBody', 'content', 'application/json', 'schema', 'properties', 'project'],
163-
severity: DiagnosticSeverity.Warning
164-
}
165-
]
166-
},
167137
{
168138
name: 'field name containing project substring',
169139
document: {
@@ -232,54 +202,6 @@ testRule('xgen-IPA-112-avoid-project-field-names', [
232202
},
233203
errors: []
234204
},
235-
{
236-
name: 'exception - schema level exception for all properties',
237-
document: {
238-
components: {
239-
schemas: {
240-
SchemaName: {
241-
'x-xgen-IPA-exception': {
242-
'xgen-IPA-112-avoid-project-field-names': 'Legacy schema that cannot be changed'
243-
},
244-
properties: {
245-
projectId: { type: 'string' },
246-
projects: { type: 'array' },
247-
myProjectDetails: { type: 'object' }
248-
}
249-
}
250-
}
251-
}
252-
},
253-
errors: []
254-
},
255-
{
256-
name: 'exception - in paths',
257-
document: {
258-
paths: {
259-
'/api/resource': {
260-
post: {
261-
requestBody: {
262-
content: {
263-
'application/json': {
264-
schema: {
265-
properties: {
266-
project: {
267-
type: 'string',
268-
'x-xgen-IPA-exception': {
269-
'xgen-IPA-112-avoid-project-field-names': 'Third-party integration requirement'
270-
}
271-
}
272-
}
273-
}
274-
}
275-
}
276-
}
277-
}
278-
}
279-
}
280-
},
281-
errors: []
282-
},
283205
{
284206
name: 'mixed valid, invalid, and exception fields',
285207
document: {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ rules:
1212
##### Implementation details
1313
Rule checks for the following conditions:
1414
- Searches through all schemas in the API definition
15-
- Identifies property names that match "group", "groups", or "groupId" (case-insensitive)
15+
- Identifies property names that match "project" (case-insensitive)
1616
- Reports any instances where these field names are used
1717
- Suggests using "group", "groups", or "groupId" as alternatives
1818
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-112-avoid-project-field-names'
1919
severity: warn
20-
given: '$..[?(@.properties)]'
20+
given: "$.components.schemas..properties[*]~"
2121
then:
22-
field: @key
23-
function: 'IPA112AvoidProjectFieldNames'
22+
function: 'IPA112AvoidProjectFieldNames'
23+
functionOptions:
24+
prohibitedFieldName: "project"

tools/spectral/ipa/rulesets/functions/IPA112AvoidProjectFieldNames.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { resolveObject } from './utils/componentUtils.js';
99

1010
const RULE_NAME = 'xgen-IPA-112-avoid-project-field-names';
1111

12-
export default (input, _, { path, documentInventory }) => {
12+
export default (input, options, { path, documentInventory }) => {
13+
1314
const oas = documentInventory.resolved;
1415
const property = resolveObject(oas, path);
1516

@@ -18,28 +19,26 @@ export default (input, _, { path, documentInventory }) => {
1819
return;
1920
}
2021

21-
const errors = checkViolationsAndReturnErrors(input, path);
22+
const errors = checkViolationsAndReturnErrors(input, options, path);
2223
if (errors.length !== 0) {
2324
return collectAndReturnViolation(path, RULE_NAME, errors);
2425
}
25-
collectAdoption(input, RULE_NAME);
26+
collectAdoption(path, RULE_NAME);
27+
2628
};
2729

28-
function checkViolationsAndReturnErrors(input, path) {
29-
const errors = [];
30+
function checkViolationsAndReturnErrors(input, options, path) {
3031
try {
31-
const prohibitedName = "project";
32-
32+
const prohibitedFieldName = options?.prohibitedFieldName || "";
3333
const lowerPropertyName = input.toLowerCase();
34-
35-
if (lowerPropertyName.includes(prohibitedName)) {
36-
errors.push({
34+
if (lowerPropertyName.includes(prohibitedFieldName)) {
35+
return [{
3736
path,
3837
message: `Field name "${input}" should be avoided. Consider using "group", "groups", or "groupId" instead.`
39-
});
38+
}];
4039
}
4140

42-
return errors;
41+
return [];
4342
} catch (e) {
4443
handleInternalError(RULE_NAME, path, e);
4544
}

0 commit comments

Comments
 (0)