-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.js
59 lines (45 loc) · 1.42 KB
/
validation.js
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
// title and author are not empty and not longer than 256 characters
const validateString = str => {
if(str === "") return 'String is empty';
if(str.length > 256) return 'String length is greater than 256 characters';
return str;
};
// uri is valid
const validateUri = url => {
let re = /www.\w+.com|net|org|uk|gov|edu|club|tv|me|singles|io|ch|at|.co.uk/;
if(re.test(url) === false) return 'Invalid URI';
return url;
};
// points, comments and rank are integers >= 0
const validateInteger = int => {
if(Array.isArray(int) && int != undefined) return int.length;
if(int === undefined) return 0;
if(int < 0) return 'Point, comment or rank is less than 0';
return int;
};
// check cmd line input
const validateInput = arr => {
let printNo = parseInt(arr[2]);
// to avoid process exit when running npm test
if (arr.length== 0) return arr;
if(arr[0] != 'hackernews') {
console.log(`Please spell "hackernews" correctly. You spelled it as: ${arr[0]}`);
// process.exit();
};
if(arr[1] != '--post') {
console.log(`Please spell "--post" correctly. You typed it as: ${arr[1]}`);
process.exit();
};
if(arr[2] === undefined || printNo < 1 || printNo > 100) {
console.log(`Please input a valid number between 1 and 100`);
process.exit();
}
return arr;
}
// export validation for use in server.js
module.exports = {
validateString,
validateUri,
validateInteger,
validateInput
}