refactor: create common validation function

This commit is contained in:
Oliver Eyton-Williams
2019-11-11 19:52:27 +01:00
committed by mrugesh
parent 55f95c2f74
commit 7500c370b6
2 changed files with 67 additions and 0 deletions

13
utils/validate.js Normal file
View File

@ -0,0 +1,13 @@
const validCharsRE = /^[a-zA-Z0-9\-_+]+$/;
const invalidCharError = {
valid: false,
error: 'contains invalid characters'
};
const validationSuccess = { valid: true, error: null };
const usernameTooShort = { valid: false, error: 'is too short' };
exports.validate = str => {
if (str.length < 3) return usernameTooShort;
if (!validCharsRE.test(str)) return invalidCharError;
return validationSuccess;
};