Created
December 8, 2018 13:15
-
-
Save talyssonoc/c30ab66e402cc8e8ae0afbacbd72ff49 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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