Skip to content

Instantly share code, notes, and snippets.

@AhmedMourad0
Last active August 12, 2020 17:48
Show Gist options
  • Save AhmedMourad0/fabbdc3c47ae8567a02b8ee34610bfe8 to your computer and use it in GitHub Desktop.
Save AhmedMourad0/fabbdc3c47ae8567a02b8ee34610bfe8 to your computer and use it in GitHub Desktop.
Code snippets for the `value-based classes and error-handling` Medium article.
data class Password private constructor(val value: String) {
companion object {
fun of(value: String): Either<Violation, Password> {
return when {
value.isTooShort() -> Violation.PasswordTooShort(MIN_LENGTH).left()
value.containsNoNumbers() -> Violation.PasswordContainsNoNumbers.left()
else -> Password(value).right()
}
}
}
sealed class Violation {
data class PasswordTooShort(val minLength: Int) : Violation()
object PasswordContainsNoNumbers : Violation()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment