Skip to content

Instantly share code, notes, and snippets.

@danclien
Forked from louissalin/gist:6486d2b9fe950a068e20
Last active August 29, 2015 14:01
Show Gist options
  • Save danclien/6325152fdba48966120f to your computer and use it in GitHub Desktop.
Save danclien/6325152fdba48966120f to your computer and use it in GitHub Desktop.
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