jsonschema

package
v0.0.0-...-19ddbcd Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2024 License: MIT Imports: 6 Imported by: 11

Documentation

Overview

Package jsonschema reads and describes JSON Schema documents.

Compatible with JSON Schema draft-07 as specified in [draft-handrews-json-schema-01](https://tools.ietf.org/html/draft-handrews-json-schema-01).

Index

Constants

View Source
const (
	UnspecifiedType PrimitiveType = "unspecified"
	NullType                      = "null"
	BooleanType                   = "boolean"
	ObjectType                    = "object"
	ArrayType                     = "array"
	NumberType                    = "number"
	StringType                    = "string"
	IntegerType                   = "integer"
)

Variables

This section is empty.

Functions

func EncodeReferenceTokens

func EncodeReferenceTokens(tokens []ReferenceToken) string

EncodeReferenceTokens encodes the reference tokens to a string.

TODO(sqs): Fully implement the encoding specified in https://tools.ietf.org/html/rfc6901#section-6.

func Walk

func Walk(v Visitor, schema *Schema)

Walk traverses a JSON Schema in depth-first order. It starts by calling v.Visit(schema); schema must not be nil. If the visitor w returned by v.Visit(schema) is not nil, Walk is invoked recursively with visitor w for each of the non-nil children of schema, followed by a call of w.Visit(nil).

Types

type DependencyValue

type DependencyValue struct {
	Schema             *Schema
	RequiredProperties []string
}

func (*DependencyValue) MarshalJSON

func (v *DependencyValue) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*DependencyValue) UnmarshalJSON

func (v *DependencyValue) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

type Enum

type Enum any

type EnumList

type EnumList []Enum

type Format

type Format string

type ID

type ID struct {
	// Base is the base URI that the reference tokens are resolved relative to.
	Base *url.URL

	// ReferenceTokens describe a value in the JSON document identified by the base URI.
	//
	// See [RFC 6091](https://tools.ietf.org/html/rfc6901).
	ReferenceTokens []ReferenceToken
}

An ID identifies a JSON Schema.

The identifier consists of the base URI and any JSON Pointer reference-tokens (see [RFC 6091](https://tools.ietf.org/html/rfc6901)).

func (ID) ResolveReference

func (id ID) ResolveReference(ref []ReferenceToken) ID

ResolveReference returns a copy of id with the provided reference tokens appended.

func (ID) String

func (id ID) String() string

func (ID) URI

func (id ID) URI() *url.URL

URI returns the URI for the ID, resolving the reference tokens relative to the base URI.

If id.Base and id.ReferenceTokens are both nil, it returns nil.

type PrimitiveType

type PrimitiveType string

func (PrimitiveType) String

func (t PrimitiveType) String() string

type PrimitiveTypeList

type PrimitiveTypeList []PrimitiveType

func (PrimitiveTypeList) Len

func (l PrimitiveTypeList) Len() int

func (PrimitiveTypeList) Less

func (l PrimitiveTypeList) Less(i, j int) bool

func (*PrimitiveTypeList) MarshalJSON

func (l *PrimitiveTypeList) MarshalJSON() ([]byte, error)

func (PrimitiveTypeList) Swap

func (l PrimitiveTypeList) Swap(i, j int)

func (*PrimitiveTypeList) UnmarshalJSON

func (l *PrimitiveTypeList) UnmarshalJSON(buf []byte) error

type ReferenceToken

type ReferenceToken struct {
	Name    string // dereference object's named property
	Keyword bool   // if Name != "", whether the Name is a JSON Schema keyword (e.g., "properties", "items", etc.)
	Index   int    // dereference array's index
}

A ReferenceToken describes a one-level traversal in a JSON document. See [RFC 6091](https://tools.ietf.org/html/rfc6901).

type Schema

type Schema struct {
	Comment              *string                      `json:"$comment,omitempty"`
	ID                   *string                      `json:"$id,omitempty"`
	Reference            *string                      `json:"$ref,omitempty"`
	SchemaRef            *string                      `json:"$schema,omitempty"`
	AdditionalItems      *Schema                      `json:"additionalItems,omitempty"`
	AdditionalProperties *Schema                      `json:"additionalProperties,omitempty"`
	AllOf                []*Schema                    `json:"allOf,omitempty"`
	AnyOf                []*Schema                    `json:"anyOf,omitempty"`
	Const                *any                         `json:"const,omitempty"`
	Contains             *Schema                      `json:"contains,omitempty"`
	Default              *any                         `json:"default,omitempty"`
	Definitions          *map[string]*Schema          `json:"definitions,omitempty"`
	Dependencies         *map[string]*DependencyValue `json:"dependencies,omitempty"`
	Description          *string                      `json:"description,omitempty"`
	Else                 *Schema                      `json:"else,omitempty"`
	Enum                 EnumList                     `json:"enum,omitempty"`
	Examples             []any                        `json:"examples,omitempty"`
	ExclusiveMaximum     *float64                     `json:"exclusiveMaximum,omitempty"`
	ExclusiveMinimum     *float64                     `json:"exclusiveMinimum,omitempty"`
	Format               *Format                      `json:"format,omitempty"`
	If                   *Schema                      `json:"if,omitempty"`
	Items                *SchemaOrSchemaList          `json:"items,omitempty"`
	MaxItems             *int64                       `json:"maxItems,omitempty"`
	MaxLength            *int64                       `json:"maxLength,omitempty"`
	MaxProperties        *int64                       `json:"maxProperties,omitempty"`
	Maximum              *float64                     `json:"maximum,omitempty"`
	MinItems             *int64                       `json:"minItems,omitempty"`
	MinLength            *int64                       `json:"minLength,omitempty"`
	MinProperties        *int64                       `json:"minProperties,omitempty"`
	Minimum              *float64                     `json:"minimum,omitempty"`
	MultipleOf           *float64                     `json:"multipleOf,omitempty"`
	Not                  *Schema                      `json:"not,omitempty"`
	OneOf                []*Schema                    `json:"oneOf,omitempty"`
	Pattern              *string                      `json:"pattern,omitempty"`
	PatternProperties    *map[string]*Schema          `json:"patternProperties,omitempty"`
	Properties           *map[string]*Schema          `json:"properties,omitempty"`
	PropertyNames        *Schema                      `json:"propertyNames,omitempty"`
	Required             []string                     `json:"required,omitempty"`
	Then                 *Schema                      `json:"then,omitempty"`
	Title                *string                      `json:"title,omitempty"`
	Type                 PrimitiveTypeList            `json:"type,omitempty"`
	UniqueItems          *bool                        `json:"uniqueItems,omitempty"`

	// Raw is the raw JSON document that this schema was unmarshaled from, if any. It can be used to
	// retrieve and set custom properties (such as for extensions to JSON Schema). It is omitted
	// from the JSON encoding of this value.
	Raw *json.RawMessage `json:"-"`

	IsEmpty   bool `json:"-"` // the schema is "true"
	IsNegated bool `json:"-"` // the schema is "false"

	// Go contains Go-specific extensions that JSON Schema authors can specify.
	Go *struct {
		TaggedUnionType bool `json:"taggedUnionType,omitempty"`
		Pointer         bool `json:"pointer,omitempty"`
	} `json:"!go,omitempty"`
}

Schema is a JSON Schema draft-07 document (as specified in [draft-handrews-json-schema-01](https://tools.ietf.org/html/draft-handrews-json-schema-01)).

func (*Schema) IsRequiredProperty

func (s *Schema) IsRequiredProperty(propertyName string) bool

IsRequiredProperty reports whether propertyName is a required property for instances of this schema.

func (*Schema) MarshalJSON

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

MarshalJSON implements json.Marshaler.

func (*Schema) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler.

type SchemaOrSchemaList

type SchemaOrSchemaList struct {
	Schema  *Schema
	Schemas []*Schema
}

SchemaOrSchemaList represents a value that can be either a valid JSON Schema or an array of valid JSON Schemas.

Exactly 1 field (Schema or Schemas) is set.

The ["items" keyword](https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4.1) is the only keyword that this is used for.

func (*SchemaOrSchemaList) MarshalJSON

func (s *SchemaOrSchemaList) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*SchemaOrSchemaList) UnmarshalJSON

func (s *SchemaOrSchemaList) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

type Visitor

type Visitor interface {
	Visit(schema *Schema, rel []ReferenceToken) (w Visitor)
}

A Visitor's Visit method is invoked for each schema (with the relative reference tokens identifying it) to encountered by Walk. If the result visitor w is not nil, Walk visits each of the subschemas of schema with the visitor w, followed by a call of w.Visit(nil).

Jump to

Keyboard shortcuts

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