Refined Module

The ciris-refined module enables decoding of refined refinement types. This is especially useful for encoding validation in the types of your configuration values. Let’s see how we can decode refinement types. We’ll start by defining a configuration source from which to read some configuration values.

import ciris.{ConfigKeyType, ConfigSource}
import ciris.refined._

val source = {
  val keyType = ConfigKeyType[String]("refined key")
  ConfigSource.fromEntries(keyType)(
    "negative" -> "-1",
    "zero" -> "0",
    "positive" -> "1",
    "other" -> "abc"
  )
}

In this example, we’ll simply try to read PosInt values, which are all Ints strictly greater than zero.

import eu.timepit.refined.types.numeric.PosInt
// import eu.timepit.refined.types.numeric.PosInt

source.read("negative").decodeValue[PosInt]
// res0: ciris.ConfigEntry[ciris.api.Id,String,String,eu.timepit.refined.types.numeric.PosInt] = ConfigEntry(negative, ConfigKeyType(refined key))

source.read("zero").decodeValue[PosInt]
// res1: ciris.ConfigEntry[ciris.api.Id,String,String,eu.timepit.refined.types.numeric.PosInt] = ConfigEntry(zero, ConfigKeyType(refined key))

source.read("positive").decodeValue[PosInt]
// res2: ciris.ConfigEntry[ciris.api.Id,String,String,eu.timepit.refined.types.numeric.PosInt] = ConfigEntry(positive, ConfigKeyType(refined key))

source.read("other").decodeValue[PosInt]
// res3: ciris.ConfigEntry[ciris.api.Id,String,String,eu.timepit.refined.types.numeric.PosInt] = ConfigEntry(other, ConfigKeyType(refined key))