jsonvalidate (version 1.1.0)

json_validate: Validate a json file

Description

Validate a single json against a schema. This is a convenience wrapper around json_validator(schema)(json)

Usage

json_validate(json, schema, verbose = FALSE, greedy = FALSE,
  error = FALSE, engine = "imjv")

Arguments

json

Contents of a json object, or a filename containing one.

schema

Contents of the json schema, or a filename containing a schema.

verbose

Be verbose? If TRUE, then an attribute "errors" will list validation failures as a data.frame

greedy

Continue after the first error?

error

Throw an error on parse failure? If TRUE, then the function returns NULL on success (i.e., call only for the side-effect of an error on failure, like stopifnot).

engine

Specify the validation engine to use. Options are "imjv" (the default; which uses "is-my-json-valid") and "ajv" (Another JSON Schema Validator). The latter supports more recent json schema features.

Examples

Run this code
# NOT RUN {
# A simple schema example:
schema <- '{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Product",
    "description": "A product from Acme\'s catalog",
    "type": "object",
    "properties": {
        "id": {
            "description": "The unique identifier for a product",
            "type": "integer"
        },
        "name": {
            "description": "Name of the product",
            "type": "string"
        },
        "price": {
            "type": "number",
            "minimum": 0,
            "exclusiveMinimum": true
        },
        "tags": {
            "type": "array",
            "items": {
                "type": "string"
            },
            "minItems": 1,
            "uniqueItems": true
        }
    },
    "required": ["id", "name", "price"]
}'

# Test if some (invalid) json conforms to the schema
jsonvalidate::json_validate("{}", schema, verbose = TRUE)

# Test if some (valid) json conforms to the schema
jsonvalidate::json_validate('{
    "id": 1,
    "name": "A green door",
    "price": 12.50,
    "tags": ["home", "green"]
}', schema)
# }

Run the code above in your browser using DataCamp Workspace