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'], userRoleTypes: ['user', 'admin', 'superuser'],
regex: { 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]*))?)/, 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})$/, 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,}))$/, 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, type: String,
unique: true, unique: true,
lowercase: 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'] required: [true, 'Username is required']
}, },
passwordHash: { passwordHash: {

View File

@ -1,15 +1,20 @@
var constants = require('../app/libs/constants'); var constants = require('../app/libs/constants');
var validateEmail = function(value) { var createRegexValidator = function(regex){
var isValidEmail = new RegExp(constants.regex.email, 'g').test(value); return function(value) {
var isValid = new RegExp(regex, 'g').test(value);
if(!isValidEmail){ if(!isValid){
return 'Please enter a valid email' return 'Please enter a valid email'
} else { } else {
return true; return true;
}
} }
} }
var validateEmail = createRegexValidator(constants.regex.email);
var validateUsername = createRegexValidator(constants.regex.username);
module.exports = { module.exports = {
replaceENVQuestion: { replaceENVQuestion: {
type: 'confirm', type: 'confirm',
@ -181,7 +186,8 @@ module.exports = {
{ {
type: 'input', type: 'input',
name: 'username', 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', type: 'password',