refactor: create common validation function
This commit is contained in:
committed by
mrugesh
parent
55f95c2f74
commit
7500c370b6
13
utils/validate.js
Normal file
13
utils/validate.js
Normal 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;
|
||||
};
|
Reference in New Issue
Block a user