0

Is there any possibility to combine different errors from multiple variables using NonEmptyList

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

def throwErrorsWhenNumberIsLessThan6(x:Int):ValidationResult[Int] = if(x<6) s"$x !> 6".invalidNel else (x+6).validNel

def throwErrorsWhenStringLengthIsLessThan6(x:String):ValidationResult[String] = if(x.length<6) s"$x length > 6".invalidNel else (x+" OK!!! ").validNel

val ints = Range.apply(1,10).map(throwErrorsWhenNumberIsLessThan6).toList

val strings = Range.apply(1,10).map(e => throwErrorsWhenStringLengthIsLessThan6(e.toString)).toList

Is there a way to combine all the errors available in ints and strings?

Assuming that ints and strings will hold same data type in NonEmptyList.

Puneeth Reddy V
  • 1,538
  • 13
  • 28

2 Answers2

0

This worked for me

type ValidationResult[A] = ValidatedNel[String, A]
def throwErrorsWhenNumberIsLessThan6(x:Int):ValidationResult[Int] = if(x<6) s"$x !> 6".invalidNel else (x+6).validNel
def throwErrorsWhenStringLengthIsLessThan6(x:String):ValidationResult[String] = if(x.length<6) s"$x length > 6".invalidNel else (x+" OK!!! ").validNel

val ints = Range.apply(0,2).map(throwErrorsWhenNumberIsLessThan6).toList
val strings = Range.apply(0,2).map(e => throwErrorsWhenStringLengthIsLessThan6(e.toString+"KK")).toList

ints.filter(_.isInvalid).:::(strings.filter(_.isInvalid))
//List(Invalid(NonEmptyList(0KK length > 6)), Invalid(NonEmptyList(1KK length > 6)), Invalid(NonEmptyList(0 !> 6)), Invalid(NonEmptyList(1 !> 6)))
Puneeth Reddy V
  • 1,538
  • 13
  • 28
0

Yes, you can methods from the Applicative typeclass for that.

For example, now your ints has type List[ValidationResult[Int]]. You can use the sequence method to convert it to ValidationResult[List[Int]]:

import cats.implicits._

scala> val validatedInts = ints.sequence

validatedInts: ValidationResult[List[Int]] =
  Invalid(NonEmptyList(1 !> 6, 2 !> 6, 3 !> 6, 4 !> 6, 5 !> 6))

Or you can use traverse instead of map, when creating the ints object to get a ValidationResult immediately:

scala> val validatedInts2 = (1 until 10).toList.traverse(throwErrorsWhenNumberIsLessThan6)

validatedInts2: ValidationResult[List[Int]] = 
  Invalid(NonEmptyList(1 !> 6, 2 !> 6, 3 !> 6, 4 !> 6, 5 !> 6))

And do the same for the strings:

scala> val validatedStrings = (0 until 2).toList.traverse(e =>
  throwErrorsWhenStringLengthIsLessThan6(e.toString + "KK")).toList

validatedStrings: ValidationResult[List[String]] = 
  Invalid(NonEmptyList(0KK length > 6, 1KK length > 6))

And now you can combine validatedInts and validatedStrings into a single ValidationResult with mapN or tupled methods:

scala> (validatedInts, validatedStrings).tupled
res0: ValidationResult[(List[Int], List[String])] = 
  Invalid(NonEmptyList(1 !> 6, 2 !> 6, 3 !> 6, 4 !> 6, 5 !> 6, 0KK length > 6, 1KK length > 6))

scala> (validatedInts, validatedStrings).mapN(_.map(_.toString) ++ _)
res1: ValidationResult[List[String]] = 
  Invalid(NonEmptyList(1 !> 6, 2 !> 6, 3 !> 6, 4 !> 6, 5 !> 6, 0KK length > 6, 1KK length > 6))
Kolmar
  • 14,086
  • 1
  • 22
  • 25