合并错误的猫已验证

时间:2019-02-11 21:38:49

标签: scala scala-cats

我提高了Cats Validated的能力,但是在连接错误累积器时遇到问题。

我收到此错误

Error:(88, 17) discarded non-Unit value
  errorList ++= errors

我不知道如何解决此错误。

我的代码:

def checkValidity(types: List[Type]): ValidationResult[Boolean] = {
   val errorList: mutable.MutableList[String] = mutable.MutableList.empty
   schemas.foreach { schema =>
    for (errors <- schema.checkValidity(types).invalid[List[String]]) {
      errorList ++= errors
    }
   }
}

感谢您的帮助。


完整代码

我在两个不同的文件中有两个case类:

case class Domain(
  name: String,
  directory: String,
  metadata: Option[Metadata] = None,
  schemas: List[Schema] = Nil,
  comment: Option[String] = None,
  extensions: Option[List[String]] = None,
  ack: Option[String] = None
) {

case class Schema(
  name: String,
  pattern: Pattern,
  attributes: List[Attribute],
  metadata: Option[Metadata],
  merge: Option[MergeOptions],
  comment: Option[String],
  presql: Option[List[String]],
  postsql: Option[List[String]]
) 

我想检查所有错误并在累加器列表中添加错误。最后,如果此列表为空,则返回Valid(true),否则返回Invalid(myAccumulatorList)

Domain.scala文件中功能的完整代码:

def checkValidity(types: List[Type]): ValidationResult[Boolean] = {
  val errorList: mutable.MutableList[String] = mutable.MutableList.empty

  // Check Domain name validity
  val dbNamePattern = Pattern.compile("[a-zA-Z][a-zA-Z0-9_]{1,100}")
  if (!dbNamePattern.matcher(name).matches())
    errorList += s"Schema with name $name should respect the pattern ${dbNamePattern.pattern()}"

// Check Schema validity
  schemas.foreach { schema =>
    for (errors <- schema.checkValidity(types).invalid[List[String]]) {
      errorList ++= errors
  }
}

  val duplicatesErrorMessage = "%s is defined %d times. A schema can only be defined once."
  for (errors <- duplicates(schemas.map(_.name), duplicatesErrorMessage).invalid[List[String]]) {
    errorList ++= errors
}

  // TODO Check partition columns

  // TODO Validate directory
  val inputDir = File(this.directory)
  if (!inputDir.exists) {
    errorList += s"$directory not found"
  }
  if (errorList.nonEmpty)
    Invalid(errorList.toList)
  else
    Valid(true)

}

0 个答案:

没有答案