I have this piece of code on the Xor
object of cats
Xor.right(data).ensure(List(s"$name cannot be blank"))(_.nonEmpty)
Now since Xor has been removed, I am trying to write something similar using Either object
Either.ensuring(name.nonEmpty, List(s"$name cannot be blank"))
but this does not work because the return type of ensuring is Either.type
I can ofcourse write an if. but I want to do the validation with the cats constructs.
Xor
was removed from cats becauseEither
is now right-biased in Scala 2.12. You can use the standard libraryEither#filterOrElse
, which does the same thing, but is not curried:Using cats, you can use
ensure
onEither
, if the order of the parameters and lack of currying isn't to your liking: