TOML
The zio-config-toml module reads TOML with the tomlj parser and turns it into a ConfigProvider. It was added in ZIO Config 4.0.8.
To add the module:
// TOML source (JVM)
libraryDependencies += "dev.zio" %% "zio-config-toml" % "4.1.3"
Building a Providerโ
import zio.config.toml._ adds these constructors to ConfigProvider. The same functions, plus fromToml and fromTomlZIO, are on the TomlConfigProvider object:
| Constructor | Reads |
|---|---|
ConfigProvider.fromTomlString(tomlString) | A TOML string |
ConfigProvider.fromTomlFile(file) | A java.io.File |
ConfigProvider.fromTomlPath(path) | A java.nio.file.Path |
ConfigProvider.fromTomlReader(reader) | A java.io.Reader |
TomlConfigProvider.fromToml(result) | A TomlParseResult that you parsed with tomlj |
Each constructor parses its input when it is called. If the file is missing, it throws the java.io.IOException from tomlj. If the TOML has a syntax error, it throws a RuntimeException whose message starts with Unable to parse TOML: and lists tomlj's errors. Each one has a ...ZIO variant, such as ConfigProvider.fromTomlFileZIO or TomlConfigProvider.fromTomlZIO, that returns a Task[ConfigProvider] and fails the task instead. Every constructor also takes enableCommaSeparatedValueAsList (default false).
The examples on this page share these definitions and a helper, fromToml, which loads a description from a TOML string:
import zio._
import zio.config._
import zio.config.magnolia.deriveConfig
import zio.config.toml._
def fromToml[A](config: Config[A], toml: String, commaSeparated: Boolean = false): Either[Config.Error, A] =
Unsafe.unsafe { implicit unsafe =>
Runtime.default.unsafe.run(ConfigProvider.fromTomlString(toml, commaSeparated).load(config).either).getOrThrow()
}
final case class Server(host: String, ports: List[Int])
final case class AppConfig(title: String, server: Server)
Tables and Arraysโ
A TOML table becomes a nested key, and an array becomes a list. The keys under [server] are read with Config#nested("server"), which derivation adds for a field called server:
fromToml(deriveConfig[AppConfig], """
title = "orders"
[server]
host = "localhost"
ports = [8080, 8443]
""")
// res1: Either[Config.Error, AppConfig] = Right(
// value = AppConfig(
// title = "orders",
// server = Server(host = "localhost", ports = List(8080, 8443))
// )
// )
An inline table reads the same as a [server] table:
fromToml(deriveConfig[AppConfig], """
title = "orders"
server = { host = "localhost", ports = [8080] }
""")
// res2: Either[Config.Error, AppConfig] = Right(
// value = AppConfig(
// title = "orders",
// server = Server(host = "localhost", ports = List(8080))
// )
// )
An array of tables, written with [[name]], is a list of case classes:
final case class Backend(url: String, weight: Int)
fromToml(Config.listOf("backends", deriveConfig[Backend]), """
[[backends]]
url = "http://a.internal"
weight = 3
[[backends]]
url = "http://b.internal"
weight = 1
""")
// res3: Either[Config.Error, List[Backend]] = Right(
// value = List(
// Backend(url = "http://a.internal", weight = 3),
// Backend(url = "http://b.internal", weight = 1)
// )
// )
Dates and Timesโ
TOML has date and time values. The provider passes them on as their ISO-8601 text, so they load with Config.localDate, Config.localDateTime, Config.localTime and Config.offsetDateTime, or as plain strings:
final case class Release(day: java.time.LocalDate, at: java.time.OffsetDateTime)
fromToml(deriveConfig[Release], """
day = 2026-11-17
at = 2026-11-17T10:30:00Z
""")
// res4: Either[Config.Error, Release] = Right(
// value = Release(day = 2026-11-17, at = 2026-11-17T10:30Z)
// )
Comma-Separated Valuesโ
By default, a comma inside a string value is not split. Pass enableCommaSeparatedValueAsList = true to split such values when they are read as lists:
fromToml(Config.listOf("ports", Config.int), """ports = "8080,8443"""", commaSeparated = true)
// res5: Either[Config.Error, List[Int]] = Right(value = List(8080, 8443))
A TOML array is a list with or without the flag.
Using a tomlj Resultโ
If you parse the TOML yourself, for example to validate it first, pass the TomlParseResult to TomlConfigProvider.fromToml:
import org.tomlj.Toml
import zio.config.toml.TomlConfigProvider
val result = Toml.parse("host = \"localhost\"")
val provider = TomlConfigProvider.fromToml(result)
Next Stepsโ
- Config providers compares every source.
- Config descriptors describes the keys a provider reads.