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
import scalaz._ | |
import scalaz.Scalaz._ | |
import scala.concurrent.Future | |
import scala.concurrent._ | |
import scala.concurrent.duration._ | |
import scala.concurrent.ExecutionContext.Implicits.global | |
type FutureList[T] = ListT[Future, T] |
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
// interp.configureCompiler(_.settings.YpartialUnification.value = true) | |
case class UserDTO(email: String, password: String) | |
def isValidEmail(email: String): Boolean = ??? | |
def isValidPassword(password: String): Boolean = ??? | |
class UserValidationException extends Exception("User validation exception") | |
def validateUserVersion0(user: UserDTO): UserDTO = |
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 Email(value: String) | |
object Email { | |
def apply(email: String): Option[Email] = Some(email).filter(isValidEmail).map(new Email(_)) | |
} | |
case class Password(value: String) | |
object Password { | |
def apply(password: String): Option[Password] = Some(password).filter(isValidPassword).map(new Password(_)) |
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
val userError = "User validation error" | |
def validateUserVersion2(user: UserDTO): Either[String, User] = | |
User.fromUserDTO(user).toRight(userError) |
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
val emailError = "invalid email" | |
val passwordError = "invalid password" | |
def validateUserVersion3(user: UserDTO): Either[String, User] = ( | |
Email(user.email).toRight(emailError), | |
Password(user.password).toRight(passwordError) | |
) match { | |
case (Right(email), Right(password)) => Right(User(email, password)) | |
case (Left(error), Right(_)) => Left(error) | |
case (Right(_), Left(error)) => Left(error) | |
case (Left(e1), Left(e2)) => Left(e1 ++ e2) |
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
def validateUserVersion4(user: UserDTO): Either[String, User] = for { | |
email <- Email(user.email).toRight(emailError) | |
password <- Password(user.password).toRight(passwordError) | |
} yield User(email, password) |
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
def validateUserVersion4(user: UserDTO): Either[String, User] = for { | |
email <- Email(user.email).toRight(emailError) | |
password <- Password(user.password).toRight(passwordError) | |
} yield User(email, password) |
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
//import $ivy.`org.typelevel::cats-core:1.6.0` | |
import cats.data.Validated | |
import cats.data.Validated.{Invalid, Valid} | |
import cats.implicits._ | |
def validateUserVersion5(user: UserDTO): Validated[String, User] = ( | |
Email(user.email).toValid(emailError), | |
Password(user.password).toValid(passwordError) | |
).mapN(User(_, _)) |
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
sealed trait UserError | |
final case object PasswordValidationError extends UserError | |
sealed trait EmailError extends UserError | |
final case object InvalidEmailError extends EmailError | |
final case object BlackListedUserError extends EmailError |
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
import cats.implicits._ | |
import cats.data.NonEmptyList | |
import cats.data.ValidatedNel | |
import cats.data.Validated | |
import cats.data.Validated.{Invalid, Valid} | |
val blackListedUsers = Seq("bart@simsom.com") | |
def validatedEvilness(email: Email): ValidatedNel[UserError, Email] = | |
Validated.condNel(!blackListedUsers.contains(email.value), |
OlderNewer