Skip to contents

Validates a TOML file or character vector using the external Taplo CLI, optionally against a JSON Schema (Draft 4).

Usage

toml_validate(input, from_file = TRUE, schema = NULL, top_errors_only = TRUE)

Arguments

input

If from_file = TRUE, the path to a TOML file as a character scalar. Otherwise, TOML content as a character vector.

from_file

Whether input is the path to a TOML file or already a character vector of TOML content.

schema

URL to a JSON Schema (Draft 4) file to validate input against. Can also be a local filesystem path specified in the file URI scheme (absolute path prefixed with file://). If NULL, no schema-based validation is performed and input is only checked to be TOML-compliant.

top_errors_only

Whether to reduce the output to the top error message of each of TOML CLI's error classes. If FALSE, TOML CLI's complete error output is shown.

Value

If the validation is successful, input invisibly. Otherwise an error is thrown.

Details

The highest supported JSON Schema specification is Draft 4. This is a limitation of the underlying tool Taplo.

Taplo allows to define the schema to be used directly in the TOML file using the schema header directive. Note that the schema argument has precendence unless set to NULL (the default).

Why JSON Schema-based validation

Although there are two noteworthy attempts at introducing a native validation format for TOML, TOLS and TOML Schema, neither of them has been officially adopted yet. As it appears, it could take several more years to decades until such thing happens, if ever.

In the meantime, we can use the JSON Schema format as an alternative thanks to an awesome implementation by the Taplo CLI, which itself is written in the Rust programming language and available as a single-binary program for all common platforms.

See also

Other TOML functions: toml_read()

Examples

try(
  pal::toml_validate(input = "key = tru",
                     from_file = FALSE)
)
#> Error in pal::toml_validate(input = "key = tru", from_file = FALSE) : 
#>   TOML validation failed with:
#>   
#> error: invalid TOML
#>   ┌─ -:1:7
#>   
#> 1  key = tru
#>          ^^^ expected value
#> 
#>