简体   繁体   中英

Scalaz validation: convert sequence of validations to a single validation

I am using scalaz validation, and have some code to validate products.

def validateProduct(product: Option[Product]): ValidationNel[String, Product] = ???

Given a list of products, I want to get a single validation containing the whole list as a successful value or a list of validation errors. It seems like some kind of fold should do it, but I am not sure what the combination function should be.

 def validateProducts(products: Seq[Option[Product]]): ValidationNel[String, Seq[Product]] = {
    val listOfValidations: Seq[ValidationNel[String, Product]] = products.map(validateProduct _)
    val validatedList:ValidationNel[Seq[String], Seq[Product]] = ??? // what to do here?
    ???
  }

Any help is appreciated

If instead of a ValidationNel[List[String], List[Product]] you want a ValidationNel[String, List[Product]] (ie, all the failures in the same list), you can just use traverse :

val result: ValidationNel[String, List[Product]] =
  products.toList.traverseU(validateProduct)

Note that I've converted the Seq to a List as there are no type class instances for raw Seq , and I'm using traverseU rather than traverse as Scala's type inference doesn't quite work for non-trivial type constructors like ValidationNel

You can use fold with applicative

  import scalaz.syntax.validation._
  import scalaz.syntax.applicative._

  case class Product(name: String)

  val allGood = Seq(
    Product("a").successNel[String],
    Product("b").successNel[String]
  )

  val aggregated: ValidationNel[String, Seq[Product]] = 
    allGood.foldLeft(Seq.empty[Product].successNel[String]) {
    case (acc , v) => (acc |@| v)(_ :+ _)
  }

  println(aggregated)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM