package config
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")
- Alphabetic
- By Inheritance
- config
- FicusInstances
- URLReader
- URIReaders
- LocalDateReader
- PeriodReader
- ISOZonedDateTimeReader
- BigNumberReaders
- ConfigValueReader
- TryReader
- DurationReaders
- ConfigReader
- CollectionReaders
- OptionReader
- SymbolReader
- StringReader
- AnyValReaders
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
-
sealed
trait
ConfigError extends AnyRef
General reasons for why a config value might fail to be validated by
validate
. - final case class FileNotFound(file: String, reason: Throwable) extends ConfigError with Product with Serializable
-
sealed
trait
PathSpec extends AnyRef
Ensures that the specified path has a value defined for it.
Ensures that the specified path has a value defined for it. This may be achieved:
- at the Typesafe configuration file level: the path must exist and have a non-null value
- using a sentinal value: the path has the sentinal value if and only if it has not been set or assigned to.
-
type
ValidationFailure[Value] = Validated[NonEmptyList[ValueFailure], Value]
Applicative functor for value validation
-
sealed
trait
ValueError extends AnyRef
Reasons why we might fail to parse a value from the config file
- final case class ValueErrors(errors: ValueError*) extends ConfigError with Product with Serializable
- final case class ValueFailure(path: String, reason: Throwable) extends ValueError with Product with Serializable
- final case class optional(value: String) extends PathSpec with Product with Serializable
- final case class required extends PathSpec with Product with Serializable
Value Members
-
implicit
val
bigDecimalReader: ValueReader[BigDecimal]
- Definition Classes
- BigNumberReaders
-
implicit
val
bigIntReader: ValueReader[BigInt]
- Definition Classes
- BigNumberReaders
-
implicit
val
booleanValueReader: ValueReader[Boolean]
- Definition Classes
- AnyValReaders
-
implicit
val
configValueReader: ValueReader[Config]
- Definition Classes
- ConfigReader
-
implicit
val
configValueValueReader: ValueReader[ConfigValue]
- Definition Classes
- ConfigValueReader
-
implicit
val
doubleValueReader: ValueReader[Double]
- Definition Classes
- AnyValReaders
-
implicit
def
durationReader: ValueReader[Duration]
- Definition Classes
- DurationReaders
-
implicit
val
ficusConfigValueReader: ValueReader[FicusConfig]
- Definition Classes
- ConfigReader
-
implicit
def
finiteDurationReader: ValueReader[FiniteDuration]
- Definition Classes
- DurationReaders
-
implicit
val
intValueReader: ValueReader[Int]
- Definition Classes
- AnyValReaders
-
implicit
val
isoZonedDateTimeReader: ValueReader[ZonedDateTime]
- Definition Classes
- ISOZonedDateTimeReader
-
implicit
val
javaURIReader: ValueReader[URI]
- Definition Classes
- URIReaders
-
implicit
val
javaURLReader: ValueReader[URL]
- Definition Classes
- URLReader
-
implicit
val
localDateReader: ValueReader[LocalDate]
- Definition Classes
- LocalDateReader
-
implicit
val
longValueReader: ValueReader[Long]
- Definition Classes
- AnyValReaders
-
implicit
def
mapValueReader[A](implicit entryReader: ValueReader[A]): ValueReader[Map[String, A]]
- Definition Classes
- CollectionReaders
-
implicit
def
optionValueReader[A](implicit valueReader: ValueReader[A]): ValueReader[Option[A]]
- Definition Classes
- OptionReader
-
implicit
val
periodReader: ValueReader[Period]
- Definition Classes
- PeriodReader
-
implicit
val
stringValueReader: ValueReader[String]
- Definition Classes
- StringReader
-
implicit
val
symbolValueReader: ValueReader[Symbol]
- Definition Classes
- SymbolReader
- implicit def toFicusConfig(config: Config): FicusConfig
- implicit def toRefinementType[Base, Refinement](implicit reader: ValueReader[Base], witness: Validate[Base, Refinement]): ValueReader[Refined[Base, Refinement]]
-
implicit
def
traversableReader[C[_], A](implicit entryReader: ValueReader[A], cbf: CanBuildFrom[Nothing, A, C[A]]): ValueReader[C[A]]
- Definition Classes
- CollectionReaders
-
implicit
def
tryValueReader[A](implicit valueReader: ValueReader[A]): ValueReader[Try[A]]
- Definition Classes
- TryReader
- def unchecked[Value](path: String)(implicit config: Config, reader: ValueReader[Value]): Validated[NonEmptyList[ValueFailure], Value]
-
def
unchecked[Value](pathSpec: PathSpec)(implicit config: Config, reader: ValueReader[Value]): Validated[NonEmptyList[ValueFailure], Value]
Used to read in a value of type
Value
.Used to read in a value of type
Value
. Other than checking the values type, no other validation is performed.- Value
type we expect the parsed and checked config value to have
- pathSpec
Typesafe config path to the value we are validating
- config
the currently in scope config object that we use
- reader
Ficus
ValueReader
that we use for type checking the parsed config value- returns
either a
ValueFailure
or the parsed and *unchecked*Value
instance
- def validate[Value](path: String, failureReason: Throwable)(check: (Value) ⇒ Boolean)(implicit config: Config, reader: ValueReader[Value]): Validated[NonEmptyList[ValueFailure], Value]
-
def
validate[Value](pathSpec: PathSpec, failureReason: Throwable)(check: (Value) ⇒ Boolean)(implicit config: Config, reader: ValueReader[Value]): Validated[NonEmptyList[ValueFailure], Value]
Used to read in a value of type
Value
and to then check that value usingcheck
.Used to read in a value of type
Value
and to then check that value usingcheck
. Ifcheck
returns false, then we fail withfailureReason
.- Value
type we expect the parsed and checked config value to have
- pathSpec
Typesafe config path to the value we are validating
- failureReason
if
check
fails, theThrowable
instance we return- check
predicate used to check the configuration value
- config
the currently in scope config object that we use
- reader
Ficus
ValueReader
that we use for type checking the parsed config value- returns
either a
ValueFailure
or the parsed and checkedValue
instance
-
def
validateConfig[ValidConfig](configFile: String)(check: (Config) ⇒ Validated[NonEmptyList[ValueError], ValidConfig]): Validated[ConfigError, ValidConfig]
Loads Typesafe config file and then builds the validated case class
ValidConfig
Loads Typesafe config file and then builds the validated case class
ValidConfig
- ValidConfig
the case class type that we are to construct
- configFile
the root Typesafe config file name
- check
the builder and validator that we will use to construct the
ValidConfig
instance- returns
either a
ConfigError
throwable instance or the validated case classValidConfig
-
def
via[ValidConfig](path: String)(inner: (Config) ⇒ Validated[NonEmptyList[ValueFailure], ValidConfig])(implicit config: Config): Validated[NonEmptyList[ValueFailure], ValidConfig]
The currently in-scope implicit
Config
instance is restricted to a specified pathThe currently in-scope implicit
Config
instance is restricted to a specified path- ValidConfig
the case class type that we are to construct
- path
we restrict our Typesafe config path to this path
- inner
configuration builder that we will apply to the restricted
Config
object- config
the current in-scope
Config
object that we need to path restrict- returns
either a
ValueError
or the validated case classValidConfig
- object MissingValue extends Exception with Product with Serializable
- object NullValue extends Exception with Product with Serializable
- object RequiredValueNotSet extends Exception with Product with Serializable
- object required extends Serializable