Using Typesafe config we read in and parse configuration files.
Using Typesafe config we read in and parse configuration files. Paths into these files are then retrieved and type checked using Ficus.
Using a lightweight DSL, we are able to then check and validate these type checked values. For example, given that the Typesafe configuration:
top-level-name = "test" test { nestedVal = 50.68 nestedDuration = 4 h nestedList = [] context { valueInt = 30 valueStr = "test string" valueDuration = 12 ms valueStrList = [ "addr1:10", "addr2:20", "addr3:30" ] valueDoubleList = [ 10.2, 20, 0.123 ] } }
has been parsed and read into an implicit of type Config
, then we are
able to validate that the value at the path test.nestedVal
has type
Double
and that it satisfies specified size bounds as follows:
case object ShouldBeAPercentageValue extends Exception validate[Double]("test.nestedVal", ShouldBeAPercentageValue)(n => 0 <= n && n <= 100)
If the configuration value at path test.nestedVal
fails to pass the
percentage bounds check, then Left(ShouldBeAPercentageValue)
is
returned.
Likewise, we can enforce that all values in the array at the path
test.context.valueStrList
match the regular expression pattern
[a-z0-9]+:[0-9]+
as follows:
case object ShouldBeASocketValue extends Exception validate[List[String]]("test.context.valueStrList", ShouldBeASocketValue)(_.matches("[a-z0-9]+:[0-9]+"))
In some instances, we may not care about checking the value at a
configuration path. In these cases we can use unchecked
:
unchecked[FiniteDuration]("test.nestedDuration")