Skip to content

Instantly share code, notes, and snippets.

Created October 17, 2016 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0766e85b7f1d7baa456793d9267087ab to your computer and use it in GitHub Desktop.
Save anonymous/0766e85b7f1d7baa456793d9267087ab to your computer and use it in GitHub Desktop.
the description for this gist
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