0

I go up to competence on Cats Validated and I have a problem to concatenate my error accumulator.

I get this error

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

I don't know how can I resolve this error.

My code :

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
    }
   }
}

Thanks for your help.


Full code

I have two case class in two different file:

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]]
) 

I want to check all errors and add errors in my accumulator list. At the final, if this list is empty I return Valid(true) else I return Invalid(myAccumulatorList)

The complete code of the function in Domain.scala file:

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)

}
Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
Robert25
  • 103
  • 6
  • For information errors is a List[String] type – Robert25 Feb 11 '19 at 21:41
  • 2
    Whatever `schemas` is, your method seems to end in a `foreach`, which should return a unit `()`, not a `ValidationResult[Boolean]`. Why are you trying to do anything with the errors at all, why not just let `Validated` do all the error handling automatically? – Andrey Tyukin Feb 11 '19 at 23:52
  • Can you see me an exemple in my context because I don’t understand how can I add all the errors in my accumulator list flreach schema knowing that schema.checkValidity(type) return a Validated[Boolean] – Robert25 Feb 12 '19 at 04:56
  • what are all those booleans supposed to mean? In other words, supposing that you can *ignore the possibility of occurrence of any errors*, what should `checkValidity` compute? The `Validated` already takes care of the errors, that's not the problem. The problem with your question is that it's unclear what the "happy path" is supposed to be. – Andrey Tyukin Feb 12 '19 at 05:03
  • I add the full code below – Robert25 Feb 12 '19 at 05:14
  • Your posting in the answer section has been deleted by a moderator as "Not An Answer". I copy-pasted the content of that answer into your question. I still think that the question could be greatly improved by reducing the example to a [mcve]. – Andrey Tyukin Feb 12 '19 at 08:28

0 Answers0