mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2026-03-10 21:25:04 +00:00
Made edit profile data validation more robust.
This commit is contained in:
@@ -3,15 +3,34 @@ const isEmpty = (str) => {
|
||||
else return false;
|
||||
};
|
||||
|
||||
exports.validateUpdateProfileInfo = (profileData) => {
|
||||
let errors = {}
|
||||
const isEmail = (str) => {
|
||||
const emailRegEx = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
|
||||
if (str.match(emailRegEx)) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
if (isEmpty(profileData.email)) {
|
||||
exports.validateUpdateProfileInfo = (data) => {
|
||||
let errors = {};
|
||||
let profileData = {};
|
||||
|
||||
// ?: Should users be able to change their handles and emails?
|
||||
|
||||
// Only adds the key to the DB if the values are not empty
|
||||
if (!isEmpty(data.firstName)) profileData.firstName = data.firstName.trim();
|
||||
if (!isEmpty(data.lastName)) profileData.lastName = data.lastName.trim();
|
||||
if (!isEmpty(data.bio)) profileData.bio = data.bio.trim();
|
||||
|
||||
if (isEmpty(data.email)) {
|
||||
errors.email = "Must not be empty.";
|
||||
} else if (!isEmail(data.email)) {
|
||||
errors.email = "Must be a valid email."
|
||||
} else {
|
||||
profileData.email = data.email;
|
||||
}
|
||||
|
||||
return {
|
||||
errors,
|
||||
valid: Object.keys(errors).length === 0 ? true : false
|
||||
valid: Object.keys(errors).length === 0 ? true : false,
|
||||
profileData
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user