freeCodeCamp/utils/validate.js
Valeria 753ea937ea
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
2021-11-04 11:18:40 +01:00

33 lines
878 B
JavaScript

const invalidCharError = {
valid: false,
error: 'contains invalid characters'
};
const validationSuccess = { valid: true, error: null };
const usernameTooShort = { valid: false, error: 'is too short' };
const usernameIsHttpStatusCode = {
valid: false,
error: 'is a reserved error code'
};
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 isValidUsername = str => {
if (!validCharsRE.test(str)) return invalidCharError;
if (str.length < 3) return usernameTooShort;
if (isHttpStatusCode(str)) return usernameIsHttpStatusCode;
return validationSuccess;
};
module.exports = {
isNumeric,
isHttpStatusCode,
isValidUsername,
validationSuccess,
usernameTooShort,
usernameIsHttpStatusCode,
invalidCharError
};