std/json-schema/schema

std/json-schema/src/schema.trb

JsonSchema, the model of a schema of draft 2020-12, and the reading of it from JSON.

type PatternSchema

type PatternSchema

One entry of patternProperties: a regular expression and the schema that every property whose name it matches has to satisfy.

field pattern

pattern: Regex

The pattern, compiled on RE2 (std/regex) and searched for anywhere in the name, as the draft says.

field schema

schema: JsonSchema

The schema of the properties it matches.

type JsonSchema

type JsonSchema

A schema of JSON Schema draft 2020-12, as far as its in-place keywords go: JsonSchema.read reads one from text, JsonSchema.parse from a JsonValue, and JsonSchema.validate answers whether a document satisfies it. A schema is an object, or the boolean true (every document is valid) or false (none is).

The fields are the keywords, each None (or empty) where the schema does not use it. enum, const, default, type, if, then, else and not are words of the language, so their fields are named enumeration, constant, defaultValue, types, ifSchema, thenSchema, elseSchema and notSchema; $schema and $comment are schema and comment. The annotation keywords are kept and not evaluated, and a keyword this model does not know is ignored, as the specification says.

Examples

const schema = JsonSchema.read("\{\"type\": \"object\", \"required\": [\"name\"]\}")?
print schema.validate(Json().parse("\{\"name\": \"Ada\"\}")?)
print schema.validate(Json().parse("\{\}")?)

Errors

Reading refuses what it cannot check: a JsonSchemaError names a $schema other than 2020-12, $ref, $dynamicRef, unevaluatedItems and unevaluatedProperties, a keyword with a value of the wrong kind, and a pattern that RE2 does not run.

field boolean

var boolean: Bool? = None

Some(true) or Some(false) for a boolean schema, None for a schema that is an object.

field schema

var schema: String? = None

The $schema the schema names, which is the 2020-12 URI where it is there at all.

field title

var title: String? = None

title, kept.

field description

var description: String? = None

description, kept.

field defaultValue

var defaultValue: JsonValue? = None

default, kept: a validator does not fill it in.

field examples

var examples: List<JsonValue> = []

examples, kept.

field deprecated

var deprecated: Bool? = None

deprecated, kept.

field readOnly

var readOnly: Bool? = None

readOnly, kept.

field writeOnly

var writeOnly: Bool? = None

writeOnly, kept.

field comment

var comment: String? = None

$comment, kept.

field types

var types: List<String> = []

type: the names the document's type has to be one of, empty where the keyword is not there.

field enumeration

var enumeration: List<JsonValue>? = None

enum: the values the document has to equal one of.

field constant

var constant: JsonValue? = None

const: the value the document has to equal. Some(JsonValue.Null) is a schema that wants null.

field multipleOf

var multipleOf: Float64? = None

multipleOf: a number above 0.

field maximum

var maximum: Float64? = None

maximum.

field exclusiveMaximum

var exclusiveMaximum: Float64? = None

exclusiveMaximum.

field minimum

var minimum: Float64? = None

minimum.

field exclusiveMinimum

var exclusiveMinimum: Float64? = None

exclusiveMinimum.

field maxLength

var maxLength: Int? = None

maxLength, counted in characters.

field minLength

var minLength: Int? = None

minLength, counted in characters.

field pattern

var pattern: Regex? = None

pattern, compiled.

field maxItems

var maxItems: Int? = None

maxItems.

field minItems

var minItems: Int? = None

minItems.

field uniqueItems

var uniqueItems: Bool? = None

uniqueItems.

field maxContains

var maxContains: Int? = None

maxContains, which only means something next to contains.

field minContains

var minContains: Int? = None

minContains, which only means something next to contains.

field prefixItems

var prefixItems: List<JsonSchema>? = None

prefixItems: the schemas of the first items, by position.

field items

var items: JsonSchema? = None

items: the schema of the items after the ones prefixItems names.

field contains

var contains: JsonSchema? = None

contains: the schema at least one item has to satisfy.

field maxProperties

var maxProperties: Int? = None

maxProperties.

field minProperties

var minProperties: Int? = None

minProperties.

field required

var required: List<String>? = None

required: the names an object has to have.

field dependentRequired

var dependentRequired: Map<String, List<String>>? = None

dependentRequired: for a property, the names that have to be there with it.

field dependentSchemas

var dependentSchemas: Map<String, JsonSchema>? = None

dependentSchemas: for a property, the schema the whole object has to satisfy where it is there.

field properties

var properties: Map<String, JsonSchema>? = None

properties: the schemas of the properties by name.

field patternProperties

var patternProperties: List<PatternSchema>? = None

patternProperties: the schemas of the properties whose names match a pattern, in the order of the schema.

field additionalProperties

var additionalProperties: JsonSchema? = None

additionalProperties: the schema of the properties that properties and patternProperties do not name.

field propertyNames

var propertyNames: JsonSchema? = None

propertyNames: the schema every property name has to satisfy as a string.

field allOf

var allOf: List<JsonSchema>? = None

allOf: every one of these has to be satisfied.

field anyOf

var anyOf: List<JsonSchema>? = None

anyOf: at least one of these has to be satisfied.

field oneOf

var oneOf: List<JsonSchema>? = None

oneOf: exactly one of these has to be satisfied.

field notSchema

var notSchema: JsonSchema? = None

not: this must not be satisfied.

field ifSchema

var ifSchema: JsonSchema? = None

if: where this is satisfied thenSchema applies, and where it is not elseSchema.

field thenSchema

var thenSchema: JsonSchema? = None

then, which only means something next to if.

field elseSchema

var elseSchema: JsonSchema? = None

else, which only means something next to if.

fn read

static fn read(text: String): Result<Self, JsonSchemaError>

Reads a schema from JSON text.

Errors

A JsonSchemaError for text that is not JSON, and for everything JsonSchema.parse refuses.

fn parse

static fn parse(value: JsonValue): Result<Self, JsonSchemaError>

Reads a schema from a document that is already parsed. Keywords of this draft with a value of the wrong kind are refused, and so is what the package does not check yet.

Errors

A JsonSchemaError with the place in the schema. It names the value of a $schema that is not the 2020-12 URI, the $ref, $dynamicRef, unevaluatedItems or unevaluatedProperties that a later step will validate, the pattern or the key of patternProperties that RE2 refuses, and the keyword whose value is of the wrong kind.