-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.js
182 lines (158 loc) · 5.79 KB
/
helpers.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//You can add and export any helper functions you want here. If you aren't using any, then you can just leave this file as is.
const bcrypt = require('bcryptjs');
const { ObjectId } = require('mongodb');
const parkNameList = require('./extra/constant').parkNameList;
const fs = require('fs');
function validDate(date) {
if (!/^\d{4}-\d{1,2}-\d{1,2}$/.test(date)) throw 'Date must be in the format YYYY-MM-DD';
let parts = date.split("-");
let day = parseInt(parts[2], 10);
let month = parseInt(parts[1], 10);
let year = parseInt(parts[0], 10);
if (year < 1899 || month == 0 || month > 12) throw 'Invalid date';
let daysInMonth = new Date(year, month, 0).getDate();
if (day < 1 || day > daysInMonth) throw 'Invalid date';
return true;
}
function validEmailAddr(email) {
if (typeof email !== 'string') throw 'Email must be a string';
if (email.trim().length === 0)
throw 'Email cannot be an empty string or just spaces';
var regx = /^([a-zA-Z0-9_\-])+@([a-zA-Z0-9_\-])+(\.[a-zA-Z0-9_\-])+$/;
if (regx.test(email)) throw 'Email address is not valid';
return true;
}
function validUserName(name) {
if (!name) throw 'You must provide an user name to search for';
if (typeof name !== 'string') throw 'User name must be a string';
if (name.trim().length === 0)
throw 'User name cannot be an empty string or just spaces';
let nameValidFlag = false;
let counter = 0;
for (let i = 0; i < name.length; i++) {
var ch = name.charAt(i);
if (!/[a-zA-Z]/g.test(ch) && isNaN(ch)) {
nameValidFlag = true;
break;
} else {
counter += 1;
}
}
if (counter < 4) {
throw 'User name is too short. User name should be at least 4 characters long.'
}
if (nameValidFlag) {
throw 'UserName-No empty spaces, no spaces in the username and only alphanumeric characters are allowed.'
}
return name
}
function validParkId(parkId) {
if (!parkId) throw 'You must provide an id to search for'
if (parkId.trim().length === 0)
throw 'id cannot be an empty string or just spaces'
parkId = parkId.trim()
return parkId
}
function isValidObjectId(objectId) {
if (!objectId) throw 'You must provide an id to search for';
if (typeof objectId !== 'string' && typeof objectId !== 'object')
throw `Id ${objectId} must be a string or ObjectId`;
if (typeof objectId === 'string') {
if (!ObjectId.isValid(objectId)) throw `Id ${objectId} is not a valid User idObjectId`;
}
if (objectId.trim().length === 0)
throw 'id cannot be an empty string or just spaces';
objectId = objectId.trim();
return objectId
}
function hashPassword(password) {
return bcrypt.hashSync(password, 16);
}
function checkPasswordString(password) {
if (typeof password != "string") {
throw 'password should be valid string.'
}
if (password.trim().length === 0) {
throw 'password should not be all empty spaces.'
}
}
function checkPassword(password) {
//The constraints for password will be: There needs to be at least one uppercase character,
//there needs to be at least one lowercase character,
//there has to be at least one number and there has to be at least one special character,
//at least 8 characters long
//var regex = /^[a-zA-Z0-9!@#\$%\^\&*\)\(+=._-]{8,}$/g;
for (let i = 0; i < password.length; i++) {
if (password.charAt(i) === " ") {
throw 'No spaces between password.'
}
}
let strongPassword = new RegExp('^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^A-Za-z0-9])(?=.{8,})')
if (!strongPassword.test(password)) {
throw 'Not valid password-At least one upper case character,at least one lower character,at least one number,at least one special character,at least 8 characters.'
}
}
function checkParkName(parkName) {
if (!parkName) throw 'You must provide an user name to search for';
if (typeof parkName !== 'string') throw 'User name must be a string';
if (parkName.trim().length === 0)
throw 'park name cannot be an empty string or just spaces';
}
function checkReviewID(reviewId) {
if (!reviewId) throw 'You must provide an id to search for';
if (typeof reviewId !== 'string' && typeof reviewId !== 'object')
throw 'Id must be a string or ObjectId';
if (typeof reviewId === 'string') {
if (!ObjectId.isValid(reviewId)) throw 'Id is not a valid ObjectId';
}
if (reviewId.trim().length === 0)
throw 'id cannot be an empty string or just spaces';
}
function checkparamsUpdateUser(userId, userName, email, birthDate, hashedPassword) {
if (!hashedPassword) throw 'User must have password'
if (!userId) throw 'You must provide an id to search for';
if (typeof userId !== 'string' && typeof userId !== 'object')
throw 'Id must be a string or ObjectId';
if (typeof userId === 'string') {
if (!ObjectId.isValid(userId)) throw 'Id is not a valid ObjectId';
}
if (userId.trim().length === 0)
throw 'id cannot be an empty string or just spaces';
userId = userId.trim();
userName = validUserName(userName)
if (!email) throw 'You must provide an email';
if (typeof email !== 'string') throw 'Email must be a string';
if (email.trim().length === 0)
throw 'Email cannot be an empty string or just spaces';
email = email.trim();
if (!validEmailAddr(email)) throw 'Email is not valid';
if (!birthDate) throw 'You must provide a birth date';
validDate(birthDate)
}
function changeParkName(parkName) {
for (let i = 0; i < parkNameList.length; i++) {
if (parkNameList[i].includes(parkName)) {
return parkNameList[i];
}
}
}
function image() {
let rawdata = fs.readFileSync("image.json");
let images = JSON.parse(rawdata);
return images;
}
module.exports = {
validDate,
validEmailAddr,
validUserName,
validParkId,
isValidObjectId,
hashPassword,
checkPassword,
checkPasswordString,
checkParkName,
checkReviewID,
checkparamsUpdateUser,
changeParkName,
image
}