-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// utils/validator.js | ||
|
||
// Input validation utility | ||
class Validator { | ||
static isEmail(email) { | ||
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; | ||
return regex.test(email); | ||
} | ||
|
||
static isNotEmpty(value) { | ||
return value && value.trim() !== ''; | ||
} | ||
|
||
static isNumber(value) { | ||
return !isNaN(value) && !isNaN(parseFloat(value)); | ||
} | ||
|
||
static validateUser Input(input) { | ||
const errors = []; | ||
if (!this.isNotEmpty(input.username)) { | ||
errors.push('Username is required.'); | ||
} | ||
if (!this.isEmail(input.email)) { | ||
errors.push('Invalid email format.'); | ||
} | ||
if (!this.isNumber(input.age)) { | ||
errors.push('Age must be a number.'); | ||
} | ||
return errors.length > 0 ? errors : null; | ||
} | ||
} | ||
|
||
module.exports = Validator; |