-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidationHelpers.js
More file actions
138 lines (122 loc) · 5.78 KB
/
validationHelpers.js
File metadata and controls
138 lines (122 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
function checkPathAndOperation(paths, targetOperation) {
// Check if the specified operation exists in the paths
if (paths && paths[targetOperation.path] && paths[targetOperation.path][targetOperation.method.toLowerCase()]) {
console.log(`Operation ${targetOperation.method} ${targetOperation.path} found in the OpenAPI Specification.`);
return true;
} else {
console.log(`Operation ${targetOperation.method} ${targetOperation.path} not found in the OpenAPI Specification.`);
return false;
}
}
// Check that the open api document specifies a request body for the specified operation
function checkRequestBodyExists(paths, targetOperation) {
// Check if the specified operation has a request body
if (paths &&
paths[targetOperation.path] &&
paths[targetOperation.path][targetOperation.method.toLowerCase()] &&
paths[targetOperation.path][targetOperation.method.toLowerCase()].requestBody) {
console.log(`Operation ${targetOperation.method} ${targetOperation.path} has a request body.`);
return true;
} else {
console.log(`Operation ${targetOperation.method} ${targetOperation.path} does not have a request body.`);
return false;
}
}
// Check that the open api document specifies a response body for the specified operation
function checkResponseBodyExists(paths, targetOperation) {
// Check if the specified operation has a response body
if (paths &&
paths[targetOperation.path] &&
paths[targetOperation.path][targetOperation.method.toLowerCase()] &&
paths[targetOperation.path][targetOperation.method.toLowerCase()].responses) {
console.log(`Operation ${targetOperation.method} ${targetOperation.path} has a response body.`);
return true;
} else {
console.log(`Operation ${targetOperation.method} ${targetOperation.path} does not have a response body.`);
return false;
}
}
function checkBodyProperties(body, targetOperation, properties, bodyType) {
const bodyContent = body.content;
const bodyContentApplicationJson = bodyContent['application/json'];
const bodyContentApplicationJsonSchema = bodyContentApplicationJson.schema;
const bodyContentApplicationJsonSchemaProperties = bodyContentApplicationJsonSchema.properties;
// Check if the request body has the specified properties
if (bodyContentApplicationJsonSchemaProperties) {
let result = true;
for (const property of properties) {
if (!bodyContentApplicationJsonSchemaProperties[property]) {
console.log(`Operation ${targetOperation.method} ${targetOperation.path} does not have property ${property} in the ${bodyType}.`);
result = false;
} else {
console.log(`Operation ${targetOperation.method} ${targetOperation.path} has property ${property} in the ${bodyType}.`);
}
}
return result;
} else {
console.log(`Operation ${targetOperation.method} ${targetOperation.path} does not have properties in the ${bodyType}.`);
return false;
}
}
// Check that the open api document specifies given properties for the speficied operation request body
function checkRequestBodyProperties(paths, targetOperation, properties) {
// Check if the specified operation has a request body
if (checkRequestBodyExists(paths, targetOperation)) {
const requestBody = paths[targetOperation.path][targetOperation.method.toLowerCase()].requestBody;
return checkBodyProperties(requestBody, targetOperation, properties, 'request body');
} else {
return false;
}
}
function checkResponseBodyProperties(paths, targetOperation, properties) {
// Check if the specified operation has a response body
if (checkResponseBodyExists(paths, targetOperation)) {
const responseBody = paths[targetOperation.path][targetOperation.method.toLowerCase()].responses['200'];
return checkBodyProperties(responseBody, targetOperation, properties, 'response body');
} else {
return false;
}
};
function checkResponseBodyIsAnArray(paths, targetOperation) {
// Check if the specified operation has a response body
if (checkResponseBodyExists(paths, targetOperation)) {
const responseBody = paths[targetOperation.path][targetOperation.method.toLowerCase()].responses['200'];
const bodyContent = responseBody.content;
const bodyContentApplicationJson = bodyContent['application/json'];
const bodyContentApplicationJsonSchema = bodyContentApplicationJson.schema;
const bodyContentApplicationJsonSchemaType = bodyContentApplicationJsonSchema.type;
// Check if the response body is an array
if (bodyContentApplicationJsonSchemaType === 'array') {
console.log(`Operation ${targetOperation.method} ${targetOperation.path} has a response body that is an array.`);
return true;
} else {
console.log(`Operation ${targetOperation.method} ${targetOperation.path} does not have a response body that is an array.`);
return false;
}
} else {
return false;
}
}
function verifyCustomerSchema(schemas) {
const customerSchema = schemas.Customer;
const customerSchemaProperties = customerSchema.properties;
const customerSchemaPropertiesContactPerson = customerSchemaProperties.contactPerson;
const customerSchemaPropertiesContactPersonProperties = customerSchemaPropertiesContactPerson.properties;
const result = customerSchemaProperties.id &&
customerSchemaProperties.companyName &&
customerSchemaProperties.companyAddress &&
customerSchemaPropertiesContactPersonProperties.name &&
customerSchemaPropertiesContactPersonProperties.phoneNumber &&
customerSchemaPropertiesContactPersonProperties.email &&
customerSchemaPropertiesContactPersonProperties.role;
return result;
}
module.exports = {
checkPathAndOperation,
checkRequestBodyExists,
checkResponseBodyExists,
checkRequestBodyProperties,
checkResponseBodyProperties,
checkResponseBodyIsAnArray,
verifyCustomerSchema
};