refactor: Replace while true with flag based validation
This commit is contained in:
@@ -48,32 +48,28 @@ const input = async (fieldName, options = {}) => {
|
|||||||
try {
|
try {
|
||||||
await knex.migrate.latest();
|
await knex.migrate.latest();
|
||||||
|
|
||||||
process.env.DEFAULT_ADMIN_EMAIL = await input('Email', {
|
let isEmailValid = false;
|
||||||
isRequired: true,
|
while (!isEmailValid) {
|
||||||
});
|
process.env.DEFAULT_ADMIN_EMAIL = await input('Email', {
|
||||||
|
isRequired: true,
|
||||||
|
});
|
||||||
|
|
||||||
// eslint-disable-next-line no-constant-condition
|
|
||||||
while (true) {
|
|
||||||
if (
|
if (
|
||||||
validator.isEmail(process.env.DEFAULT_ADMIN_EMAIL) &&
|
!validator.isEmail(process.env.DEFAULT_ADMIN_EMAIL) ||
|
||||||
process.env.DEFAULT_ADMIN_EMAIL.length <= 256
|
process.env.DEFAULT_ADMIN_EMAIL.length > 256
|
||||||
) {
|
) {
|
||||||
|
console.log('Email must be a valid e-mail address with no more than 256 characters.');
|
||||||
|
} else {
|
||||||
const existingUser = await knex('user_account')
|
const existingUser = await knex('user_account')
|
||||||
.where('email', process.env.DEFAULT_ADMIN_EMAIL.toLowerCase())
|
.where('email', process.env.DEFAULT_ADMIN_EMAIL.toLowerCase())
|
||||||
.first();
|
.first();
|
||||||
|
|
||||||
if (!existingUser) {
|
if (existingUser) {
|
||||||
break;
|
console.log('Email is already in use.');
|
||||||
|
} else {
|
||||||
|
isEmailValid = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Email is already in use.');
|
|
||||||
} else {
|
|
||||||
console.log('Email must be a valid e-mail address with no more than 256 characters.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
process.env.DEFAULT_ADMIN_EMAIL = await input('Email', {
|
|
||||||
isRequired: true,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
process.env.DEFAULT_ADMIN_PASSWORD = await input('Password', {
|
process.env.DEFAULT_ADMIN_PASSWORD = await input('Password', {
|
||||||
@@ -85,7 +81,6 @@ const input = async (fieldName, options = {}) => {
|
|||||||
isRequired: true,
|
isRequired: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// eslint-disable-next-line no-constant-condition
|
|
||||||
while (process.env.DEFAULT_ADMIN_NAME.length > 128) {
|
while (process.env.DEFAULT_ADMIN_NAME.length > 128) {
|
||||||
console.log('Name must be no more than 128 characters.');
|
console.log('Name must be no more than 128 characters.');
|
||||||
process.env.DEFAULT_ADMIN_NAME = await input('Name', {
|
process.env.DEFAULT_ADMIN_NAME = await input('Name', {
|
||||||
@@ -93,38 +88,34 @@ const input = async (fieldName, options = {}) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
process.env.DEFAULT_ADMIN_USERNAME = await input('Username');
|
const USERNAME_REGEX = /^[a-zA-Z0-9]+((_|\.)?[a-zA-Z0-9])*$/;
|
||||||
|
|
||||||
if (process.env.DEFAULT_ADMIN_USERNAME) {
|
let isUsernameValid = false;
|
||||||
const USERNAME_REGEX = /^[a-zA-Z0-9]+((_|\.)?[a-zA-Z0-9])*$/;
|
while (!isUsernameValid) {
|
||||||
|
process.env.DEFAULT_ADMIN_USERNAME = await input('Username');
|
||||||
|
|
||||||
// eslint-disable-next-line no-constant-condition
|
if (!process.env.DEFAULT_ADMIN_USERNAME) {
|
||||||
while (true) {
|
break;
|
||||||
const isValid =
|
}
|
||||||
process.env.DEFAULT_ADMIN_USERNAME.length >= 3 &&
|
|
||||||
process.env.DEFAULT_ADMIN_USERNAME.length <= 32 &&
|
|
||||||
USERNAME_REGEX.test(process.env.DEFAULT_ADMIN_USERNAME);
|
|
||||||
|
|
||||||
if (isValid) {
|
const isValid =
|
||||||
const existingUser = await knex('user_account')
|
process.env.DEFAULT_ADMIN_USERNAME.length >= 3 &&
|
||||||
.where('username', process.env.DEFAULT_ADMIN_USERNAME.toLowerCase())
|
process.env.DEFAULT_ADMIN_USERNAME.length <= 32 &&
|
||||||
.first();
|
USERNAME_REGEX.test(process.env.DEFAULT_ADMIN_USERNAME);
|
||||||
|
|
||||||
if (!existingUser) {
|
if (!isValid) {
|
||||||
break;
|
console.log(
|
||||||
}
|
'Username must be 3-32 characters and contain only letters, digits, underscores, and dots (e.g., john_doe).',
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
const existingUser = await knex('user_account')
|
||||||
|
.where('username', process.env.DEFAULT_ADMIN_USERNAME.toLowerCase())
|
||||||
|
.first();
|
||||||
|
|
||||||
|
if (existingUser) {
|
||||||
console.log('Username is already in use.');
|
console.log('Username is already in use.');
|
||||||
} else {
|
} else {
|
||||||
console.log(
|
isUsernameValid = true;
|
||||||
'Username must be 3-32 characters and contain only letters, digits, underscores, and dots (e.g., john_doe).',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
process.env.DEFAULT_ADMIN_USERNAME = await input('Username');
|
|
||||||
|
|
||||||
if (!process.env.DEFAULT_ADMIN_USERNAME) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user