Skip to content

Commit

Permalink
Create validator.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 4, 2024
1 parent 256e068 commit 137cf14
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/utils/validator.js
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;

0 comments on commit 137cf14

Please sign in to comment.