-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathvalidator.js
More file actions
41 lines (37 loc) · 1.21 KB
/
validator.js
File metadata and controls
41 lines (37 loc) · 1.21 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
const fetch = require('node-fetch');
function includesAllowedVersions (versions, request) {
return versions.some(version => {
return request.toLowerCase().includes(version.toLowerCase());
});
}
function validateJson (query, isValidJson) {
const contentTypeHeaders = query.headers.find(header => header.name.toLowerCase().includes('content-type'));
// xml content will not pass test
if (contentTypeHeaders && !contentTypeHeaders.value.includes('xml')) {
try {
JSON.parse(query.postBody);
} catch (error) {
isValidJson = false;
}
}
return isValidJson;
}
async function validateLink (linkUrl) {
try {
const response = await fetch(linkUrl, { method: 'HEAD' });
return response.ok;
} catch (error) {
console.log(JSON.stringify(error, null, 2));
return false;
}
}
function hasWhiteSpace( sampleUrl ){
if(!sampleUrl) { return false }
const requestUrl = sampleUrl.split('?');
const whiteSpace = (requestUrl && requestUrl.length > 0) ? requestUrl[0].trim().indexOf(' ') : -1;
return whiteSpace === 1;
}
exports.validateJson = validateJson;
exports.includesAllowedVersions = includesAllowedVersions;
exports.validateLink = validateLink;
exports.hasWhiteSpace = hasWhiteSpace;