I'd like to propose weird alternative to this:
function throwError(error) {
throw new Error(error);
}
async function createUser(user) {
validateUserInput(user) || throwError(err.userValidationFailed);
isPasswordValid(user.password) || throwError(err.invalidPassword);
!(await userService.getUserByEmail(user.email)) || throwError(err.userExists);
What if...
[
[() => validateUserInput(user), err.userValidationFailed],
[() => isPasswordValid(user.password), err.invalidPassword],
[() => !(await userService.getUserByEmail(user.email)), err.userExists],
].forEach(function([is_good, error]) {
if (!is_good()) {
throw new Error(error);
}
});
Also on the regex:
const rules = [/[a-z]{1,}/, /[A-Z]{1,}/, /[0-9]{1,}/, /\W{1,}/];
No one caught that in all four of these, "{1,}" could be replaced with the much more common "+". A bit odd considering the desire for brevity. I do personally prefer "[0-9]" over "\d", especially considering the other rules, but can go either way on "\W".
I might have also added a fifth regex for length though, instead of doing it differently, if my head was in that mode: /.{8,}/
I might have also added a fifth regex for length though, instead of doing it differently, if my head was in that mode: /.{8,}/