S7schema() provides a generic way of working with yaml configuration files.
The object is created by supplying both an initial YAML configuration (file)
and the JSON schema definition (schema) of the configuration file.
The initial configuration is validated before the new object is returned. If not valid the first error is thrown, together with a path to the entry in the YAML file, and a description of the error.
The S7schema class inherits from list, ensuring that the content of the YAML
file can be accessed as if read directly with yaml::read_yaml(), and supports
the below workflow:
Read and validate config file:
x <- S7schema(...)Edit content as if it was a list:
x$new_entry <- "new_value"Validate new content against the original schema:
validate(x)Use values in downstream functions:
x$new_entry
Arguments
- file
character(1)path to a yaml file to be checked. OrNULLif created using.data.- schema
character(1)path to a JSON schema.- .data
list()input to create the object. Mutually exclusive withfile.
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:
NA, NaN, and Inf values are validated as null. A field that may be
missing must allow null in the schema:
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.
Properties
- schema
character(1)path to JSON schema being used to validate against.- validator
Internal
validator()used to validate the content (read-only).- file
character(1)path to the source YAML file (orNULLif not set).
Examples
# Work with yaml configuration file:
S7schema(
file = system.file("examples/config.yml", package = "S7schema"),
schema = system.file("examples/schema.json", package = "S7schema")
)
#> <S7schema::S7schema> List of 1
#> $ my_config_var: int 1
#> @ schema : chr "/home/runner/work/_temp/Library/S7schema/examples/schema.json"
#> @ validator: <S7schema::validator>
#> .. @ context:List of 6
#> .. .. $ context :<pointer: 0x60136a498340>
#> .. .. $ validate:function (code_string)
#> .. .. $ source :function (file = NULL, code = NULL)
#> .. .. $ call :function (function_name, ...)
#> .. .. $ get :function (var_name)
#> .. .. $ assign :function (var_name, value)
#> .. .. - attr(*, "class")= chr "JSContext"
#> @ file : chr "/home/runner/work/_temp/Library/S7schema/examples/config.yml"
# Create object in memory
S7schema(
.data = list(my_config_var = 6),
schema = system.file("examples/schema.json", package = "S7schema")
)
#> <S7schema::S7schema> List of 1
#> $ my_config_var: num 6
#> @ schema : chr "/home/runner/work/_temp/Library/S7schema/examples/schema.json"
#> @ validator: <S7schema::validator>
#> .. @ context:List of 6
#> .. .. $ context :<pointer: 0x60136a498340>
#> .. .. $ validate:function (code_string)
#> .. .. $ source :function (file = NULL, code = NULL)
#> .. .. $ call :function (function_name, ...)
#> .. .. $ get :function (var_name)
#> .. .. $ assign :function (var_name, value)
#> .. .. - attr(*, "class")= chr "JSContext"
#> @ file : NULL
