2019-11-11 18:52:27 +00:00
|
|
|
const invalidCharError = {
|
|
|
|
valid: false,
|
2020-12-16 08:02:52 +00:00
|
|
|
error: 'contains invalid characters'
|
2019-11-11 18:52:27 +00:00
|
|
|
};
|
|
|
|
const validationSuccess = { valid: true, error: null };
|
2020-12-16 08:02:52 +00:00
|
|
|
const usernameTooShort = { valid: false, error: 'is too short' };
|
2019-11-27 05:19:17 +00:00
|
|
|
const usernameIsHttpStatusCode = {
|
|
|
|
valid: false,
|
2020-12-16 08:02:52 +00:00
|
|
|
error: 'is a reserved error code'
|
2019-11-27 05:19:17 +00:00
|
|
|
};
|
2021-05-20 07:04:00 +00:00
|
|
|
const usernameUpperCase = { valid: false, error: 'must be lowercase' };
|
2019-11-11 18:52:27 +00:00
|
|
|
|
2019-11-27 05:19:17 +00:00
|
|
|
const isNumeric = num => !isNaN(num);
|
|
|
|
const validCharsRE = /^[a-zA-Z0-9\-_+]*$/;
|
|
|
|
const isHttpStatusCode = str =>
|
2021-03-10 19:01:46 +00:00
|
|
|
isNumeric(str) && parseInt(str, 10) >= 100 && parseInt(str, 10) <= 599;
|
2021-05-20 07:04:00 +00:00
|
|
|
const isUsernameLowercase = str => {
|
|
|
|
return str === str.toLowerCase();
|
|
|
|
};
|
2019-11-27 05:19:17 +00:00
|
|
|
const isValidUsername = str => {
|
2019-11-11 18:52:27 +00:00
|
|
|
if (!validCharsRE.test(str)) return invalidCharError;
|
2019-11-27 05:19:17 +00:00
|
|
|
if (str.length < 3) return usernameTooShort;
|
|
|
|
if (isHttpStatusCode(str)) return usernameIsHttpStatusCode;
|
2021-05-20 07:04:00 +00:00
|
|
|
if (!isUsernameLowercase(str)) return usernameUpperCase;
|
2019-11-11 18:52:27 +00:00
|
|
|
return validationSuccess;
|
|
|
|
};
|
2019-11-27 05:19:17 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
isNumeric,
|
|
|
|
isHttpStatusCode,
|
|
|
|
isValidUsername,
|
2021-05-20 07:04:00 +00:00
|
|
|
isUsernameLowercase,
|
2019-11-27 05:19:17 +00:00
|
|
|
validationSuccess,
|
|
|
|
usernameTooShort,
|
|
|
|
usernameIsHttpStatusCode,
|
2021-05-20 07:04:00 +00:00
|
|
|
invalidCharError,
|
|
|
|
usernameUpperCase
|
2019-11-27 05:19:17 +00:00
|
|
|
};
|