json_schema_validator: simple way to validate a json instance under a given schema

Description Usage Arguments Details References Examples

View source: R/utils.R

Description

simple way to validate a json instance under a given schema

Usage

1
json_schema_validator(json_data = NULL, json_schema = NULL)

Arguments

json_data

a named list specifying the input data to validate against the json-schema

json_schema

a named list specifying the json-schema

Details

Define a json-schema that the input data should follow and then validate the input data against the schema. If the input data follows the schema then by running the function nothing will be returned, otherwise an error with Traceback will be printed in the R-session.

In case that type is at the same time also a property name in the json data, then do not include "type" = "string" in the json schema ( https://github.com/epoberezkin/ajv/issues/137 )

References

https://pypi.org/project/jsonschema/, https://python-jsonschema.readthedocs.io/en/latest/

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
try({
  if (reticulate::py_available(initialize = FALSE)) {
    if (reticulate::py_module_available("jsonschema")) {

      library(GeoMongo)

      schema_dict = list("type" = "object",

                         "properties" = list(

                           "name" = list("type" = "string"),

                           "location" = list("type" = "object",

                                             "properties" = list(

                                               "type" = list("enum" = c("Point", "Polygon")),

                                               "coordinates" = list("type" = "array")
                                             ))))


      data_dict = list("name" = "example location",

                       "location" = list("type" = "Point", "coordinates" = c(-120.24, 39.21)))


      json_schema_validator(json_data = data_dict, json_schema = schema_dict)
    }
  }
}, silent=TRUE)

GeoMongo documentation built on Sept. 11, 2021, 5:06 p.m.