forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvalidate.js
31 lines (29 loc) · 882 Bytes
/
validate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
};