Refined module

kantan.csv comes with a refined module that can be used by adding the following dependency to your build.sbt:

libraryDependencies += "com.nrinaudo" %% "kantan.csv-refined" % "0.7.0"

You then need to import the corresponding package:

import kantan.csv.refined._

And that’s pretty much it. You can now encode and decode refined types directly.

Let’s first set our types up:

import eu.timepit.refined.api.Refined
import eu.timepit.refined.numeric.Positive
import kantan.csv._
import kantan.csv.ops._

type PositiveInt = Int Refined Positive

We can then simply write the following:

"1,2".readCsv[List, List[PositiveInt]](rfc)
// res0: List[ReadResult[List[PositiveInt]]] = List(Right(value = List(1, 2)))

"1,-2".readCsv[List, List[PositiveInt]](rfc)
// res1: List[ReadResult[List[PositiveInt]]] = List(
//   Left(
//     value = TypeError(message = "Not acceptable: 'Predicate failed: (-2 > 0).'")
//   )
// )

Other tutorials: