Skip to contents

See also official documentation at https://json-schema.org/

Usage

validate(
  schema,
  instance,
  version = c("draft202012", "draft201909", "draft07", "draft06", "draft04"),
  style = c("minimal", "basic"),
  err_on_invalid = FALSE
)

Arguments

schema

the schema to validate against, either a json string, an R list of lists, or a file

instance

the instance to validate, either a json string, an R list of lists, or a file

version

the version of the JSON schema to use, one of '2020-12', '2019-09', 'draft-07', 'draft-06', 'draft-05', see also https://json-schema.org/specification

style

the style of the output, one of 'minimal' or 'basic'. Minimal returns only TRUE/FALSE, whereas basic returns a list with more details.

err_on_invalid

when set to TRUE, the function calls stop with an error message. The error message is the output of the 'basic' style error for a single error, or 'multiple errors' otherwise. Default FALSE.

Value

TRUE/FALSE depending on whether the instance is valid against the schema

Examples

validate(schema = '{"type": "string"}', instance = '"hello"')
#> [1] TRUE
validate(schema = '{"type": "string"}', instance = '123')
#> [1] FALSE
validate(schema = '{"type": "string"}', instance = c('"hello"', '123'))
#> [1]  TRUE FALSE

# or provide R objects directly, mixing json and R lists is possible
validate(schema = list(type = "string"), instance = list("hello"))
#> [1] FALSE
validate(schema = list(type = "string"), instance = list(hello = "world"))
#> [1] FALSE
validate(schema = '{"type": "string"}', instance = list(hello = "world"))
#> [1] FALSE

# FYI, the correct schema for the above instance is:
validate(schema = '{"type": "object", "properties": {"hello": {"type": "string"}}}',
         instance = list(hello = "world"))
#> [1] TRUE

# err_on_invalid throws error for invalid instances
tryCatch(validate('{"type": "string"}', instance = '123', err_on_invalid = TRUE),
         error = function(e) errorCondition(e))
#> <error: Error in validate("{\"type\": \"string\"}", instance = "123", err_on_invalid = TRUE): '123 is not of type "string"' for field '/type'
#> >

# wrong JSON format throws an error
tryCatch(validate(schema = 'wrong JSON format"', ""),
         error = function(e) errorCondition(e))
#> <error: Error in validate(schema = "wrong JSON format\"", ""): Schema must be valid JSON
#> >

# a little more involved example
schema <- '{
  "type": "object",
  "properties": {
  "name": { "type": "string" },
  "age": { "type": "integer" }
  },
  "required": ["name"]
}'
validate(schema, "{\"name\": \"John\"}") # age is optional
#> [1] TRUE
validate(schema, "{\"name\": \"John\", \"age\": 30}")
#> [1] TRUE
validate(schema, "{\"age\": 30}") # name is required
#> [1] FALSE

validate(schema, "{\"age\": 30}", style = "basic")
#> [[1]]
#> [[1]]$errors
#> [[1]]$errors[[1]]
#> [[1]]$errors[[1]]$error
#> [1] "\"name\" is a required property"
#> 
#> [[1]]$errors[[1]]$instanceLocation
#> [1] ""
#> 
#> [[1]]$errors[[1]]$keywordLocation
#> [1] "/required"
#> 
#> 
#> 
#> [[1]]$valid
#> [1] FALSE
#> 
#> 

# use different versions
# if credit_card is present, billing_address must be present as a string
# the dependentSchemas keyword is only available after draft-07
dep_schema <- '
{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "credit_card": { "type": "string" }
   },
  "dependentSchemas": {
    "credit_card": {
      "properties": {
        "billing_address": { "type": "string" }
      },
    "required": ["billing_address"]
    }
  }
}'
validate(dep_schema, '{"name": "Alice", "credit_card": "123"}') # billing address is missing
#> [1] FALSE
validate(dep_schema, '{"name": "Alice", "credit_card": "123"}', version = "draft04")
#> [1] FALSE

validate(
  schema = '{"$schema": "http://json-schema.org/draft-07/schema#", "type": "string"}',
  instance = '"hello"'
)
#> [1] TRUE

# schema and instance in files
schema_file <- tempfile(fileext = ".json")
writeLines('{"type": "string"}', schema_file)

instance_file <- tempfile(fileext = ".json")
writeLines('"hello"', instance_file)

validate(schema_file, instance_file)
#> [1] TRUE

file.remove(c(schema_file, instance_file))
#> [1] TRUE TRUE