zio-prelude
yoshi-zio-prelude lets a validation target zio-prelude types: its non-empty collections become validation outputs, and a Newtype becomes a validation.
Setup
libraryDependencies += "dev.hshn" %% "yoshi-zio-prelude" % "0.6.0"
Having the module on the classpath is enough — import yoshi.* brings its instances in along with the core syntax. Nothing else to import.
import yoshi.*
import yoshi.defaults.*
Non-empty collections
A NonEmptyList[A] validates element by element, and each violation is reported under the index of the element it came from:
import zio.prelude.NonEmptyList
NonEmptyList("1", "x", "3").validateAs[NonEmptyList[Int]].left.map(_.toList)
// res0: Either[List[Tuple2[Paths, Err]], NonEmptyList[Int]] = Left(
// List((Paths(List(Index(1))), NonIntegerString("x")))
// )
A plain List[A] can be validated into a NonEmptyList[B] too. The output type says the domain needs at least one element, so an empty input fails with the Required violation — the same one an absent Option reports:
List("1", "2").validateAs[NonEmptyList[Int]]
// res1: Either[Violations[Err], NonEmptyList[Int]] = Right(
// Cons(head = 1, tail0 = Single(2))
// )
List.empty[String].validateAs[NonEmptyList[Int]].left.map(_.toList)
// res2: Either[List[Tuple2[Paths, Err]], NonEmptyList[Int]] = Left(
// List((Paths(List()), Required))
// )
Inside a cursor this reads like any other field:
case class FormInput(tags: List[String])
case class Post(tags: NonEmptyList[Int])
val post: Validation[Violation, FormInput, Post] =
Validation.cursor[FormInput] { c =>
(
c.validateAs[NonEmptyList[Int]](_.tags)
).validateN { tags =>
Post(tags)
}
}
post.run(FormInput(tags = List("1", "x"))).left.map(_.toList)
// res3: Either[List[Tuple2[Paths, Violation]], Post] = Left(
// List((Paths(List(Key("tags"), Index(1))), NonIntegerString("x")))
// )
post.run(FormInput(tags = Nil)).left.map(_.toList)
// res4: Either[List[Tuple2[Paths, Violation]], Post] = Left(
// List((Paths(List(Key("tags"))), Required))
// )
NonEmptySet works the same way: NonEmptySet[A] validates into NonEmptySet[B], and a Set[A] validates into NonEmptySet[B], failing with Required when empty.
As with the core derivations, the module knows nothing about your violation type. It reports an empty input through the Required instance — yoshi.defaults supplies one for Violation, and a violation type of your own needs a single given Required[MyViolation], as described in Getting Started.
Newtypes
Validation.newtype turns a Newtype into a validation from the underlying type to the newtype, using the newtype's own assertion. You decide what an assertion failure becomes: the function receives the rejected value and the assertion's message.
import zio.prelude.Assertion
import zio.prelude.Newtype
object PositiveInt extends Newtype[Int]:
override inline def assertion: Assertion[Int] = Assertion.greaterThan(0)
type PositiveInt = PositiveInt.Type
val positiveInt: Validation[Violation, Int, PositiveInt] =
Validation.newtype(PositiveInt)((value, _) => Violation.NonPositive(value))
positiveInt.run(42)
// res5: Either[Violations[Violation], PositiveInt] = Right(42)
positiveInt.run(0).left.map(_.toList)
// res6: Either[List[Tuple2[Paths, Violation]], PositiveInt] = Left(
// List((Paths(List()), NonPositive(0)))
// )
Being an ordinary Validation, it composes with everything else — parse first, then refine:
val parsePositiveInt: Validation[Violation, String, PositiveInt] =
Validations.parseInt >> positiveInt
parsePositiveInt.run("42")
// res7: Either[Violations[Violation], PositiveInt] = Right(42)
parsePositiveInt.run("abc").left.map(_.toList)
// res8: Either[List[Tuple2[Paths, Violation]], PositiveInt] = Left(
// List((Paths(List()), NonIntegerString("abc")))
// )
Accumulating with zio-prelude
The module also provides AssociativeBoth for Either[Violations[V], *], so zio-prelude's own combinators accumulate violations instead of stopping at the first failure:
import zio.prelude.AssociativeBothOps
(
Option.empty[String].validateAs[String].at("name") <*>
"abc".validateAs[Int].at("age")
).left.map(_.toList)
// res9: Either[List[Tuple2[Paths, Violation]], Tuple2[String, Int]] = Left(
// List(
// (Paths(List(Key("age"))), NonIntegerString("abc")),
// (Paths(List(Key("name"))), Required)
// )
// )