forked from twang1905/CS-546-Final-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.js
98 lines (86 loc) · 3.33 KB
/
validator.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
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
87
88
89
90
91
92
93
94
95
96
97
98
import { ObjectId } from "mongodb";
const exportedMethods = {
checkId(id, varName) {
if (!id) throw `You must provide a ${varName}`;
if (typeof id !== "string") throw `${varName} must be a string`;
id = id.trim();
if (id.length === 0)
throw `${varName} cannot be an empty string or just spaces`;
if (!ObjectId.isValid(id)) throw `${varName} invalid object ID`;
return id;
},
checkString(strVal, varName) {
if (!strVal) throw `You must supply a ${varName}!`;
if (typeof strVal !== "string") throw `${varName} must be a string!`;
strVal = strVal.trim();
if (strVal.length === 0)
throw `${varName} cannot be an empty string or string with just spaces`;
if (!isNaN(strVal))
throw `${strVal} is not a valid value for ${varName} as it only contains digits`;
return strVal;
},
checkStringArray(arr, varName) {
if (!arr || !Array.isArray(arr))
throw `You must provide an array of ${varName}`;
for (let i in arr) {
if (typeof arr[i] !== "string" || arr[i].trim().length === 0) {
throw `One or more elements in ${varName} array is not a string or is an empty string`;
}
arr[i] = arr[i].trim();
}
return arr;
},
checkEmail(email, varName) {
email = this.checkString(email, varName);
email = email.toLowerCase();
let atSplit = email.split("@");
if (atSplit.length !== 2)
throw `${varName} must have 1, and only 1, '@' character in it`;
let dotSplit = atSplit[1].split(".");
if (dotSplit.length < 2)
throw `${varName} must have at least 1 '.' after the '@' character`;
return email;
},
isObject(variable) {
return Object.prototype.toString.call(variable) === "[object Object]";
},
checkInt(intVal, varName) {
if (!intVal && intVal !== 0) throw `You must supply a ${varName}!`;
if (typeof intVal !== "number")
throw `${varName} must be an integer!`;
if (Math.trunc(intVal) !== intVal)
throw `${varName} must be an integer`;
return intVal;
},
checkNum(numVal, varName) {
if (!numVal && numVal !== 0) throw `You must supply a ${varName}`;
if (typeof numVal !== "number") throw `${varName} must be a number`;
return numVal;
},
checkPassword(password, varName) {
password = this.checkString(password, varName);
if (password.length < 8) throw `${varName} must be 8 characters or longer`;
if (!password.match(/[A-Z]/)) throw `${varName} must have a capital letter`;
if (!password.match(/[0-9]/)) throw `${varName} must have a number`;
if (!password.match(/[\W_]/))
throw `${varName} must have a special character`;
return password;
},
checkName(name, varName) {
name = this.checkString(name, varName);
if (name.length < 3 || name.length > 25)
throw `${varName} must be between 3 and 25 characters (inclusive)`;
if (name.match(/[^a-zA-Z\-]/))
throw `${varName} can only include letters and hyphens`;
return name;
},
checkUserName(userName, varName) {
userName = this.checkString(userName, varName);
if (userName.length < 3 || userName.length > 25)
throw `${varName} must be between 3 and 25 characters (inclusive)`;
if (userName.match(/[^\w-\_]/))
throw `${varName} can only include letters, numbers, hyphens, and underscores.`;
return userName.toLowerCase();
},
};
export default exportedMethods;