Skip to contents

j_schema_is_vaild() uses JSON Schema https://json-schema.org/ to validate JSON 'data' according to 'schema'.

j_schema_validate() returns a JSON or R object, data.frame, or tibble, describing how data does not conform to schema. See the "Using 'jsoncons' in R" vignette for help interpreting validation results.

Usage

j_schema_is_valid(
  data,
  schema,
  ...,
  data_type = j_data_type(data),
  schema_type = j_data_type(schema)
)

j_schema_validate(
  data,
  schema,
  as = "string",
  ...,
  data_type = j_data_type(data),
  schema_type = j_data_type(schema)
)

Arguments

data

JSON character vector, file, or URL defining document to be validated. NDJSON data and schema are not supported.

schema

JSON character vector, file, or URL defining the schema against which data will be validated.

...

passed to jsonlite::toJSON when data is not character-valued.

data_type

character(1) type of data; one of "json" or a value returned by j_data_type(); schema validation does not support "ndjson" data.

schema_type

character(1) type of schema; see data_type.

as

for j_schema_validate(), one of "string", "R", "data.frame", "tibble", or "details", to determine the representation of the return value.

Examples

## Allowable `data_type=` and `schema_type` -- excludes 'ndjson'
j_data_type() |>
    Filter(\(type) !"ndjson" %in% type, x = _) |>
    str()
#> List of 4
#>  $ : chr "json"
#>  $ : chr [1:2] "json" "file"
#>  $ : chr [1:2] "json" "url"
#>  $ : chr "R"
## compare JSON patch to specification. 'op' key should have value
## 'add'; 'paths' key should be key 'path'
## schema <- "https://json.schemastore.org/json-patch.json"
schema <- system.file(package = "rjsoncons", "extdata", "json-patch.json")
op <- '[{
    "op": "adds", "paths": "/biscuits/1",
    "value": { "name": "Ginger Nut" }
}]'
j_schema_is_valid(op, schema)
#> [1] FALSE

j_schema_validate(op, schema, as = "details")
#> # A tibble: 10 × 5
#>    valid evaluationPath                    schemaLocation instanceLocation error
#>    <lgl> <chr>                             <chr>          <chr>            <chr>
#>  1 FALSE /items/oneOf/0/required           https://json.… /0               Requ…
#>  2 FALSE /items/oneOf/0/properties/op/enum https://json.… /0/op            'add…
#>  3 FALSE /items/oneOf/0/additionalPropert… https://json.… /0/paths         Addi…
#>  4 FALSE /items/oneOf/1/required           https://json.… /0               Requ…
#>  5 FALSE /items/oneOf/1/properties/op/enum https://json.… /0/op            'add…
#>  6 FALSE /items/oneOf/1/additionalPropert… https://json.… /0/paths         Addi…
#>  7 FALSE /items/oneOf/2/required           https://json.… /0               Requ…
#>  8 FALSE /items/oneOf/2/required           https://json.… /0               Requ…
#>  9 FALSE /items/oneOf/2/properties/op/enum https://json.… /0/op            'add…
#> 10 FALSE /items/oneOf/2/additionalPropert… https://json.… /0/paths         Addi…