Skip to content

Commit

Permalink
Add toValidatedNec to Validated (#2487)
Browse files Browse the repository at this point in the history
* add toValidatedNec to Validated

* add test for toValidatedNec
  • Loading branch information
kun-song authored and Luka Jacobowitz committed Sep 24, 2018
1 parent 33f5c5b commit e5a6c91
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/src/main/scala/cats/data/Validated.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ sealed abstract class Validated[+E, +A] extends Product with Serializable {
case Invalid(e) => Validated.invalidNel(e)
}

/** Lift the Invalid value into a NonEmptyChain. */
def toValidatedNec[EE >: E, AA >: A]: ValidatedNec[EE, AA] =
this match {
case v @ Valid(_) => v
case Invalid(e) => Validated.invalidNec(e)
}

/**
* Convert to an Either, apply a function, convert back. This is handy
* when you want to use the Monadic properties of the Either type.
Expand Down
8 changes: 8 additions & 0 deletions tests/src/test/scala/cats/tests/ValidatedSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ class ValidatedSuite extends CatsSuite {
}
}

test("ValidatedNec") {
forAll { (e: String)
val manual = Validated.invalid[NonEmptyChain[String], Int](NonEmptyChain.one(e))
Validated.invalidNec[String, Int](e) should === (manual)
Validated.invalid[String, Int](e).toValidatedNec should === (manual)
}
}

test("isInvalid consistent with forall and exists") {
forAll { (v: Validated[String, Int], p: Int => Boolean) =>
if (v.isInvalid) {
Expand Down

0 comments on commit e5a6c91

Please sign in to comment.