Validation in Scala with Cats effect and ZIO
Briefly

Validation in Scala with Cats effect and ZIO
"One thing I quickly came to appreciate about ZIO (and effect system in general) is that function signatures do not lie. Along with the happy path of what it returns it tells you what error to expect. In case of ZIO it also tells you the environment (resources) the function depends on. For example, in Cats Effect you will see IO[Error, A] while in ZIO the type is ZIO[Resource, Error, A]"
"The main idea is that instead of short circuiting on the first error, ValidatedNel accumulates all error in an NonEmptyList. If you prefer using a case class rather than tuple you can use mapN instead. Running this we can see that we get all the error messages. def validateInput (maybeName: Option[String], phone: String): cats.data.ValidatedNel[String, ResultTuple]val result: cats.data.ValidatedNel[String, ResultTuple] = Invalid(NonEmptyList(Name cannot be empty, Phone number must be 8 digits))Invalid input: Name cannot be empty, Phone number must be 8 digits"
ValidatedNel accumulates multiple validation errors in a NonEmptyList instead of short-circuiting on the first failure. mapN constructs case classes from multiple Validated values rather than tuples. ValidatedNel is a type alias: Validated[NonEmptyList[E], A]. EitherNel retains an Either-like semantics and does not accumulate errors, serving compatibility. Examples show validation functions returning Invalid(NonEmptyList(...)) and concatenated error messages. ZIO encodes environment, error, and result in ZIO[R, E, A], making function signatures reveal required resources and possible failures. Effect monads suspend side effects until execution time, preserving referential transparency. Caliban was used for GraphQL migration instead of Grackle or Sangria.
Read at Medium
Unable to calculate read time
[
|
]