Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toValidatedNec to Validated #2487

Merged
merged 2 commits into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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