std/data/json/schema/validation

Standard Library documentation

JSON Schema 2020-12 evaluator.

Module

Name
std/data/json/schema/validation
Area
Standard Library
Source
modules/std/data/json/schema/validation.zzm

NAME

std/data/json/schema/validation - JSON Schema 2020-12 evaluator.

SYNOPSIS

  from std/data/json/schema/validation import JSONSchema, validate, valid;

  let schema := new JSONSchema(
    schema: {
      type: "array",
      items: { type: "integer" },
    },
  );

  say( schema.is_valid([ 1, 2, 3 ]) );

  let result := validate( { minimum: 10 }, 4 );
  say( result.errors()[0].error() );

IMPLEMENTATION SUPPORT

This Pure Zuzu module is supported by all implementations of ZuzuScript. Network loading requires std/net/http through std/data/json/schema/core.

DESCRIPTION

This module contains the JSON Schema 2020-12 evaluator. It is normally used through the facade module std/data/json/schema, which re-exports the public API and the error classes.

The evaluator supports boolean schemas, object schemas, references, applicators, object and array validation, numeric and string constraints, dependent keywords, content keywords, and format validation. It validates decoded Zuzu values and uses std/data/json/schema/model to project native PairList, Set, and Bag values into the JSON data model where that is meaningful.

EXPORTS

Classes

  • JSONSchema

    Compiled schema wrapper. Construct with new JSONSchema( schema: ... ). Pass options as a named field when configuration is needed.

    • is_valid( instance )

      Returns true or false. This mode stops after the first failure.

    • validate( instance )

      Returns a ValidationResult and collects independent failures as ValidationError objects.

Functions

  • valid( schema, instance, options := {} )

    Constructs a temporary JSONSchema and returns is_valid(instance).

  • validate( schema, instance, options := {} )

    Constructs a temporary JSONSchema and returns validate(instance).

OPTIONS

options may be a Dict or PairList.

  • base_uri

    Base URI for the root schema.

  • registry

    SchemaRegistry used for references. If omitted, a new registry is created and the root schema is registered in it.

  • loader

    Callable or object with load(uri) used to load missing referenced resources. A string result is decoded as JSON.

  • allow_network

    When true and loader is not supplied, uses HTTPResourceLoader.

  • formats or format_registry

    FormatRegistry used for format checks.

  • format_assert

    Boolean. Defaults to false. When false, format never makes validation fail.

  • unknown_format

    Defaults to ignore. Use fail to report unknown asserted formats.

COPYRIGHT AND LICENCE

std/data/json/schema/validation is copyright Toby Inkster.

It is free software; you may redistribute it and/or modify it under the terms of either the Artistic License 1.0 or the GNU General Public License version 2.