-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.js
More file actions
46 lines (45 loc) · 1.74 KB
/
utility.js
File metadata and controls
46 lines (45 loc) · 1.74 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
const exportModule = {
checkValidJson: function (inputString) {
try {
JSON.parse(inputString);
return true;
} catch (error) {
return false;
}
},
isLoggedIn: function (req, res, next) {
if (req.user) {
next();
} else {
res.status(503).send();
}
},
checkObjectValidWithSchema: function (schema, testObject) {
try {
const resultObject = {};
schema.forEach((column) => {
const inputValue = testObject[column.columnKey];
if (inputValue !== undefined) {
if (!this.validateColumnDataType(inputValue, column.columnType)) throw Error('not valid');
this.validateColumnDataType(inputValue, column.columnType);
resultObject[column.columnKey] = inputValue;
} else {
resultObject[column.columnKey] = column.defaultValue;
}
});
return resultObject;
} catch (error) {
return null;
}
},
validateColumnDataType: function (inputValue, columnType) {
if (!['Number', 'Boolean', 'Array', 'Object', 'String'].includes(columnType)) return false;
if (columnType === 'Number' && typeof inputValue !== 'number') return false;
if (columnType === 'Boolean' && typeof inputValue !== 'boolean') return false;
if (columnType === 'Array' && !Array.isArray(inputValue)) return false;
if (columnType === 'Object' && typeof inputValue !== 'object') return false;
if (columnType === 'String' && typeof inputValue !== 'string') return false;
return true;
}
}
module.exports = exportModule;