Created
May 8, 2014 20:21
-
-
Save louissalin/6486d2b9fe950a068e20 to your computer and use it in GitHub Desktop.
A test of adding validations to a domain model
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[String] | |
else | |
name.successNel[String] | |
} | |
def withValidations(id: Int, recipient: String): ValidationNel[String, Invoice] = { | |
(id.successNel[String] |@| checkName(recipient)) { Invoice(_, _) } | |
} | |
} | |
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