feat: Allow display username with uppercase characters (#43667)
* feat: Allow display username with uppercase characters * fix: ensure user can change username to uppercased version * fix: ensure that same username in a different case does not require validation
This commit is contained in:
@ -8,20 +8,16 @@ 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;
|
||||
};
|
||||
|
||||
@ -29,10 +25,8 @@ module.exports = {
|
||||
isNumeric,
|
||||
isHttpStatusCode,
|
||||
isValidUsername,
|
||||
isUsernameLowercase,
|
||||
validationSuccess,
|
||||
usernameTooShort,
|
||||
usernameIsHttpStatusCode,
|
||||
invalidCharError,
|
||||
usernameUpperCase
|
||||
invalidCharError
|
||||
};
|
||||
|
@ -3,8 +3,7 @@ const {
|
||||
usernameTooShort,
|
||||
validationSuccess,
|
||||
usernameIsHttpStatusCode,
|
||||
invalidCharError,
|
||||
usernameUpperCase
|
||||
invalidCharError
|
||||
} = require('./validate');
|
||||
|
||||
function inRange(num, range) {
|
||||
@ -37,9 +36,6 @@ describe('isValidUsername', () => {
|
||||
expect(isValidUsername('a-b')).toStrictEqual(validationSuccess);
|
||||
expect(isValidUsername('a_b')).toStrictEqual(validationSuccess);
|
||||
});
|
||||
it('rejects uppercase characters', () => {
|
||||
expect(isValidUsername('Quincy')).toStrictEqual(usernameUpperCase);
|
||||
});
|
||||
|
||||
it('rejects all other ASCII characters', () => {
|
||||
const allowedCharactersList = ['-', '_', '+'];
|
||||
@ -54,7 +50,7 @@ describe('isValidUsername', () => {
|
||||
let expected = invalidCharError;
|
||||
if (allowedCharactersList.includes(char)) expected = validationSuccess;
|
||||
if (inRange(code, numbers)) expected = validationSuccess;
|
||||
if (inRange(code, upperCase)) expected = usernameUpperCase;
|
||||
if (inRange(code, upperCase)) expected = validationSuccess;
|
||||
if (inRange(code, lowerCase)) expected = validationSuccess;
|
||||
expect(isValidUsername(base + char)).toStrictEqual(expected);
|
||||
}
|
||||
|
Reference in New Issue
Block a user