Forked from louissalin/gist:6486d2b9fe950a068e20
Last active
August 29, 2015 14:01
-
-
Save danclien/6325152fdba48966120f to your computer and use it in GitHub Desktop.
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._ | |
class InvoiceValidations { | |
def checkName(name: String): ValidationNel[String, String] = { | |
if (name.length > 100) | |
"name too long".failNel | |
else | |
name.successNel | |
} | |
def withValidations(id: Int, recipient: String): ValidationNel[String, Invoice] = { | |
// This one needed the [String] type parameter because it's the first one in the list | |
(id.successNel[String] |@| checkName(recipient)) { Invoice(_, _) } | |
// (checkName(recipient) |@| id.successNel) { (name, id) => Invoice(id, name) } | |
} | |
} | |
case class Invoice(id: Int, recipient: String) | |
object Invoice extends InvoiceValidations | |
val invoice = Invoice.withValidations(1, "Louis Salin") | |
println(invoice) | |
// returns Success(Invoice(1,Louis Salin)) | |
// if the name length is greater than 100, it returns Failure(NonEmptyList(name too long)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment