Created
August 12, 2020 01:47
-
-
Save AhmedMourad0/4a094ffc5509762002dfd3ad50aa1b78 to your computer and use it in GitHub Desktop.
Code snippets for the `value-based classes and error-handling` Medium article.
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
data class Password private constructor(val value: String) { | |
companion object { | |
fun of(value: String): Password { | |
return when { | |
value.isTooShort() -> throw PasswordTooShortException() | |
value.containsNoNumbers() -> throw PasswordContainsNoNumbersException() | |
else -> Password(value) | |
} | |
} | |
} | |
} | |
class PasswordTooShortException() : Exception("Password is too short!") | |
class PasswordContainsNoNumbersException() : Exception("Password contains no numbers!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment