-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.js
147 lines (133 loc) · 4.53 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
const {min_design, max_design, min_bg, max_bg, min_hat, max_hat} = require('./gameConstants').designLimits;
const {ObjectId} = require('mongodb');
// Checks that id (as a string) exists and is a valid mongoDB Object ID.
// Does *not* check that object with id exists!
function validateIdString(id){
if(!id){throw 'Error: Object ID must be provided.'}
if (typeof id !== 'string'){
throw 'Error: Object ID input must be a string.'
}
id = id.trim()
if (id.length === 0){
throw 'Error: Object ID input cannot be only whitespace.'
}
if (!ObjectId.isValid(id)){
throw 'Error: Object ID input is not a valid Object ID.'
}
return id;
}
function validateName(name, prefix){
if(!name){throw `Error: ${prefix} name must be provided.`}
if (typeof name !== 'string'){
throw `Error: ${prefix} name must be a string`
}
name = name.trim().toLowerCase()
if (name.length === 0){
throw `Error: ${prefix} name cannot be only whitespace.`
}
if (/[^a-z]/.test(name)){
throw `Error: ${prefix} name may only contain alphabetical characters.`
}
return name
}
function validateEmail(email){
if(!email){throw `Error: Email must be provided.`}
if (typeof email !== 'string'){
throw 'Error: Email must be a string.'
}
email = email.trim().toLowerCase()
if (email.length === 0){
throw 'Error: Email cannot be only whitespace'
}
// Source for email regex: https://www.w3resource.com/javascript/form/email-validation.php
if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
throw 'Error: Cannot parse email input.'
}
return email;
}
function validateUsername(username) {
if(!username){throw `Error: Username must be provided.`}
if (typeof username !== 'string'){
throw 'Error: Username must be a string.'
}
username = username.trim().toLowerCase();
if (username.length < 4){
throw 'Error: Username must be at least four characters (not counting whitespace).'
}
if (/[^a-z0-9]/.test(username)){
throw 'Error: Username may only contain alphanumeric characters.'
}
return username
}
function validatePassword(password) {
if(!password){throw 'Error: Password must be provided.'}
if (typeof password !== 'string'){
throw 'Error: Password must be a string.';
}
if (password.length < 6){
throw 'Error: Password must be at least six characters (not counting whitespace).';
}
if (/\s/.test(password)){
throw 'Error: Password may not contain whitespace.'
}
if (!/[A-Z]/.test(password) || !/[0-9]/.test(password) || !/[^A-Za-z0-9]/.test(password)){
throw 'Error: Password must contain at least one uppercase, \
one numeric, and one special character.'
}
return password;
}
function setUserSession(user) {
return {
id: user._id,
username: user.username,
points: user.points,
background: user.background,
hatsUnlocked: user.hatsUnlocked,
backgroundsUnlocked: user.backgroundsUnlocked
}
}
function validatePoints(points) {
if (typeof points !== 'number' || !Number.isInteger(points)){
throw 'Error: Points must be an integer.';
}
return points;
}
function validateDesignNumber(designNumber) {
if (typeof designNumber !== 'number' || !Number.isInteger(designNumber)){
throw 'Error: Design number must be an integer.';
}
if (designNumber < min_design || designNumber > max_design){
throw `Error: Design number must be between ${min_design} and ${max_design} inclusive.`
}
return designNumber;
}
function validateHatNumber(hatNumber) {
if (typeof hatNumber !== 'number' || !Number.isInteger(hatNumber)){
throw 'Error: Hat number must be an integer.';
}
if (hatNumber < min_hat || hatNumber > max_hat){
throw `Error: Hat number must be between ${min_hat} and ${max_hat} inclusive.`
}
return hatNumber;
}
function validateBgNumber(bgNumber) {
if (typeof bgNumber !== 'number' || !Number.isInteger(bgNumber)){
throw 'Error: Background number must be an integer.';
}
if (bgNumber < min_bg || bgNumber > max_bg){
throw `Error: Background number must be between ${min_bg} and ${max_bg} inclusive.`
}
return bgNumber;
}
module.exports = {
validateIdString,
validateName,
validateEmail,
validateUsername,
validatePassword,
setUserSession,
validatePoints,
validateDesignNumber,
validateHatNumber,
validateBgNumber
}