schema

package
v0.1.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package schema checks a value against the JSON Schema 2020-12 schemas of the format version the library reads, reporting every keyword that fails with its place (format/README.md, sections 5 and 8), and builds the open view of a value from the marks x-open in the same schemas (section 6). The schemas are a copy of schemas/ of spec.Format at the tag go.mod requires, built into the package, so that the code holds the schemas of the version it runs, as module.FormatVersion holds the version; a test holds the copy to spec.Format byte for byte (R23).

The validator is github.com/santhosh-tekuri/jsonschema/v6. It collects no annotations, so Open walks the schemas by section 6 itself and asks the validator only whether a subschema passes, as spec's format-check asks ajv (R23). Decode reads a JSON value into a type of formatv0, and ValueOf gives the JSON value of one back. The check of a module, the open view, the templates and the test of vectors call it.

Index

Constants

This section is empty.

Variables

View Source
var Keywords = []string{
	"$schema", "$id", "$ref", "$defs", "$comment",
	"allOf", "anyOf", "oneOf", "not", "if", "then", "else", "prefixItems", "items", "contains", "properties",
	"patternProperties", "additionalProperties", "propertyNames", "unevaluatedProperties",
	"type", "enum", "const", "multipleOf", "maximum", "exclusiveMaximum", "minimum", "exclusiveMinimum", "maxLength",
	"minLength", "pattern", "maxItems", "minItems", "uniqueItems", "maxProperties", "minProperties", "required",
	"dependentRequired",
	"title", "description", "default", "deprecated", "readOnly", "writeOnly", "examples", "format",
	"x-open",
}

Keywords are the keywords that a schema of the format uses, and so the only ones an Error may name, as section 5 of format/README.md lists them; a test holds the list to the text.

Functions

func Decode added in v0.1.12

func Decode[T any](value any) (T, error)

Decode reads a JSON value, as the reader of YAML gives it, into a Go type strictly: a field that the type does not know is an error. A reader of the format checks a value against its schema first, and only then reads it by its type of formatv0 (R72 spec).

func Files

func Files() []string

Files names the schemas of the format, as schemas/ of spec.Format names them.

func Open added in v0.1.11

func Open(location string, value any) any

Open gives the open view of value at location, as Validate takes them: value without its closed fields, which is what the learner's app receives of a part (format/README.md, section 6). A field is open when a subschema of its place that applies and passes carries "x-open": true, an element of an open array is open, and the root is open; an open object whose fields are all closed stays, as an empty object. The view is made of new maps and slices, and value is left as it is.

A mark counts only where its subschema applies and passes, as JSON Schema 2020-12 collects an annotation (core, section 7.7.1.2), so Open asks the validator whether each branch of anyOf and oneOf, and each if, passes. The view is the format's of a value valid against the schema at location; of another value Open gives what the same walk gives. Like Validate, it panics on a location or a reference that names nothing in the set, and it may be called from many goroutines at once.

func Pointer

func Pointer(tokens []string) string

Pointer writes a place in a value, token by token, as a JSON Pointer in URI fragment form (RFC 6901, sections 3 and 6): # for the whole value, then / before each token, with ~ written ~0 and / written ~1, and what a fragment of a URI cannot hold percent-encoded, as in #/modules/example~1smallest/0.1.0/withdrawn.

func ValueOf added in v0.1.19

func ValueOf(value any) any

ValueOf gives a Go value, such as a part as its type of formatv0 holds it, as the reader of YAML gives a JSON value, the other way from Decode: map[string]any, []any, string, int64 for a number without a fraction or an exponent, float64 for another, bool or nil. A part of formatv0 holds only strings, integers, booleans and lists and maps of them, so it is always JSON; a value that encoding/json does not write, such as a NaN, is a fault of module-kit, and ValueOf panics.

Types

type Error

type Error struct {
	Keyword string
	// The place in the value, token by token from its root: the name of a member, or the index of
	// an element in decimal digits. Pointer writes it as a JSON Pointer.
	Location []string
	Message  string
}

An Error is one keyword that fails, at the place where the schema holding it applies, as an example of the format names it: the object for required, unevaluatedProperties and the other keywords of an object, the array for those of an array, and the value for type, pattern and the other keywords of a value (format/README.md, section 8).

func Validate

func Validate(location string, value any) []Error

Validate checks value against the schema at location: the name of a schema's file, such as task.schema.json, or that name with a JSON Pointer to a subschema in it as its fragment, such as task.schema.json#/$defs/check. value is a JSON value as the reader of YAML gives it: map[string]any, []any, string, int64, float64, bool or nil. It gives every keyword that fails, and none when the value is valid. Each schema is compiled once, and Validate may be called from many goroutines at once.

A location that names no schema of the format, and a schema that does not compile, are faults in module-kit rather than in the value, and Validate panics on them, as regexp.MustCompile does.

func (Error) Pointer

func (e Error) Pointer() string

Pointer gives the place of the error as a JSON Pointer in URI fragment form, as the first line of an invalid example writes it: #/answer_slots/0.

func (Error) String

func (e Error) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL