jsonschema

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 3 Imported by: 0

README

jsonschema

import "github.com/kitsunium/sdk/pkg/v1/data/codec/jsonschema"

Package jsonschema validates JSON documents somebody else wrote against a JSON Schema 2020-12 they publish — a webhook's payload, an OpenAPI request body, another program's configuration — with the standard library alone.

v, err := jsonschema.NewValidator(schema, jsonschema.Config{})
report, err := v.Validate(body, 1<<20)
if !report.OK() { … report.Paths() … }

The core, applicator and validation vocabularies are compiled; "$ref" resolves within the schema and to the resources Config registers, never over the network; "format" is asserted only for the formats Config.AssertFormats names. Drafts before 2020-12, the dynamic scope, "$vocabulary", the unevaluated and content vocabularies are refused by name. A report's paths follow the validation package's grammar and no message quotes the document.

Index

Constants

const CodeFormatUnknown errs.Code = 222980

CodeFormatUnknown is 0.3.103.4, FormatUnknown.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jsonschema.CodeJSONSchemaFormatUnknown.

Code 0.3.103.4

const CodeKeywordRefused errs.Code = 222978

CodeKeywordRefused is 0.3.103.2, KeywordRefused.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jsonschema.CodeJSONSchemaKeywordRefused.

Code 0.3.103.2

const CodeRefUnresolved errs.Code = 222979

CodeRefUnresolved is 0.3.103.3, RefUnresolved.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jsonschema.CodeJSONSchemaRefUnresolved.

Code 0.3.103.3

const CodeReferenceLoop errs.Code = 222982

CodeReferenceLoop is 0.3.103.6, ReferenceLoop.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jsonschema.CodeJSONSchemaReferenceLoop.

Code 0.3.103.6

const CodeSchemaInvalid errs.Code = 222977

CodeSchemaInvalid is 0.3.103.1, SchemaInvalid.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jsonschema.CodeJSONSchemaSchemaInvalid.

Code 0.3.103.1

const CodeViolation errs.Code = 222981

CodeViolation is 0.3.103.5: the code of a violation no validation code names — type, multipleOf, the combinators, the applicators, format.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jsonschema.CodeJSONSchemaViolation.

Code 0.3.103.5

Variables

var FormatUnknown *errs.Error

FormatUnknown refuses a format to assert without a checker.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jsonschema.FormatUnknown.

Code 0.3.103.4 · reason FORMAT_UNKNOWN · public "A format to assert is not supported"

var KeywordRefused *errs.Error

KeywordRefused refuses a keyword out of scope, or another draft.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jsonschema.KeywordRefused.

Code 0.3.103.2 · reason KEYWORD_REFUSED · public "The schema uses a keyword this validator does not support"

var RefUnresolved *errs.Error

RefUnresolved refuses a "$ref" naming nothing compiled or registered.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jsonschema.RefUnresolved.

Code 0.3.103.3 · reason REF_UNRESOLVED · public "A schema reference could not be resolved"

var ReferenceLoop *errs.Error

ReferenceLoop refuses a "$ref" cycle that consumes nothing.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jsonschema.ReferenceLoop.

Code 0.3.103.6 · reason REFERENCE_LOOP · public "The schema references itself without end"

var SchemaInvalid *errs.Error

SchemaInvalid refuses a schema NewValidator cannot compile.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jsonschema.SchemaInvalid.

Code 0.3.103.1 · reason SCHEMA_INVALID · public "The schema is not a JSON Schema 2020-12 this validator compiles"

var Violation *errs.Error

Violation is the sentinel of CodeViolation.

Initialised with github.com/kitsunium/sdk/internal/core/data/codec/jsonschema.Violation.

Code 0.3.103.5 · reason VIOLATION · public "The document does not satisfy the schema"

func NewValidator

func NewValidator(schema []byte, cfg Config) (*Validator, error)

NewValidator compiles a JSON Schema 2020-12 document, or refuses it with SchemaInvalid, KeywordRefused, RefUnresolved or FormatUnknown.

type Config

Config configures NewValidator — the resources a "$ref" may name, the formats asserted.

type Config = svcjsonschema.Config

Config is an alias of github.com/kitsunium/sdk/internal/service/data/codec/jsonschema.Config, declared as:

type Config struct {
	// Resources are schema documents a "$ref" may name, by URI: each is a
	// resource at that URI, and at its "$id" when it has one. A reference to
	// any other URI is refused; nothing is fetched.
	Resources     map[string][]byte
	// AssertFormats are the formats asserted rather than annotated:
	// "date-time" (RFC 3339), "uuid" and "email" (an addr-spec). Naming
	// another is refused.
	AssertFormats []string
}

type Validator

Validator is a compiled schema, immutable and reused; its Validate returns the report.

type Validator = svcjsonschema.Validator

Validator is an alias of github.com/kitsunium/sdk/internal/service/data/codec/jsonschema.Validator, declared as:

type Validator struct{}

func (Validator) Validate
func (*Validator) Validate(doc []byte, maxBytes int64) (validation.ReportValue, error)

Validate reads doc through strictjson's bounds — at most maxBytes, one value, no duplicated member name — and returns the violations of the schema, located in the validation package's path grammar ("items[2].name"). A message never quotes the document. An empty report means doc is valid; an error means doc was not read: strictjson's refusal, or ReferenceLoop for a "$ref" cycle that consumes nothing.

Generated from docs/api by tools/genindex.

Documentation

Overview

Package jsonschema validates JSON documents somebody else wrote against a JSON Schema 2020-12 they publish — a webhook's payload, an OpenAPI request body, another program's configuration — with the standard library alone.

v, err := jsonschema.NewValidator(schema, jsonschema.Config{})
report, err := v.Validate(body, 1<<20)
if !report.OK() { … report.Paths() … }

The core, applicator and validation vocabularies are compiled; "$ref" resolves within the schema and to the resources Config registers, never over the network; "format" is asserted only for the formats Config.AssertFormats names. Drafts before 2020-12, the dynamic scope, "$vocabulary", the unevaluated and content vocabularies are refused by name. A report's paths follow the validation package's grammar and no message quotes the document.

Index

Constants

View Source
const (
	// CodeSchemaInvalid is 0.3.103.1, [SchemaInvalid].
	CodeSchemaInvalid errs.Code = corejsonschema.CodeJSONSchemaSchemaInvalid
	// CodeKeywordRefused is 0.3.103.2, [KeywordRefused].
	CodeKeywordRefused errs.Code = corejsonschema.CodeJSONSchemaKeywordRefused
	// CodeRefUnresolved is 0.3.103.3, [RefUnresolved].
	CodeRefUnresolved errs.Code = corejsonschema.CodeJSONSchemaRefUnresolved
	// CodeFormatUnknown is 0.3.103.4, [FormatUnknown].
	CodeFormatUnknown errs.Code = corejsonschema.CodeJSONSchemaFormatUnknown
	// CodeViolation is 0.3.103.5: the code of a violation no validation code
	// names — type, multipleOf, the combinators, the applicators, format.
	CodeViolation errs.Code = corejsonschema.CodeJSONSchemaViolation
	// CodeReferenceLoop is 0.3.103.6, [ReferenceLoop].
	CodeReferenceLoop errs.Code = corejsonschema.CodeJSONSchemaReferenceLoop
)

The error codes, range 0.3.103.*, declared in the core (ADR 0160).

Variables

View Source
var (
	// SchemaInvalid refuses a schema NewValidator cannot compile.
	SchemaInvalid = corejsonschema.SchemaInvalid
	// KeywordRefused refuses a keyword out of scope, or another draft.
	KeywordRefused = corejsonschema.KeywordRefused
	// RefUnresolved refuses a "$ref" naming nothing compiled or registered.
	RefUnresolved = corejsonschema.RefUnresolved
	// FormatUnknown refuses a format to assert without a checker.
	FormatUnknown = corejsonschema.FormatUnknown
	// Violation is the sentinel of [CodeViolation].
	Violation = corejsonschema.Violation
	// ReferenceLoop refuses a "$ref" cycle that consumes nothing.
	ReferenceLoop = corejsonschema.ReferenceLoop
)

The sentinels, for errors.Is: each carries the code of the same name.

Functions

This section is empty.

Types

type Config

type Config = svcjsonschema.Config

Config configures NewValidator — the resources a "$ref" may name, the formats asserted.

type Validator

type Validator = svcjsonschema.Validator

Validator is a compiled schema, immutable and reused; its Validate returns the report.

func NewValidator

func NewValidator(schema []byte, cfg Config) (*Validator, error)

NewValidator compiles a JSON Schema 2020-12 document, or refuses it with SchemaInvalid, KeywordRefused, RefUnresolved or FormatUnknown.

Jump to

Keyboard shortcuts

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