fix: valid username lowercase (#42172)

* fix: valid username lowercase

* test: add test in the validate for reject uppercase characters

* test: add test in the cypress for check on uppercase characters and reject it.
This commit is contained in:
Lance
2021-05-20 02:04:00 -05:00
committed by GitHub
parent 938e5913fa
commit 9c86168b2f
5 changed files with 35 additions and 5 deletions

View File

@ -8,15 +8,20 @@ const usernameIsHttpStatusCode = {
valid: false,
error: 'is a reserved error code'
};
const usernameUpperCase = { valid: false, error: 'must be lowercase' };
const isNumeric = num => !isNaN(num);
const validCharsRE = /^[a-zA-Z0-9\-_+]*$/;
const isHttpStatusCode = str =>
isNumeric(str) && parseInt(str, 10) >= 100 && parseInt(str, 10) <= 599;
const isUsernameLowercase = str => {
return str === str.toLowerCase();
};
const isValidUsername = str => {
if (!validCharsRE.test(str)) return invalidCharError;
if (str.length < 3) return usernameTooShort;
if (isHttpStatusCode(str)) return usernameIsHttpStatusCode;
if (!isUsernameLowercase(str)) return usernameUpperCase;
return validationSuccess;
};
@ -24,8 +29,10 @@ module.exports = {
isNumeric,
isHttpStatusCode,
isValidUsername,
isUsernameLowercase,
validationSuccess,
usernameTooShort,
usernameIsHttpStatusCode,
invalidCharError
invalidCharError,
usernameUpperCase
};