added username validation to setup script

This commit is contained in:
David Baldwynn 2017-11-21 12:33:47 -08:00
parent e27f27f7f0
commit c97f207699
3 changed files with 15 additions and 8 deletions

View File

@ -98,6 +98,7 @@ var constants = module.exports = {
userRoleTypes: ['user', 'admin', 'superuser'],
regex: {
username: /^[a-zA-Z0-9\-]+$/,
url: /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/,
hexCode: /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/,
email: /^(([^<>()\[\]\\.,;:\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,}))$/,

View File

@ -38,7 +38,7 @@ var UserSchema = new Schema({
type: String,
unique: true,
lowercase: true,
match: [/^[a-zA-Z0-9\-]+$/, 'Username can only contain alphanumeric characters and \'-\''],
match: [constants.regex.username, 'Username can only contain alphanumeric characters and \'-\''],
required: [true, 'Username is required']
},
passwordHash: {

View File

@ -1,15 +1,20 @@
var constants = require('../app/libs/constants');
var validateEmail = function(value) {
var isValidEmail = new RegExp(constants.regex.email, 'g').test(value);
var createRegexValidator = function(regex){
return function(value) {
var isValid = new RegExp(regex, 'g').test(value);
if(!isValidEmail){
return 'Please enter a valid email'
} else {
return true;
if(!isValid){
return 'Please enter a valid email'
} else {
return true;
}
}
}
var validateEmail = createRegexValidator(constants.regex.email);
var validateUsername = createRegexValidator(constants.regex.username);
module.exports = {
replaceENVQuestion: {
type: 'confirm',
@ -181,7 +186,8 @@ module.exports = {
{
type: 'input',
name: 'username',
message: 'What should be the username for your admin account?'
message: 'What should be the username for your admin account?',
validate: validateUsername
},
{
type: 'password',