def checkInvoiceNumber : ValidationNel[String, String] = invoice.invoiceNumber match {
case Some(v) => v.successNel
case None => "Missing invoice-number".failureNel
}
def checkSentDate : ValidationNel[String, LocalDate] = invoice.sentDate match {
case Some(v) => v.successNel
case None => "Missing invoice-data".failureNel
}
etc..
(checkInvoiceNumber
⊛ checkSentDate
⊛ checkCompany
⊛ checkSourceCompany
⊛ checkInvoiceSource
⊛ checkBankAccount
⊛ checkCustAddressStreet
⊛ checkCustAddressPlace
⊛ checkCustAddressCity
⊛ checkCustAddressZip
⊛ checkSenderAddressStreet
⊛ checkSenderAddressPlace
).tupled match {
case Failure(msgList) => prinln(msgList)
case Success((invoiceNumber, invoiceDate, company, sourceComp, invoiceSource, fromBankAccount
, custAddrStreet, custAddrPlace, custAddrCity, custAddrZip
, senderAddrStreet, senderAddrPlace
)) =>
This approach stops working when having more then 12 checks (because ApplicativeBuilder12 is the "last one").
How can I accomplish the same using a List of any length, preserving the types in Success so I can use them like the match-statement above?
Thanks.
--
Andreas
import org.joda.time.LocalDate
import scalaz.Scalaz._
import scalaz._
case class Comp(name: String)
case class Inv(invoiceNumber: Option[String], sentDate: Option[LocalDate], company: Option[Comp])
class ValidationTest {
def checkInvoiceNumber(invoice: Inv) : ValidationNel[String, String] = invoice.invoiceNumber match {
case Some(v) => v.successNel
case None => "Missing invoice-number".failureNel
}
def checkSentDate(invoice: Inv) : ValidationNel[String, LocalDate] = invoice.sentDate match {
case Some(v) => v.successNel
case None => "Missing invoice-date".failureNel
}
def checkCompany(invoice: Inv): ValidationNel[String, Comp] = invoice.company match {
case Some(v) => v.successNel
case None => "Missing company".failureNel
}
def validate(): Unit = {
val invoice = Inv("aaaaaa".some, LocalDate.now().some, Option.empty[Comp])
(checkInvoiceNumber(invoice)
⊛ checkSentDate(invoice)
⊛ checkCompany(invoice)
).tupled match {
case Failure(msgList) =>
case Success((invoiceNumber, sentDate, company)) =>
}
}
}
import org.joda.time.LocalDate
import org.testng.annotations.Test
import scalaz.Scalaz._
import scalaz._
case class Comp(name: String)
case class Inv(invoiceNumber: Option[String], sentDate: Option[LocalDate], company: Option[Comp])
@Test
class ValidationTest {
def checkInvoiceNumber(invoice: Inv) : ValidationNel[String, String] = invoice.invoiceNumber match {
case Some(v) => v.successNel
case None => "Missing invoice-number".failureNel
}
def checkSentDate(invoice: Inv) : ValidationNel[String, LocalDate] = invoice.sentDate match {
case Some(v) => v.successNel
case None => "Missing invoice-date".failureNel
}
def checkCompany(invoice: Inv): ValidationNel[String, Comp] = invoice.company match {
case Some(v) => v.successNel
case None => "Missing company".failureNel
}
val badInvoice = Inv("aaaaaa".some, LocalDate.now().some, Option.empty[Comp])
val goodInvoice = Inv("aaaaaa".some, LocalDate.now().some, Comp("Visena").some)
def crankMan(company: Comp, sentDate: LocalDate, invoiceNumber: String): Inv = {
Inv(invoiceNumber.some, sentDate.some, company.some)
}
def testInv(invoice: Inv): Unit = {
(checkInvoiceNumber(invoice)
<*> (checkSentDate(invoice)
<*> (checkCompany(invoice) map (crankMan _).curried
))) match {
case Failure(msgList) => println(msgList.toList.mkString("\n"))
case Success(inv) => print(s"Invoice: $inv")
}
}
def testFisk(): Unit = {
testInv(badInvoice)
testInv(goodInvoice)
}
}
--
You received this message because you are subscribed to the Google Groups "scalaz" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaz+un...@googlegroups.com.
To post to this group, send email to sca...@googlegroups.com.
Visit this group at https://groups.google.com/group/scalaz.
For more options, visit https://groups.google.com/d/optout.
Then I use the ⊛ operator to accumulate errors:
(checkInvoiceNumber ⊛ checkSentDate ⊛ checkCompany ⊛ checkSourceCompany ⊛ checkInvoiceSource ⊛ checkBankAccount ⊛ checkCustAddressStreet ⊛ checkCustAddressPlace ⊛ checkCustAddressCity ⊛ checkCustAddressZip ⊛ checkSenderAddressStreet ⊛ checkSenderAddressPlace ).tupled match { case Failure(msgList) => prinln(msgList) case Success((invoiceNumber, invoiceDate, company, sourceComp, invoiceSource, fromBankAccount , custAddrStreet, custAddrPlace, custAddrCity, custAddrZip , senderAddrStreet, senderAddrPlace )) =>
This approach stops working when having more then 12 checks (because ApplicativeBuilder12 is the "last one").How can I accomplish the same using a List of any length, preserving the types in Success so I can use them like the match-statement above?