std/json-schema/error

std/json-schema/src/error.trb

The two kinds of problem std/json-schema reports: a schema it cannot read or refuses to read (JsonSchemaError), and a document that does not satisfy a schema (Validation, made of ValidationErrors).

type JsonSchemaError

type JsonSchemaError with Show, Error

Why a schema is not one this package reads: text that is not JSON, a keyword with a value of the wrong kind, a $schema of another draft, a keyword of a later step of the package ($ref, $dynamicRef, unevaluatedItems, unevaluatedProperties), or a pattern that RE2 cannot run. A refusal names what it refused, so a schema is never answered "valid" by a package that did not check part of it.

Examples

use JsonSchema from "std/json-schema"

match JsonSchema.read("\{\"$ref\": \"#/$defs/name\"\}") {
  Ok(_) => print "read"
  Fail(problem) => print problem
}

field message

message: String

What is wrong or refused, naming the keyword or the value.

field location

location: String = ""

Where in the schema it is, as a JSON Pointer: the empty text is the schema itself.

fn show

fn show(): String

The message, led by its place in the schema where it has one: /properties/age: ....

type ValidationError

type ValidationError with Show, Equals

One keyword that a document failed. The two locations are JSON Pointers, the empty text meaning the schema or the document itself.

Examples

use JsonSchema from "std/json-schema"
use Json from "std/json"

const schema = JsonSchema.read("\{\"properties\": \{\"age\": \{\"minimum\": 0\}\}\}")?
const result = schema.validate(Json().parse("\{\"age\": -1\}")?)
for problem in result.errors {
  print "{problem.instanceLocation} {problem.keyword} {problem.schemaLocation}"
}

field keyword

keyword: String

The keyword that failed: minimum, required, anyOf; false for the boolean schema false.

field schemaLocation

schemaLocation: String

The place of the keyword in the schema: /properties/age/minimum.

field instanceLocation

instanceLocation: String

The place in the document that failed: /items/2/name.

field message

message: String

What is wrong, in words.

fn show

fn show(): String

The instance location as a URI fragment (#/items/2/name, # for the document), then the message.

type Validation

type Validation with Show, Equals

The answer of JsonSchema.validate: whether the document is valid and, where it is not, one ValidationError for every keyword it failed, in the order the schema is walked. This is the "basic" output format of the specification as a flat list.

A keyword that tests the document itself (type, minimum, required) reports its own failure. anyOf, oneOf, not, contains, minContains and maxContains cannot name one cause, so they report themselves, and the first two follow it with what each subschema found. The keywords that only hand a part of the document to a subschema (allOf, properties, items, if, then, ...) report what the subschema found, and nothing of their own.

Examples

use JsonSchema from "std/json-schema"
use Json from "std/json"

const schema = JsonSchema.read("\{\"type\": \"string\"\}")?
print schema.validate(Json().parse("1")?)

field valid

valid: Bool

Whether the document satisfies the schema: true exactly when errors is empty.

field errors

errors: List<ValidationError>

What the document failed, empty for a valid document.

fn of

static fn of(errors: List<ValidationError>): Self

The validation of a document that failed errors: valid when there are none.

fn show

fn show(): String

valid for a valid document, and one line #location: message for every failure.