Skip to content

Commit 9329353

Browse files
author
Mike Kistler
committed
Refactor operationId naming convention check
1 parent e91e156 commit 9329353

File tree

2 files changed

+25
-37
lines changed

2 files changed

+25
-37
lines changed

src/plugins/validation/2and3/semantic-validators/operation-ids.js

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -68,69 +68,51 @@ module.exports.validate = function({ resolvedSpec }, config) {
6868
// - paths that do not end with param has a GET and POST operation
6969
// - paths that end with param has a GET, DELETE, POST, PUT or PATCH.
7070

71-
let checkPassed = true;
7271
const verbs = [];
7372

7473
if (!pathEndsWithParam) {
7574
// operationId for GET should starts with "list"
76-
if (opKey === 'get' && !operationId.match(/^list[a-zA-Z0-9_]+/m)) {
77-
checkPassed = false;
75+
if (opKey === 'get') {
7876
verbs.push('list');
7977
}
80-
8178
// operationId for POST should starts with "create" or "add"
82-
else if (
83-
opKey === 'post' &&
84-
!operationId.match(/^(add|create)[a-zA-Z0-9_]+/m)
85-
) {
86-
checkPassed = false;
79+
else if (opKey === 'post') {
8780
verbs.push('add');
8881
verbs.push('create');
8982
}
9083
} else {
9184
// operationId for GET should starts with "get"
92-
if (opKey === 'get' && !operationId.match(/^get[a-zA-Z0-9_]+/m)) {
93-
checkPassed = false;
85+
if (opKey === 'get') {
9486
verbs.push('get');
9587
}
96-
9788
// operationId for DELETE should starts with "delete"
98-
else if (
99-
opKey === 'delete' &&
100-
!operationId.match(/^delete[a-zA-Z0-9_]+/m)
101-
) {
102-
checkPassed = false;
89+
else if (opKey === 'delete') {
10390
verbs.push('delete');
10491
}
105-
10692
// operationId for PATCH should starts with "update"
107-
else if (
108-
opKey === 'patch' &&
109-
!operationId.match(/^update[a-zA-Z0-9_]+/m)
110-
) {
111-
checkPassed = false;
93+
else if (opKey === 'patch') {
11294
verbs.push('update');
113-
} else if (opKey === 'post') {
114-
// If PATCH operation doesn't exist for path, POST operationId should start with "update"
115-
if (
116-
!allPathOperations.includes('patch') &&
117-
!operationId.match(/^update[a-zA-Z0-9_]+/m)
118-
) {
119-
checkPassed = false;
95+
}
96+
// If PATCH operation doesn't exist for path, POST operationId should start with "update"
97+
else if (opKey === 'post') {
98+
if (!allPathOperations.includes('patch')) {
12099
verbs.push('update');
121100
}
122101
}
123-
124102
// operationId for PUT should starts with "replace"
125-
else if (
126-
opKey === 'put' &&
127-
!operationId.match(/^replace[a-zA-Z0-9_]+/m)
128-
) {
129-
checkPassed = false;
103+
else if (opKey === 'put') {
130104
verbs.push('replace');
131105
}
132106
}
133-
return { checkPassed, verbs };
107+
108+
if (verbs.length > 0) {
109+
const checkPassed = verbs
110+
.map(verb => operationId.startsWith(verb))
111+
.some(v => v);
112+
return { checkPassed, verbs };
113+
}
114+
115+
return { checkPassed: true };
134116
};
135117

136118
operations.forEach(op => {

test/plugins/validation/2and3/operation-ids.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ describe('validation plugin - semantic - operation-ids', function() {
184184
},
185185
post: {
186186
operationId: 'addBooks'
187+
},
188+
delete: {
189+
operationId: 'deleteAllBooks'
187190
}
188191
},
189192
'/coffee': {
@@ -215,6 +218,9 @@ describe('validation plugin - semantic - operation-ids', function() {
215218
},
216219
patch: {
217220
operationId: 'updateBook'
221+
},
222+
head: {
223+
operationId: 'headBook'
218224
}
219225
},
220226
'/coffee/{id}': {

0 commit comments

Comments
 (0)