2015-12-11 04:44:21 +00:00
|
|
|
var user = require('./user.js');
|
|
|
|
var Config = require('./config.js').config;
|
|
|
|
|
2015-12-12 22:52:56 +00:00
|
|
|
exports.validateNonEmpty = validateNonEmpty;
|
|
|
|
exports.validateMessageSubject = validateMessageSubject;
|
2015-12-11 04:44:21 +00:00
|
|
|
exports.validateUserNameAvail = validateUserNameAvail;
|
2016-07-25 07:01:14 +00:00
|
|
|
exports.validateUserNameExists = validateUserNameExists;
|
2015-12-11 04:44:21 +00:00
|
|
|
exports.validateEmailAvail = validateEmailAvail;
|
|
|
|
exports.validateBirthdate = validateBirthdate;
|
|
|
|
exports.validatePasswordSpec = validatePasswordSpec;
|
|
|
|
|
2015-12-12 22:52:56 +00:00
|
|
|
function validateNonEmpty(data, cb) {
|
|
|
|
cb(data && data.length > 0 ? null : new Error('Field cannot be empty'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function validateMessageSubject(data, cb) {
|
|
|
|
cb(data && data.length > 1 ? null : new Error('Subject too short'));
|
2015-12-24 18:55:37 +00:00
|
|
|
}
|
2015-12-12 22:52:56 +00:00
|
|
|
|
2015-12-11 04:44:21 +00:00
|
|
|
function validateUserNameAvail(data, cb) {
|
|
|
|
if(data.length < Config.users.usernameMin) {
|
|
|
|
cb(new Error('Username too short'));
|
|
|
|
} else if(data.length > Config.users.usernameMax) {
|
|
|
|
// generally should be unreached due to view restraints
|
|
|
|
cb(new Error('Username too long'));
|
|
|
|
} else {
|
|
|
|
var usernameRegExp = new RegExp(Config.users.usernamePattern);
|
|
|
|
var invalidNames = Config.users.newUserNames + Config.users.badUserNames;
|
|
|
|
|
|
|
|
if(!usernameRegExp.test(data)) {
|
|
|
|
cb(new Error('Username contains invalid characters'));
|
|
|
|
} else if(invalidNames.indexOf(data.toLowerCase()) > -1) {
|
|
|
|
cb(new Error('Username is blacklisted'));
|
|
|
|
} else {
|
|
|
|
user.getUserIdAndName(data, function userIdAndName(err) {
|
|
|
|
if(!err) { // err is null if we succeeded -- meaning this user exists already
|
|
|
|
cb(new Error('Userame unavailable'));
|
|
|
|
} else {
|
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 07:01:14 +00:00
|
|
|
function validateUserNameExists(data, cb) {
|
|
|
|
const invalidUserNameError = new Error('Invalid username');
|
|
|
|
|
|
|
|
if(0 === data.length) {
|
|
|
|
return cb(invalidUserNameError);
|
|
|
|
}
|
|
|
|
|
|
|
|
user.getUserIdAndName(data, (err) => {
|
|
|
|
return cb(err ? invalidUserNameError : null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-11 04:44:21 +00:00
|
|
|
function validateEmailAvail(data, cb) {
|
2015-12-12 22:52:56 +00:00
|
|
|
//
|
|
|
|
// This particular method allows empty data - e.g. no email entered
|
|
|
|
//
|
|
|
|
if(!data || 0 === data.length) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Otherwise, it must be a valid email. We'll be pretty lose here, like
|
|
|
|
// the HTML5 spec.
|
|
|
|
//
|
|
|
|
// See http://stackoverflow.com/questions/7786058/find-the-regex-used-by-html5-forms-for-validation
|
|
|
|
//
|
|
|
|
var emailRegExp = /[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9-]+(.[a-z0-9-]+)*/;
|
|
|
|
if(!emailRegExp.test(data)) {
|
|
|
|
return cb(new Error('Invalid email address'));
|
|
|
|
}
|
|
|
|
|
2015-12-11 04:44:21 +00:00
|
|
|
user.getUserIdsWithProperty('email_address', data, function userIdsWithEmail(err, uids) {
|
|
|
|
if(err) {
|
|
|
|
cb(new Error('Internal system error'));
|
|
|
|
} else if(uids.length > 0) {
|
|
|
|
cb(new Error('Email address not unique'));
|
|
|
|
} else {
|
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function validateBirthdate(data, cb) {
|
|
|
|
// :TODO: check for dates in the future, or > reasonable values
|
|
|
|
cb(isNaN(Date.parse(data)) ? new Error('Invalid birthdate') : null);
|
|
|
|
}
|
|
|
|
|
|
|
|
function validatePasswordSpec(data, cb) {
|
|
|
|
cb((!data || data.length < Config.users.passwordMin) ? new Error('Password too short') : null);
|
|
|
|
}
|