Skip to contents

Check if a configuration is in accordance with a JSON schema definition.

It is possible to either validate an existing list object in memory or an existing yaml configuration file.

Usage

validate_list(x, schema)

validate_yaml(file, schema)

Arguments

x

list object to validate

schema

character(1) path to a JSON schema.

file

character(1) path to a yaml file to be checked. Or NULL if created using .data.

Value

  • validate_list(): invisible(x)

  • validate_yaml(): invisible(file)

Details

The content is validated as R reads it: files are read with yaml::read_yaml(), and S7schema objects are converted with to_yaml() and read back before validation. list objects in validate_list() are validated as they are. See internal validator() documentation for more info on how the validation is done.

In YAML files and S7schema objects, R reads a YAML sequence with one item ([a]) as a single value ("a"). For fields that can hold one or more values, allow both forms in the schema with oneOf:

{
  "oneOf": [
    {"type": "string"},
    {"type": "array", "items": {"type": "string"}}
  ]
}

NA, NaN, and Inf values are validated as null. A field that may be missing must allow null in the schema:

{"type": ["string", "null"]}

Numbers with up to 16 significant digits are validated exactly. Numbers that need 17, e.g. 0.1 + 0.2, are validated exactly in files and lists, but rounded in S7schema objects, as to_yaml() writes them.

See also

Examples

# Validate list object in memory
validate_list(
  x = list(my_config_var = 1),
  schema = system.file("examples/schema.json", package = "S7schema")
) |>
  print()
#> $my_config_var
#> [1] 1
#> 

# Validate yaml file on disk
validate_yaml(
  file = system.file("examples/config.yml", package = "S7schema"),
  schema = system.file("examples/schema.json", package = "S7schema")
) |>
  print()
#> [1] "/home/runner/work/_temp/Library/S7schema/examples/config.yml"