Search code examples
scalavalidationdesign-patternsfunctional-programmingfor-comprehension

Data validation with EitherT-s inside for-comp


Consider a simplified version of the problem that I'm trying to solve. Here's my current implementation.

case class Foo()

def addFoo(json: String): EitherT[Future, Exception, Long] = {
  def parse(json: String): EitherT[Future, Exception, Foo] = ???

  def validate(foo: Foo): EitherT[Future, Exception, Foo] = ???

  def save(foo: Foo): EitherT[Future, Exception, Long] = ???

  for {
    foo <- parse(json)
    _ <- validate(foo)
    id <- save(foo)
  } yield id
}

This doesn't feel right because, although validate returns an EitherT of Exception or Foo, it just performs the validation and returns either an exception or an unchanged foo value. So, we are never interested in the right part of the Either and never use it. Is there any approach that corresponds better to the "validation" semantics?


Solution

  • If you don't care about right value you can just use Unit instead of Foo, like:

    def validate(foo: Foo): EitherT[Future, Exception, Unit] = ???