jsonschema

package
v0.54.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package jsonschema derives, parses, and validates JSON Schema contracts.

Example
package main

import (
	"fmt"

	"github.com/Tangerg/scope/core/jsonschema"
)

func main() {
	type answer struct {
		Value int `json:"value"`
	}
	schema, err := jsonschema.For[answer]()
	if err != nil {
		panic(err)
	}

	fmt.Println(schema.Validate([]byte(`{"value":42}`)))
}
Output:
<nil>

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalid    = errors.New("jsonschema: invalid schema")
	ErrValidation = errors.New("jsonschema: instance validation failed")
)

Functions

This section is empty.

Types

type Constrained added in v0.54.0

type Constrained interface{ JSONSchemaConstraints() Constraints }

Constrained projects domain-owned limits onto a type or its JSON properties. Runtime validation and schema generation must derive from the same declarations. Implementations use a value receiver.

type Constraints added in v0.54.0

type Constraints struct {
	Minimum    *float64
	Maximum    *float64
	MinLength  *uint64
	MaxLength  *uint64
	MinItems   *uint64
	MaxItems   *uint64
	Properties map[string]Constraints
}

type Enumerator added in v0.47.0

type Enumerator interface {
	JSONSchemaEnum() []string
}

Enumerator is implemented by named string types whose JSON value is one of a fixed set, so the schema derives the enum from the type that owns the vocabulary instead of restating it in field tags. Use a value receiver.

type Modeler

type Modeler interface {
	// JSONSchemaAlias returns a non-nil typed value whose encoding/json/v2 wire shape
	// exactly matches the receiver's custom encoding. The concrete model type is
	// part of the schema contract and must remain stable across calls.
	JSONSchemaAlias() any
}

Modeler is implemented by values whose encoding/json/v2 representation is described by a separate typed model. Implementations must use a value receiver; nested and recursive models share one schema definition set.

type Patterned added in v0.47.0

type Patterned interface {
	JSONSchemaPattern() string
}

Patterned is implemented by named string types constrained to a regular expression, so the schema derives the pattern from the type that owns the rule instead of restating it in field tags. Use a value receiver.

type Schema

type Schema struct {
	// contains filtered or unexported fields
}

Schema is an immutable, compiled JSON Schema. Its zero value is invalid and successfully constructed values are safe for concurrent validation.

func For

func For[T any]() (Schema, error)

For derives and compiles the JSON Schema contract for T. It follows encoding/json/v2 field names and options and the invopop/jsonschema tag dialect. Structs reject additional properties by default. Integer bounds preserve the Go representation even when field tags specify a wider semantic range. Named definitions include their Go package; moving a type changes its canonical schema and any deployment identities derived from it. Custom encodings require Modeler, except standard-library types with known wire contracts. Opaque Schema values cannot be derived from their Go fields.

func Parse

func Parse(raw []byte) (Schema, error)

Parse validates, compiles, and takes ownership of one self-contained JSON Schema document. References may resolve within the document or to standard metaschemas; implicit loading of external resources is prohibited.

func (Schema) Equal added in v0.54.0

func (s Schema) Equal(other Schema) bool

Equal compares valid schemas by their canonical JSON contract.

func (Schema) JSON

func (s Schema) JSON() json.RawMessage

func (Schema) MarshalJSON

func (s Schema) MarshalJSON() ([]byte, error)

func (*Schema) UnmarshalJSON

func (s *Schema) UnmarshalJSON(raw []byte) error

func (Schema) Valid

func (s Schema) Valid() bool

func (Schema) Validate

func (s Schema) Validate(raw []byte) error

Validate requires one RFC 7493 JSON document and validates it against the compiled schema without rounding JSON numbers through floating-point values.

Jump to

Keyboard shortcuts

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