Created
October 17, 2016 11:48
-
-
Save anonymous/0766e85b7f1d7baa456793d9267087ab to your computer and use it in GitHub Desktop.
the description for this gist
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
case class User(password: String, email: String) | |
sealed trait UserCreationError | |
case object PasswordTooShort extends UserCreationError | |
case object EmailWrongPattern extends UserCreationError | |
val emailPattern = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}$".r | |
def checkPasswordLength(password: String): ValidatedNel[UserCreationError, String] = | |
if (password.length < 6) | |
invalidNel(PasswordTooShort) | |
else | |
valid(password) | |
def checkEmailPattern(email: String): ValidatedNel[UserCreationError, String] = (emailPattern findFirstIn email) match { | |
case Some(_) => valid(email) | |
case None => invalidNel(EmailWrongPattern) | |
} | |
def createUser(password: String, email: String): ValidatedNel[UserCreationError, User] = | |
(checkPasswordLength(password) |@| checkEmailPattern(email)) map User.apply(_, _) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment