R/utils.R
json_schema_validator.Rd
simple way to validate a json instance under a given schema
json_schema_validator(json_data = NULL, json_schema = NULL)
json_data | a named list specifying the input data to validate against the json-schema |
---|---|
json_schema | a named list specifying the json-schema |
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 )
https://pypi.org/project/jsonschema/, https://python-jsonschema.readthedocs.io/en/latest/
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)