-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
86 lines (75 loc) · 2.84 KB
/
Copy pathindex.js
File metadata and controls
86 lines (75 loc) · 2.84 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
/**
* Checks a variable against a list of parameters
* @param {object[]} configs - a list of objects containing variables to be validated and the rules for validation
* @param {string} configs[].variableValue - the value of the variable to be checked
* @param {string} configs[].variableText - the name of the variable to be used in the error return strings.
* @param {number} configs[].min - the minimum characters the variable should be
* @param {number} configs[].max - the maximum characters the variable should be
* @returns {string[]} - array of strings containing errors
*/
validateSize = configs => {
let errors = [];
for (let i = 0; i < configs.length; i++) {
if (configs[i].variableValue.length < configs[i].min) {
errors.push(
configs[i].variableText +
" must be a minimum of " +
configs[i].min +
" letters."
);
} else if (configs[i].variableValue.length > configs[i].max) {
errors.push(
configs[i].variableText +
" must be a maximum of " +
configs[i].max +
" letters."
);
}
}
return errors;
};
/**
* Checks a request body against an array of required variables
* @param {object} body - the express request.body object
* @param {object[]} variables - the variables that are required in the request
* @param {string} variables[].variable - the name of the variable to be checked
* @param {string} variables[].variableText - the name of the variable to be used in the return string.
* @returns {string[]} - a list of strings containing errors
*/
validateRequest = (body, variables) => {
let existenceErrors = [];
for (let i = 0; i < variables.length; i++) {
if (
!body.hasOwnProperty(variables[i].variable) ||
body[variables[i].variable] === ""
) {
existenceErrors.push("please enter " + variables[i].variableText);
}
}
return existenceErrors;
};
/**
* Takes an express request body and a config object and validates it.
* @param {object} body - an express request.body object
* @param {object[]} configs - a list of objects containing variables to be validated and the rules for validation
* @param {string} configs[].variable - the name of the variable to be checked
* @param {string} configs[].variableText - the name of the variable to be used in the error return strings.
* @param {number} configs[].min - the minimum characters the variable should be
* @param {number} configs[].max - the maximum characters the variable should be
* @returns {string[]} - a list of strings containing errors
*/
validate = (body, configs) => {
let errors = validateRequest(body, configs);
if (errors == []) {
return errors;
}
//Assign variableValue only after checking if the body actually has these values.
for (let i = 0; i < configs.length; i++) {
configs[i].variableValue = body[configs[i].variable];
}
errors = validateSize(configs);
return errors;
};
module.exports = {
validate
};