Skip to content

Commit 2fd8098

Browse files
CLOUDP-287245: IPA-109: Validate custom method must be GET or POST (#313)
1 parent c29cfec commit 2fd8098

File tree

4 files changed

+167
-1
lines changed

4 files changed

+167
-1
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import testRule from './__helpers__/testRule';
2+
import { DiagnosticSeverity } from '@stoplight/types';
3+
4+
testRule('xgen-IPA-109-custom-method-must-be-GET-or-POST', [
5+
{
6+
name: 'valid methods',
7+
document: {
8+
paths: {
9+
'/a/{exampleId}:method': {
10+
post: {},
11+
},
12+
'/a:method': {
13+
post: {},
14+
},
15+
'/b/{exampleId}:method': {
16+
get: {},
17+
},
18+
'/b:method': {
19+
get: {},
20+
},
21+
'/c/{exampleId}:method': {
22+
get: {},
23+
'x-xgen-IPA-exception': {},
24+
},
25+
'/c:method': {
26+
get: {},
27+
'x-xgen-IPA-exception': {},
28+
},
29+
},
30+
},
31+
errors: [],
32+
},
33+
{
34+
name: 'invalid methods',
35+
document: {
36+
paths: {
37+
'/a/{exampleId}:method': {
38+
put: {},
39+
},
40+
'/a:method': {
41+
put: {},
42+
},
43+
'/b/{exampleId}:method': {
44+
get: {},
45+
put: {},
46+
},
47+
'/b:method': {
48+
get: {},
49+
put: {},
50+
},
51+
'/c/{exampleId}:method': {
52+
post: {},
53+
get: {},
54+
put: {},
55+
},
56+
'/c:method': {
57+
post: {},
58+
get: {},
59+
put: {},
60+
},
61+
'/d/{exampleId}:method': {
62+
post: {},
63+
get: {},
64+
},
65+
'/d:method': {
66+
post: {},
67+
get: {},
68+
},
69+
},
70+
},
71+
errors: [
72+
{
73+
code: 'xgen-IPA-109-custom-method-must-be-GET-or-POST',
74+
message: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109',
75+
path: ['paths', '/a/{exampleId}:method'],
76+
severity: DiagnosticSeverity.Warning,
77+
},
78+
{
79+
code: 'xgen-IPA-109-custom-method-must-be-GET-or-POST',
80+
message: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109',
81+
path: ['paths', '/a:method'],
82+
severity: DiagnosticSeverity.Warning,
83+
},
84+
{
85+
code: 'xgen-IPA-109-custom-method-must-be-GET-or-POST',
86+
message: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109',
87+
path: ['paths', '/b/{exampleId}:method'],
88+
severity: DiagnosticSeverity.Warning,
89+
},
90+
{
91+
code: 'xgen-IPA-109-custom-method-must-be-GET-or-POST',
92+
message: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109',
93+
path: ['paths', '/b:method'],
94+
severity: DiagnosticSeverity.Warning,
95+
},
96+
{
97+
code: 'xgen-IPA-109-custom-method-must-be-GET-or-POST',
98+
message: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109',
99+
path: ['paths', '/c/{exampleId}:method'],
100+
severity: DiagnosticSeverity.Warning,
101+
},
102+
{
103+
code: 'xgen-IPA-109-custom-method-must-be-GET-or-POST',
104+
message: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109',
105+
path: ['paths', '/c:method'],
106+
severity: DiagnosticSeverity.Warning,
107+
},
108+
{
109+
code: 'xgen-IPA-109-custom-method-must-be-GET-or-POST',
110+
message: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109',
111+
path: ['paths', '/d/{exampleId}:method'],
112+
severity: DiagnosticSeverity.Warning,
113+
},
114+
{
115+
code: 'xgen-IPA-109-custom-method-must-be-GET-or-POST',
116+
message: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109',
117+
path: ['paths', '/d:method'],
118+
severity: DiagnosticSeverity.Warning,
119+
},
120+
],
121+
},
122+
]);
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
extends:
2+
- ./rulesets/IPA-005.yaml
23
- ./rulesets/IPA-102.yaml
34
- ./rulesets/IPA-104.yaml
4-
- ./rulesets/IPA-005.yaml
5+
- ./rulesets/IPA-109.yaml
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# IPA-109: Custom Methods
2+
# http://go/ipa/109
3+
4+
functions:
5+
- eachCustomMethodMustBeGetOrPost
6+
7+
rules:
8+
xgen-IPA-109-custom-method-must-be-GET-or-POST:
9+
description: 'The HTTP method for custom methods must be GET or POST. http://go/ipa/109'
10+
message: '{{error}} http://go/ipa/109'
11+
severity: warn
12+
given: '$.paths[*]'
13+
then:
14+
function: 'eachCustomMethodMustBeGetOrPost'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { isCustomMethod } from './utils/resourceEvaluation.js';
2+
3+
const ERROR_MESSAGE = 'The HTTP method for custom methods must be GET or POST.';
4+
const ERROR_RESULT = [{ message: ERROR_MESSAGE }];
5+
const VALID_METHODS = ['get', 'post'];
6+
const HTTP_METHODS = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'];
7+
8+
export default (input, opts, { path }) => {
9+
// Extract the path key (e.g., '/a/{exampleId}:method') from the JSONPath.
10+
let pathKey = path[1];
11+
12+
if (!isCustomMethod(pathKey)) return;
13+
14+
//Extract the keys which are equivalent of the http methods
15+
let keys = Object.keys(input);
16+
const httpMethods = keys.filter((key) => HTTP_METHODS.includes(key));
17+
18+
// Check for invalid methods
19+
if (httpMethods.some((method) => !VALID_METHODS.includes(method))) {
20+
return ERROR_RESULT;
21+
}
22+
23+
// Check for multiple valid methods
24+
const validMethodCount = httpMethods.filter((method) => VALID_METHODS.includes(method)).length;
25+
26+
if (validMethodCount > 1) {
27+
return ERROR_RESULT;
28+
}
29+
};

0 commit comments

Comments
 (0)