The validation effect allows for the distinction between valid and invalid values in a program, accumulating the validation errors when executing it.

The validation effect, like state, supports parameterization to any type remaining type safe throughout the program declaration.

There needs to be implicit evidence of MonadState[M[_], List[E]] for any runtime M[_] where E is the type of the validation error due to the constraints placed by this effect.

The validation effect comes with three basic operations valid, invalid, and errors. Apart from these, it includes a couple of combinators for accumulating errors: fromEither and fromValidatedNel.

import freestyle._
import freestyle.implicits._
import freestyle.effects.validation
import cats.implicits._

sealed trait ValidationError
case class NotValid(explanation: String) extends ValidationError

val vl = validation[ValidationError]
import vl.implicits._

type ValidationResult[A] = State[List[ValidationError], A]


valid lifts a valid value to the program without accumulating any errors:

import cats.instances.list._
// import cats.instances.list._

def programValid[F[_]: vl.ValidationM] =
  for {
    a <- FreeS.pure(1)
    b <- vl.ValidationM[F].valid(1)
    c <- FreeS.pure(1)
  } yield a + b + c
// programValid: [F[_]](implicit evidence$1: vl.ValidationM[F])[[β$0$][F,β$0$],Int]

// res3: cats.Eval[(List[ValidationError], Int)] = cats.Eval$$anon$8@49b42d05


invalid accumulates a validation error:

def programInvalid[F[_]: vl.ValidationM] =
  for {
    a <- FreeS.pure(1)
    _ <- vl.ValidationM[F].invalid(NotValid("oh no"))
    b <- FreeS.pure(1)
  } yield a + b
// programInvalid: [F[_]](implicit evidence$1: vl.ValidationM[F])[[β$0$][F,β$0$],Int]

// res4: cats.Eval[(List[ValidationError], Int)] = cats.Eval$$anon$8@344b150


errors allows you to inspect the accumulated errors so far:

def programErrors[F[_]: vl.ValidationM] =
  for {
    _ <- vl.ValidationM[F].invalid(NotValid("oh no"))
    errs <- vl.ValidationM[F].errors
    _ <- vl.ValidationM[F].invalid(NotValid("this won't be in errs"))
  } yield errs
// programErrors: [F[_]](implicit evidence$1: vl.ValidationM[F])[[β$0$][F,β$0$],List[ValidationError]]

// res5: cats.Eval[(List[ValidationError], List[ValidationError])] = cats.Eval$$anon$8@49ee78dc


We can interleave Either[ValidationError, ?] values in the program. If they have errors on the left side they will be accumulated:

def programFromEither[F[_]: vl.ValidationM] =
  for {
    _ <- vl.ValidationM[F].fromEither(Left(NotValid("oh no")) : Either[ValidationError, Int])
	a <- vl.ValidationM[F].fromEither(Right(42) : Either[ValidationError, Int])
  } yield a
// programFromEither: [F[_]](implicit evidence$1: vl.ValidationM[F])[[β$0$][F,β$0$],scala.util.Either[ValidationError,Int]]

// res6: cats.Eval[(List[ValidationError], scala.util.Either[ValidationError,Int])] = cats.Eval$$anon$8@566bc5ce


We can interleave ValidatedNel[ValidationError, ?] values in the program. If they have errors in the invalid case they will be accumulated:

import{Validated, ValidatedNel, NonEmptyList}
// import{Validated, ValidatedNel, NonEmptyList}

def programFromValidatedNel[F[_]: vl.ValidationM] =
  for {
    a <- vl.ValidationM[F].fromValidatedNel(
	_ <- vl.ValidationM[F].fromValidatedNel(
	    Validated.invalidNel[ValidationError, Unit](NotValid("oh no"))
	_ <- vl.ValidationM[F].fromValidatedNel(
	    Validated.invalidNel[ValidationError, Unit](NotValid("another error!"))
  } yield a
// programFromValidatedNel: [F[_]](implicit evidence$1: vl.ValidationM[F])[[β$0$][F,β$0$],[[ValidationError],Int]]

// res7: cats.Eval[(List[ValidationError],[[ValidationError],Int])] = cats.Eval$$anon$8@17a191ab


By importing the validation effect implicits, a couple of methods are available for lifting valid and invalid values to our program: liftValid and liftInvalid.

def programSyntax[F[_]: vl.ValidationM] =
  for {
    a <- 42.liftValid
	_ <- NotValid("oh no").liftInvalid
	_ <- NotValid("another error!").liftInvalid
  } yield a
// programSyntax: [F[_]](implicit evidence$1: vl.ValidationM[F])[[β$0$][F,β$0$],Int]

// res8: cats.Eval[(List[ValidationError], Int)] = cats.Eval$$anon$8@50b4eeec