Scala-Cats验证:值mapN不是ValidatedNel元组的成员

时间:2018-04-16 20:44:11

标签: scala functional-programming monads scala-cats scala-2.12

Scala社区。

目前我正在尝试使用猫Validated Monad实现自定义模型/单参数验证。但是,自从1.0删除笛卡尔积后,我无法使用(v1 | @ | v2)map(f)并且无法编译我的代码:

import cats.Semigroupal
import cats.data.Validated.{Invalid, Valid}
import cats.data.{ValidatedNel, _}
import cats.implicits._
import cats.instances.all._

case class FieldErrorInfo(name: String, error: String)

type FieldName = String

type ValidationResult[A] = ValidatedNel[FieldErrorInfo, A]

trait SingleFieldValidationRule[U] extends ((U, FieldName) => ValidationResult[U])

trait ModelValidationRule[M] extends (M => ValidationResult[M])

object ValidateNameRule extends SingleFieldValidationRule[String] {
  override def apply(v1: String, name: String): ValidationResult[String]  = {
    if (v1.contains("cats"))
      v1.validNel
    else
      FieldErrorInfo(name, "Some Error").invalidNel
  }
}

object ValidateQuantityRule extends SingleFieldValidationRule[Int] {
  override def apply(v1: Int, name: String): ValidationResult[Int] =
    if (v1 > 0)
      v1.validNel
    else FieldErrorInfo(name, "Some Error").invalidNel
}

case class SampleModel(name: String, quantity: Int)

object ValidateSampleModel extends ModelValidationRule[SampleModel] {
  override def apply(v1: SampleModel): ValidationResult[SampleModel] = {
    val stage1: ValidatedNel[FieldErrorInfo, String] = ValidateNameRule(v1.name, "name")
    val stage2: ValidatedNel[FieldErrorInfo, Int] = ValidateQuantityRule(v1.quantity, "quantity")

    implicit val sga: Semigroupal[NonEmptyList] = new Semigroupal[NonEmptyList] {
      override def product[A, B](fa: NonEmptyList[A], fb: NonEmptyList[B]): NonEmptyList[(A, B)] = fa.flatMap(a => fb.map(b => a -> b))
    }

    (stage1, stage2).mapN(SampleModel)
  }
}

编译器说,

Error:(43, 23) value mapN is not a member of (cats.data.ValidatedNel[FieldErrorInfo,String], cats.data.ValidatedNel[FieldErrorInfo,Int])
    (stage1, stage2).mapN(SampleModel)
                     ^

请指点我如何使用新的Applicative语法或我做错了什么...(忘了创建/导入一些含义)

2 个答案:

答案 0 :(得分:7)

您似乎缺少以下导入:

import cats.syntax.apply._

代表mapN

请确保您已激活-Ypartial-unification编译器标志,否则编译器将难以从ValidatedNel[FieldErrorInfo, ?]stage1的类型中提取stage2

libraryDependencies += "org.typelevel" %% "cats-core" % "1.1.0"
scalaVersion := "2.12.5"
scalacOptions += "-Ypartial-unification"

通过以上设置,以下工作:

import cats.Semigroupal
import cats.data.Validated.{Invalid, Valid}
import cats.data.ValidatedNel
import cats.data.NonEmptyList
import cats.syntax.apply._     // for `mapN`
import cats.syntax.validated._ // for `validNel`

case class FieldErrorInfo(name: String, error: String)

type FieldName = String

type ValidationResult[A] = ValidatedNel[FieldErrorInfo, A]

trait SingleFieldValidationRule[U] extends ((U, FieldName) => ValidationResult[U])

trait ModelValidationRule[M] extends (M => ValidationResult[M])

object ValidateNameRule extends SingleFieldValidationRule[String] {
  override def apply(v1: String, name: String): ValidationResult[String]  = {
    if (v1.contains("cats"))
      v1.validNel
    else
      FieldErrorInfo(name, "Some Error").invalidNel
  }
}

object ValidateQuantityRule extends SingleFieldValidationRule[Int] {
  override def apply(v1: Int, name: String): ValidationResult[Int] =
    if (v1 > 0)
      v1.validNel
    else FieldErrorInfo(name, "Some Error").invalidNel
}

case class SampleModel(name: String, quantity: Int)

object ValidateSampleModel extends ModelValidationRule[SampleModel] {
  override def apply(v1: SampleModel): ValidationResult[SampleModel] = {
    val stage1: ValidatedNel[FieldErrorInfo, String] = ValidateNameRule(v1.name, "name")
    val stage2: ValidatedNel[FieldErrorInfo, Int] = ValidateQuantityRule(v1.quantity, "quantity")

    implicit val sga: Semigroupal[NonEmptyList] = new Semigroupal[NonEmptyList] {
      override def product[A, B](fa: NonEmptyList[A], fb: NonEmptyList[B]): NonEmptyList[(A, B)] = fa.flatMap(a => fb.map(b => a -> b))
    }


    (stage1, stage2).mapN(SampleModel)
  }
}

答案 1 :(得分:6)

stage1stage2必须包含ValidationResult[_]类型。

在这种情况下,隐含mapN应该有效。

 object ValidateSampleModel extends ModelValidationRule[SampleModel] {
  override def apply(v1: SampleModel): ValidationResult[SampleModel] = {
    val stage1: ValidationResult[String] = ValidateNameRule(v1.name, "name")
    val stage2: ValidationResult[Int] = ValidateQuantityRule(v1.quantity, "quantity")

    (stage1, stage2).mapN(SampleModel)
  }
}