-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.js
More file actions
101 lines (90 loc) · 3.03 KB
/
validation.js
File metadata and controls
101 lines (90 loc) · 3.03 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import {ObjectId} from 'mongodb';
const exportedMethods = {
checkId(id, varName) {
if (!id) throw `Error: You must provide a ${varName}`;
if (typeof id !== 'string') throw `Error:${varName} must be a string`;
id = id.trim();
if (id.length === 0)
throw `Error: ${varName} cannot be an empty string or just spaces`;
if (!ObjectId.isValid(id)) throw `Error: ${varName} invalid object ID`;
return id;
},
checkString(strVal, varName) {
if (!strVal) throw `Error: You must supply a ${varName}!`;
if (typeof strVal !== 'string') throw `Error: ${varName} must be a string!`;
strVal = strVal.trim();
if (strVal.length === 0)
throw `Error: ${varName} cannot be an empty string or string with just spaces`;
if (!isNaN(strVal))
throw `Error: ${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;
},
checkPassword(password, varName){
if(password.trim() === ""){
throw `${varName} cannot be empty`;
}
if(typeof password !== 'string'){
throw `${varName} must be of string type`;
}
if (!password) {
throw 'Password is required';
}
if (password.length < 8) {
throw 'Password must be at least 8 characters long';
}
if (!/[a-z]/.test(password)) {
throw 'Password must contain at least one lowercase letter';
}
if (!/[A-Z]/.test(password)) {
throw 'Password must contain at least one uppercase letter';
}
if (!/\d/.test(password)) {
throw 'Password must contain at least one number';
}
return password;
},
checkEmail(email, varName){
if(email.trim() === ""){
throw `${varName} cannot be empty`;
}
if(typeof email !== 'string'){
throw `${varName} must be a string`;
}
// const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const emailRegex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(!email.match(emailRegex)){
throw `The ${varName} must be a valid email address`;
}
return email;
},
checkAge(age, varName){
if (isNaN(age)) {
throw `${varName} must be a number`;
}
if (age < 18) {
throw "You must be at least 18 years old to use this website";
}
if (age > 120) {
throw "Invalid age";
}
return age;
},
checkZip(zip_code, varName){
if(isNaN(zip_code)){
throw `${varName} must be a number`;
}
return zip_code;
}
};
export default exportedMethods;