Skip to content

Commit 571c2dd

Browse files
CLOUDP-306576: IPA-119: Multi-Cloud Support by Default
1 parent a07466e commit 571c2dd

File tree

5 files changed

+237
-0
lines changed

5 files changed

+237
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-119-no-default-for-cloud-providers', [
5+
{
6+
name: 'valid when no default in cloud provider field',
7+
document: {
8+
components: {
9+
schemas: {
10+
Schema: {
11+
properties: {
12+
cloudProvider: {
13+
type: 'string',
14+
enum: ['AWS', 'GCP', 'AZURE'],
15+
},
16+
},
17+
},
18+
},
19+
},
20+
},
21+
errors: [],
22+
},
23+
{
24+
name: 'invalid when cloud provider field has default value',
25+
document: {
26+
components: {
27+
schemas: {
28+
Schema: {
29+
properties: {
30+
cloudProvider: {
31+
type: 'string',
32+
enum: ['AWS', 'GCP', 'AZURE'],
33+
default: 'AWS',
34+
},
35+
},
36+
},
37+
},
38+
},
39+
},
40+
errors: [
41+
{
42+
code: 'xgen-IPA-119-no-default-for-cloud-providers',
43+
message: 'When using a provider field or param, API producers should not define a default value.',
44+
path: ['components', 'schemas', 'Schema', 'properties', 'cloudProvider'],
45+
severity: DiagnosticSeverity.Warning,
46+
},
47+
],
48+
},
49+
{
50+
name: 'valid when non-provider field has default value',
51+
document: {
52+
components: {
53+
schemas: {
54+
Schema: {
55+
properties: {
56+
region: {
57+
type: 'string',
58+
default: 'us-east-1',
59+
},
60+
},
61+
},
62+
},
63+
},
64+
},
65+
errors: [],
66+
},
67+
{
68+
name: 'field with provider in name has exception',
69+
document: {
70+
components: {
71+
schemas: {
72+
Schema: {
73+
properties: {
74+
cloudProvider: {
75+
type: 'string',
76+
enum: ['AWS', 'GCP', 'AZURE'],
77+
default: 'AWS',
78+
'x-xgen-IPA-exception': {
79+
'xgen-IPA-119-no-default-for-cloud-providers': 'Reason',
80+
},
81+
},
82+
},
83+
},
84+
},
85+
},
86+
},
87+
errors: [],
88+
},
89+
{
90+
name: 'parameters with provider in name',
91+
document: {
92+
paths: {
93+
'/resources': {
94+
get: {
95+
parameters: [
96+
{
97+
name: 'cloudProvider',
98+
in: 'query',
99+
schema: {
100+
type: 'string',
101+
default: 'AWS',
102+
},
103+
},
104+
],
105+
},
106+
},
107+
},
108+
},
109+
errors: [
110+
{
111+
code: 'xgen-IPA-119-no-default-for-cloud-providers',
112+
message: 'When using a provider field or param, API producers should not define a default value.',
113+
path: ['paths', '/resources', 'get', 'parameters', '0'],
114+
severity: DiagnosticSeverity.Warning,
115+
},
116+
],
117+
},
118+
{
119+
name: 'parameters with provider in name - exceptions',
120+
document: {
121+
paths: {
122+
'/resources': {
123+
get: {
124+
parameters: [
125+
{
126+
name: 'cloudProvider',
127+
in: 'query',
128+
schema: {
129+
type: 'string',
130+
default: 'AWS',
131+
},
132+
'x-xgen-IPA-exception': {
133+
'xgen-IPA-119-no-default-for-cloud-providers': 'Reason',
134+
},
135+
},
136+
],
137+
},
138+
},
139+
},
140+
},
141+
errors: [],
142+
},
143+
]);

tools/spectral/ipa/ipa-spectral.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extends:
1212
- ./rulesets/IPA-113.yaml
1313
- ./rulesets/IPA-114.yaml
1414
- ./rulesets/IPA-117.yaml
15+
- ./rulesets/IPA-119.yaml
1516
- ./rulesets/IPA-123.yaml
1617
- ./rulesets/IPA-125.yaml
1718

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# IPA-119: Multi-Cloud Support by Default
2+
# http://go/ipa/119
3+
4+
functions:
5+
- IPA119NoDefaultForCloudProviders
6+
rules:
7+
xgen-IPA-119-no-default-for-cloud-providers:
8+
description: |
9+
When using a provider field or parameter, API producers should not define a default value.
10+
As providers are added, having a default value can impact usability.
11+
This rule checks fields containing "provider" or "cloudProvider" and ensures they do not have a default value.
12+
It also checks enum fields that might contain cloud provider values.
13+
severity: warn
14+
message: '{{error}} https://mdb.link/mongodb-atlas-openapi-validation#xgen-IPA-119-no-default-for-cloud-providers'
15+
given:
16+
# Target properties with "cloudProvider" in their name
17+
- '$.paths[*][get,put,post,delete,options,head,patch,trace].parameters'
18+
- '$.paths[*][get,put,post,delete,options,head,patch,trace]..content..properties'
19+
- '$.components.schemas..properties'
20+
then:
21+
field: '@key'
22+
function: IPA119NoDefaultForCloudProviders
23+
functionOptions:
24+
propertyNameToLookFor: 'cloudProvider'

tools/spectral/ipa/rulesets/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,20 @@ The rule checks for the presence of the `schema`, `examples` or `example` proper
772772

773773

774774

775+
### IPA-119
776+
777+
Rules are based on [http://go/ipa/IPA-119](http://go/ipa/IPA-119).
778+
779+
#### xgen-IPA-119-no-default-for-cloud-providers
780+
781+
![warn](https://img.shields.io/badge/warning-yellow)
782+
When using a provider field or parameter, API producers should not define a default value.
783+
As providers are added, having a default value can impact usability.
784+
This rule checks fields containing "provider" or "cloudProvider" and ensures they do not have a default value.
785+
It also checks enum fields that might contain cloud provider values.
786+
787+
788+
775789
### IPA-123
776790

777791
Rules are based on [http://go/ipa/IPA-123](http://go/ipa/IPA-123).
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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-119-no-default-for-cloud-providers';
11+
const ERROR_MESSAGE = 'When using a provider field or param, API producers should not define a default value.';
12+
export default (input, { propertyNameToLookFor }, { path, documentInventory }) => {
13+
const oas = documentInventory.resolved;
14+
const propertyObject = resolveObject(oas, path);
15+
const fieldType = path[path.length - 2];
16+
17+
if (hasException(propertyObject, RULE_NAME)) {
18+
collectException(propertyObject, RULE_NAME, path);
19+
return;
20+
}
21+
22+
const errors = checkViolationsAndReturnErrors(input, propertyObject, path, propertyNameToLookFor, fieldType);
23+
if (errors.length !== 0) {
24+
return collectAndReturnViolation(path, RULE_NAME, errors);
25+
}
26+
collectAdoption(path, RULE_NAME);
27+
};
28+
29+
function checkViolationsAndReturnErrors(propertyName, propertyObject, path, propertyNameToLookFor, fieldType) {
30+
try {
31+
if (fieldType === 'properties') {
32+
if (propertyName === propertyNameToLookFor && propertyObject.default !== undefined) {
33+
return [
34+
{
35+
path,
36+
message: ERROR_MESSAGE,
37+
},
38+
];
39+
}
40+
} else if (fieldType === 'parameters') {
41+
if (propertyObject.name === propertyNameToLookFor && propertyObject.schema.default !== undefined) {
42+
return [
43+
{
44+
path,
45+
message: ERROR_MESSAGE,
46+
},
47+
];
48+
}
49+
}
50+
51+
return [];
52+
} catch (e) {
53+
handleInternalError(RULE_NAME, path, e);
54+
}
55+
}

0 commit comments

Comments
 (0)