cats
yoshi-cats lets a validation target cats data types, and lets its result flow into the cats combinators you already use.
Setup
libraryDependencies += "dev.hshn" %% "yoshi-cats" % "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 cats.data.NonEmptyList
NonEmptyList.of("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")))
// )
Any Iterable 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(
// NonEmptyList(head = 1, tail = List(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))
// )
NonEmptyChain and NonEmptySet work the same way — each validates element by element, and any Iterable validates into one, failing with Required when empty. The index in a violation path follows the input's iteration order, so validate from an ordered collection where the path has to be stable.
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.
Validated
runValidated runs a validation into Validated, so its result composes with the accumulating cats combinators:
import cats.syntax.all.*
case class Member(name: String, age: Int)
(
Validations.minLength(3).runValidated("ab").leftMap(_.asChild("name")),
Validations.parseInt.runValidated("abc").leftMap(_.asChild("age")),
).mapN(Member.apply).leftMap(_.toList)
// res5: Validated[List[Tuple2[Paths, Violation]], Member] = Invalid(
// List(
// (Paths(List(Key("age"))), NonIntegerString("abc")),
// (Paths(List(Key("name"))), TooShortString(value = "ab", minLength = 3))
// )
// )
The other direction is Validation.fromValidated: a function that reports failures as Validated becomes an ordinary Validation, composable with everything else.
import cats.data.Validated
val parseInt: Validation[Violation, String, Int] =
Validation.fromValidated { value =>
value.toIntOption match
case Some(int) => Validated.valid(int)
case None => Validated.invalid(Violations.of(Violation.NonIntegerString(value)))
}
val requiredInt: Validation[Violation, Option[String], Int] =
Validations.required[String] >> parseInt
requiredInt.run(Some("42"))
// res6: Either[Violations[Violation], Int] = Right(42)
requiredInt.run(None).left.map(_.toList)
// res7: Either[List[Tuple2[Paths, Violation]], Int] = Left(
// List((Paths(List()), Required))
// )
Violations as a Monoid
Violations[V] has a Monoid that merges violation trees. This is what Validated needs from its error type to accumulate through mapN above, and it is what lets Either[Violations[V], *] — the type every run returns — accumulate through parMapN:
(
Option.empty[String].validateAs[String].at("name"),
"abc".validateAs[Int].at("age"),
).parMapN((_, _)).left.map(_.toList)
// res8: Either[List[Tuple2[Paths, Violation]], Tuple2[String, Int]] = Left(
// List(
// (Paths(List(Key("age"))), NonIntegerString("abc")),
// (Paths(List(Key("name"))), Required)
// )
// )