Validation in Scala with Cats effect and ZIO
Briefly

Validation in Scala with Cats effect and ZIO
"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"
"If we look closer at the signature of ValidatedNel we will actually see it contains a List for error messages allowing us to collect them all. type ValidatedNel[+E, +A] = Validated[NonEmptyList[E], A] There is also an EitherNel type with a similar signature, but it does not accumulate errors. It behave like a regular Either. And its mostly there for compatibility. See also https://typelevel.org/cats/datatypes/validated.html We started using ZIO in my company when we migrated from REST to GraphQL and landed on Caliban (instead of Grackle or Sangria)."
ValidatedNel accumulates validation errors in a NonEmptyList, allowing multiple errors to be collected rather than short-circuiting on the first failure. mapN can be used to map validated values into a case class instead of tuples. Example output demonstrates Invalid(NonEmptyList(Name cannot be empty, Phone number must be 8 digits)) showing both error messages. The type alias is type ValidatedNel[+E, +A] = Validated[NonEmptyList[E], A], making error collection explicit in the type. EitherNel has a similar signature but behaves like a regular Either and does not accumulate errors. ZIO was adopted with Caliban for GraphQL; ZIO types express environment, error, and success and ensure effects are suspended and referentially transparent.
Read at Medium
Unable to calculate read time
[
|
]