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.5.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:

scala> "1,2".readCsv[List, List[PositiveInt]](rfc)
res0: List[kantan.csv.ReadResult[List[PositiveInt]]] = List(Right(List(1, 2)))

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

Other tutorials: