-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
58 lines (52 loc) · 1.85 KB
/
Copy pathutils.js
File metadata and controls
58 lines (52 loc) · 1.85 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
const { error_messages } = require("./constants");
const { alphanumericRegex, onlyAlphabets } = require("./regex");
/**
* @param {object} req
* @param {Object} rule
* @returns {*} value
* extracts value from header, param, body, query
*/
const extractValue = (req, rule) => {
if (!rule.from) throw new Error(`from ${error_messages.required_field_missing}`); // to extract from which type
if (!rule.key) throw new Error(`key ${error_messages.required_field_missing}`); // what is the key of the value
const { key, from } = rule;
switch (from) {
case "header":
return req.get(key);
case "param":
return req.params[key];
case "query":
return req.query[key];
case "body":
if (!req.body) return undefined;
return req.body[key];
}
}
/**
* takes a type and value and checks if the type required and datatype of value is matching
* @param {string} type
* @param {any} value
*/
const dataTypeCheck = (type, value) => {
switch (type.toLowerCase()) {
case "number":
case "int":
case "integer":
case "num":
return !isNaN(Number(value));
case "str":
case "string":
return typeof value == "string" && isNaN(Number(value)); // checks if typeof is string and first character is not a number
case "only-alpha":
return typeof value == "string" && isNaN(Number(value)) && onlyAlphabets(value);
case "alphanumeric":
return typeof value == "string" && isNaN(Number(value)) && alphanumericRegex(value);
case "float":
return !isNaN(Number(value)) && Number(value) % 1 != 0;
case "object":
case "obj":
return typeof value == "object";
}
}
exports.extractValue = extractValue;
exports.dataTypeCheck = dataTypeCheck;