Skip to contents

Checks a theory against the package's schema, theory.schema.json, without a JSON Schema engine, so that it runs wherever R does, webR included.

Usage

tf_validate(theory, full = FALSE)

Arguments

theory

A theory object (named list), such as one from tf_read().

full

When TRUE, also check ids, cross-references and the rest of the schema.

Value

TRUE (invisibly) on success. Otherwise, it stops with "invalid theory object: " followed by every problem found, joined by "; ". The messages count entries from 0, as the Python package does, so prediction[0] is theory$predictions[[1]].

Details

The default (full = FALSE) checks structure. It covers the required top-level fields, the maturity and theory_form enums, the top-level field names and the required fields and enums of each construct, proposition and prediction. It also checks that every collection is a list. A field that is absent, NULL or blank is reported as missing, and one that holds another type as such (id must be a string).

With full = TRUE, it also checks that every id is unique within its collection and that every cross-reference points to a declared id. The cross-references are the endpoints of a proposition, the derivations and diagnostics of a prediction and the target of an assumption, an evidence entry or a test outcome. It then checks the rest of the schema. That covers the required fields of assumptions, alternatives, evidence and test outcomes, a logical passed, the evidence direction and formal-model type enums, the version block and the schema_version pattern. It also covers numbers within [0, 1] where the schema asks for them, a prediction's bounds and test block and a test outcome's estimate and se. It compares each prediction's type and its claim's sign with its bounds, and reports a directional prediction whose claim, with no given, has the sign nonzero, which states no direction. Last, it checks the type of every other field and of every entry of a string array.

Besides the ids and references, which the schema cannot express, the full check is stricter than a JSON Schema 2020-12 validator in three ways. A blank required string counts as missing. An entry of a string array must be a nonempty string. A prediction's bounds must hold a finite bound with lower not above upper, a test outcome's estimate and se and a test block's sesoi must be finite, that block's n_planned must lie within the range of a double and its power_lo must not be above power_hi. The full check is more lenient in three ways. A NULL optional field other than theory_form is absent, as is a single NA, which tf_write() writes as null. A single string stands for a one-element array of strings. An empty list() stands for an empty mapping as well as an empty sequence.

Both checks are deterministic, and the Python package's Theory.validate() reports the same problems in the same order with the same text.

Examples

theory <- tf_read(tf_example_path("panic-network.theory.yaml"))
isTRUE(tf_validate(theory))              # required fields and enums
#> [1] TRUE
isTRUE(tf_validate(theory, full = TRUE)) # also ids, cross-references and the rest of the schema
#> [1] TRUE

# The failure path is the more informative one. Point a prediction at a
# proposition that was never declared.
broken <- theory
broken$predictions[[1]]$derives_from <- "p_missing"
tryCatch(tf_validate(broken, full = TRUE), error = conditionMessage)
#> [1] "invalid theory object: prediction[0] derives_from 'p_missing' is not a known proposition"