std/json_schema

JSON Schema, draft 2020-12: checking that JSON values are what a schema says, as settings editors and language servers do.

import "std/json"
import "std/json_schema"

JsonSchema person = json_schema(parse_json(read_text("person.schema.json"))?)?
for e in person.validate(parse_json(read_text("ana.json"))?) do
    print(e.instance, e.keyword, e.message)   // /age /properties/age/minimum -1 is less than the minimum, 0
end
Function or method Result
json_schema(schema), json_schema(schema, documents) a Result[JsonSchema]; documents maps URIs to other schemas the schema refers to
schema.validate(value) the JsonSchemaError items of a value, none when it is valid
schema.is_valid(value) whether a value is valid

A JsonSchemaError has the instance, the JSON Pointer of the value that fails, the keyword, where the keyword that fails is in the schema, as /properties/age/minimum, and a message. A schema that is not valid against the meta-schema of 2020-12 is an ERROR of kind "invalid_schema".

Every keyword of the core, applicator, unevaluated, and validation vocabularies is checked; format and the content keywords are annotations, as the draft makes them by default, and a meta-schema of the schema's own that leaves the validation vocabulary out turns those keywords off. $ref and $dynamicRef reach schemas by $id, $anchor, $dynamicAnchor, and JSON Pointer, in the schema, in the documents given with it, and in the meta-schemas of 2020-12, which come with the module. Patterns are std/regex patterns, which take the long names of Unicode categories too, as \p{Letter}.