| Title: | A High-Performance 'JSON Schema' Validator |
|---|---|
| Description: | Validates R values and 'JSON' text against 'JSON Schema' drafts 4, 6, 7, 2019-09 and 2020-12, with results collection and annotations. A schema is compiled once into a validator, which reads R lists and vectors in place, or parses 'JSON' text itself. The evaluator is the 'corvus-json-schema' 'Rust' crate, which passes the whole 'JSON-Schema-Test-Suite'. |
| Authors: | Matthew Adams [aut, cre], Endjin Limited [cph, fnd] |
| Maintainer: | Matthew Adams <[email protected]> |
| License: | Apache License (>= 2) |
| Version: | 0.1.1 |
| Built: | 2026-10-09 21:10:30 UTC |
| Source: | https://github.com/corvus-dotnet/Corvus.JsonSchema |
Validates R values and JSON text against JSON Schema drafts 4, 6, 7, 2019-09 and 2020-12, with results collection and annotations.
Compile a schema once with compile_schema. Then is_valid validates an R value, read in
place, and is_valid_json validates JSON text. evaluate and evaluate_json
report why a value is not valid, and the annotations a schema makes.
The evaluator is the corvus-json-schema Rust crate, one of a family of implementations of one design in
several languages. It passes every test of the JSON-Schema-Test-Suite.
Matthew Adams, Endjin Limited.
https://github.com/corvus-dotnet/Corvus.JsonSchema
Compiles a JSON Schema (draft 4, 6, 7, 2019-09 or 2020-12) into a validator. Compile a schema once and use the validator for every value: compiling does the work that makes validating fast.
compile_schema(schema, default_dialect = "draft2020-12", assert_format = NA, assert_format_in_legacy_drafts = FALSE, assert_content = TRUE, formats = NULL, resolver = NULL, base_uri = NULL, entry_point = NULL, max_depth = 128L)compile_schema(schema, default_dialect = "draft2020-12", assert_format = NA, assert_format_in_legacy_drafts = FALSE, assert_content = TRUE, formats = NULL, resolver = NULL, base_uri = NULL, entry_point = NULL, max_depth = 128L)
schema |
The schema: one string of JSON text, or R values (a list, or |
default_dialect |
The dialect of a schema that has no |
assert_format |
Whether |
assert_format_in_legacy_drafts |
With |
assert_content |
Whether |
formats |
A named list of functions, one for each custom format. Each takes the string and returns whether it is valid. They take precedence over the built-in formats. |
resolver |
A function that gives the document of an absolute URI the schema refers to: JSON text, R values, or
|
base_uri |
The base URI of the schema, for a schema with relative references and no |
entry_point |
A reference, relative to the schema's root, to the subschema to validate against, such as
|
max_depth |
How many times a schema may refer to itself without moving through the value before evaluation is abandoned with an error. |
An object of class corvus_json_schema, for is_valid, is_valid_json,
evaluate and evaluate_json. It holds compiled native code, so it does not survive
saveRDS or a saved workspace: compile the schema again in a new session.
A schema that cannot be compiled raises a condition of class corvus_compilation_error; schema text that is not
JSON, corvus_invalid_json_error; and an R value that is not a JSON value, corvus_value_error. All
inherit from corvus_json_schema_error.
validator <- compile_schema('{ "type": "object", "required": ["id"], "properties": {"id": {"type": "integer", "minimum": 1}} }') is_valid(validator, list(id = 3)) is_valid_json(validator, '{"id": 0}') # The same schema as R values. compile_schema(list(type = "object", required = list("id"))) # A custom format. even <- compile_schema('{"format": "even-length"}', assert_format = TRUE, formats = list("even-length" = function(s) nchar(s) %% 2 == 0)) is_valid(even, "ab")validator <- compile_schema('{ "type": "object", "required": ["id"], "properties": {"id": {"type": "integer", "minimum": 1}} }') is_valid(validator, list(id = 3)) is_valid_json(validator, '{"id": 0}') # The same schema as R values. compile_schema(list(type = "object", required = list("id"))) # A custom format. even <- compile_schema('{"format": "even-length"}', assert_format = TRUE, formats = list("even-length" = function(s) nchar(s) %% 2 == 0)) is_valid(even, "ab")
The version of the corvus-json-schema Rust crate the package was built from.
crate_version()crate_version()
One string, such as "0.1.6".
crate_version()crate_version()
Evaluates an R value (evaluate) or JSON text (evaluate_json) against a compiled schema without stopping
at the first failure, and returns what each keyword found.
evaluate(validator, value, level = "detailed") evaluate_json(validator, json, level = "detailed")evaluate(validator, value, level = "detailed") evaluate_json(validator, json, level = "detailed")
validator |
A compiled schema, from |
value |
An R value, read as |
json |
One string of JSON text. |
level |
How much to report: |
An object of class corvus_evaluation, a list with:
valid |
Whether the value is valid. |
level |
The level asked for. |
results |
A data frame with a row for each result, the root's own among them: |
annotations |
At the |
validator <- compile_schema('{ "type": "object", "required": ["name"], "properties": {"name": {"type": "string", "title": "Name"}, "age": {"type": "integer", "minimum": 0}} }') result <- evaluate(validator, list(age = -1)) result result$results[, c("instance_location", "message")] evaluate_json(validator, '{"name": "Ada"}', level = "verbose")$annotationsvalidator <- compile_schema('{ "type": "object", "required": ["name"], "properties": {"name": {"type": "string", "title": "Name"}, "age": {"type": "integer", "minimum": 0}} }') result <- evaluate(validator, list(age = -1)) result result$results[, c("instance_location", "message")] evaluate_json(validator, '{"name": "Ada"}', level = "verbose")$annotations
is_valid says whether an R value is valid against a compiled schema, reading the value in place.
is_valid_json says whether JSON text is, parsing the text itself, for each element of a character vector.
Both stop at the first failure. Use evaluate to find out why a value is not valid.
is_valid(validator, value) is_valid_json(validator, json)is_valid(validator, value) is_valid_json(validator, json)
validator |
A compiled schema, from |
value |
An R value, read as JSON as described below. |
json |
A character vector of JSON texts. |
is_valid returns TRUE or FALSE. is_valid_json returns a logical vector as long as
json, with NA for an NA element.
NULL is null.
A list with names is an object, and a list without names is an array. So list() is [], and an
empty named list is {}. This is how the jsonlite package's fromJSON(simplifyVector = FALSE)
gives JSON values. Where two elements have one name, the first is read.
A logical, integer, double or character vector of length one is a scalar. A vector of any other length is an
array of scalars, and its names are ignored. Wrap a vector of length one in I for it to read as an
array of one element.
NA of any type is null.
A whole double, such as 3, is an integer as well as a number, in every dialect.
Strings are read as UTF-8 whatever encoding they are marked with.
Nothing else is a JSON value: not NaN or an infinity, a function, an environment, a raw or complex vector, or
an object with a class, such as a data frame, a factor or a date. Convert such a value first. A value is read only
where the schema examines it, so a value the schema ignores is never an error.
An R string cannot hold a NUL character, so JSON text with one in a string can only be validated as text.
A value that is not a JSON value raises corvus_value_error; text that is not JSON,
corvus_invalid_json_error; a schema that refers to itself more than max_depth times without moving
through the value, corvus_depth_error; and an error in a custom format function,
corvus_callback_error. All inherit from corvus_json_schema_error.
validator <- compile_schema('{"type": "array", "items": {"type": "integer"}}') is_valid(validator, 1:3) is_valid(validator, list(1, 2.5)) is_valid(validator, 1) # a scalar, not an array is_valid(validator, I(1)) # an array of one is_valid_json(validator, c("[1, 2]", '["a"]', NA))validator <- compile_schema('{"type": "array", "items": {"type": "integer"}}') is_valid(validator, 1:3) is_valid(validator, list(1, 2.5)) is_valid(validator, 1) # a scalar, not an array is_valid(validator, I(1)) # an array of one is_valid_json(validator, c("[1, 2]", '["a"]', NA))