Skip to content

Instantly share code, notes, and snippets.

@talyssonoc
Created December 8, 2018 13:15
Show Gist options
  • Save talyssonoc/c30ab66e402cc8e8ae0afbacbd72ff49 to your computer and use it in GitHub Desktop.
Save talyssonoc/c30ab66e402cc8e8ae0afbacbd72ff49 to your computer and use it in GitHub Desktop.
class User {
constructor({ name, age }) {
this.name = name;
this.age = age;
}
validate() {
const hasName = Boolean(this.name);
const hasMinAge = this.age >= User.MIN_AGE;
const validation = {
isValid: hasName && hasMinAge
};
if(!hasName) {
validation.error = {
name: 'Name is empty'
};
}
if(!hasMinAge) {
validation.error = validation.error || {};
validation.error.age = 'Age is not the minimum';
}
return validation;
}
}
User.MIN_AGE = 18;
module.exports = User;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment