Skip to content

Instantly share code, notes, and snippets.

@louissalin
Created May 8, 2014 20:21
Show Gist options
  • Save louissalin/6486d2b9fe950a068e20 to your computer and use it in GitHub Desktop.
Save louissalin/6486d2b9fe950a068e20 to your computer and use it in GitHub Desktop.
A test of adding validations to a domain model
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