Skip to contents

The read direction of S7_to_JSONSchema: reads each property's PropertySpec back out of the schema and assembles a live S7 class whose properties carry the same types, bounds, enums, containers, and validators as the class the schema was generated from.

Usage

JSONSchema_to_S7(
  schema,
  defaults = NULL,
  refs = NULL,
  name = NULL,
  package = NULL
)

Arguments

schema

Named list: A JSON Schema, as produced by S7_to_JSONSchema or parsed from the published tree with jsonlite::fromJSON(simplifyVector = FALSE).

defaults

Optional named list: Property name to default value, as published in the defaults artifact under this schema's $id.

refs

Optional named list: Property name to S7 class, for properties published as a $ref to another schema.

name

Optional Character: Class name. Defaults to the schema title.

package

Optional Character: Package name recorded on the class.

Value

S7 class.

Details

Defaults are supplied separately because they are not a schema keyword: the published tree carries no default, and defaults live in their own versioned artifact keyed by schema $id. A property that is neither nullable nor const therefore needs an entry in defaults, and the error names any that are missing rather than inventing a value.

Properties holding a nested config are published as a $ref to that config's own schema, which this function does not fetch; supply the corresponding S7 classes via refs.

Author

EDG

Examples

# A schema as `S7_to_JSONSchema()` emits it: standard keywords carry the
# bounds, `x-rtemis` carries the leaf type.
schema <- list(
  title = "Demo",
  properties = list(
    k = list(
      type = "integer",
      minimum = 1L,
      maximum = 10L,
      description = "Number of components.",
      `x-rtemis` = list(type = "integer")
    )
  )
)
Demo <- JSONSchema_to_S7(schema, defaults = list(k = 2L))
Demo()@k
#> [1] 2
Demo(k = 5L)@k
#> [1] 5
# The reconstructed class enforces the schema's bounds.
tryCatch(Demo(k = 99L), error = conditionMessage)
#> [1] "<Demo> object properties are invalid:\n- @k must be <= 10."