diff options
Diffstat (limited to 'graphs/js/accountConstraints')
| -rw-r--r-- | graphs/js/accountConstraints/account.js | 42 | ||||
| -rw-r--r-- | graphs/js/accountConstraints/accountError.js | 23 |
2 files changed, 65 insertions, 0 deletions
diff --git a/graphs/js/accountConstraints/account.js b/graphs/js/accountConstraints/account.js new file mode 100644 index 0000000..4a3ddbe --- /dev/null +++ b/graphs/js/accountConstraints/account.js @@ -0,0 +1,42 @@ +"use strict"; + +const { + InvalidUsernameError, + InvalidPasswordError, + InvalidYearOfBirthError, +} = require("./accountError"); + +class Account { + // FIXME DONE: A constructor was deleted here + constructor(username, password, yearOfBirth) { + const regUsername = /^[a-zA-Z0-9\-_]+$/; + const lower = /[a-z]/; + const upper = /[A-Z]/; + const digits = /[0-9]/; + + if (!regUsername.test(username)) { + throw new InvalidUsernameError("Invalid username."); + } + + this.username = username; + if ( + !password.match(lower) || + !password.match(upper) || + !password.match(digits) || + password.length < 8 || + password.length > 100 + ) { + throw new InvalidPasswordError("Invalid password."); + } + + this.password = password; + if (yearOfBirth <= 1800 || yearOfBirth > new Date().getFullYear()) { + throw new InvalidYearOfBirthError("Invalid birth year"); + } + + this.yearOfBirth = yearOfBirth; + } +} +module.exports = { + Account, +}; diff --git a/graphs/js/accountConstraints/accountError.js b/graphs/js/accountConstraints/accountError.js new file mode 100644 index 0000000..9b427e0 --- /dev/null +++ b/graphs/js/accountConstraints/accountError.js @@ -0,0 +1,23 @@ +class InvalidUsernameError extends Error { + name = "InvalidUsernameError"; + constructor(message) { + super("Invalid username: " + message); + } +} +class InvalidPasswordError extends Error { + name = "InvalidPasswordError"; + constructor(message) { + super("Invalid password: " + message); + } +} +class InvalidYearOfBirthError extends Error { + name = "InvalidYearOfBirthError"; + constructor(message) { + super("Invalid birth year: " + message); + } +} +module.exports = { + InvalidPasswordError, + InvalidUsernameError, + InvalidYearOfBirthError, +}; |
