jsonschema

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2021 License: MIT Imports: 19 Imported by: 0

README

jsonschema

Qri GoDoc License Codecov CI Go Report Card

golang implementation of the JSON Schema Specification, which lets you write JSON that validates some other json. Rad.

Package Features
  • Encode schemas back to JSON
  • Supply Your own Custom Validators
  • Uses Standard Go idioms
  • Fastest Go implementation of JSON Schema validators (draft2019_9 only, (old — draft 7) benchmarks are here — thanks @TheWildBlue!)
Getting Involved

We would love involvement from more people! If you notice any errors or would like to submit changes, please see our Contributing Guidelines.

Developing

We’ve set up a separate document for developer guidelines!

Basic Usage

Here’s a quick example pulled from the godoc:

package main

import (
	"context"
	"encoding/json"
	"fmt"

	"github.com/qri-io/jsonschema"
)

func main() {
	ctx := context.Background()
	var schemaData = []byte(`{
    "$id": "https://qri.io/schema/",
    "$comment" : "sample comment",
    "title": "Person",
    "type": "object",
    "properties": {
        "firstName": {
            "type": "string"
        },
        "lastName": {
            "type": "string"
        },
        "age": {
            "description": "Age in years",
            "type": "integer",
            "minimum": 0
        },
        "friends": {
          "type" : "array",
          "items" : { "title" : "REFERENCE", "$ref" : "#" }
        }
    },
    "required": ["firstName", "lastName"]
  }`)

	rs := &jsonschema.Schema{}
	if err := json.Unmarshal(schemaData, rs); err != nil {
		panic("unmarshal schema: " + err.Error())
	}

	var valid = []byte(`{
    "firstName" : "George",
    "lastName" : "Michael"
    }`)
	errs, err := rs.ValidateBytes(ctx, valid)
	if err != nil {
		panic(err)
	}

	if len(errs) > 0 {
		fmt.Println(errs[0].Error())
	}

	var invalidPerson = []byte(`{
    "firstName" : "Prince"
    }`)

	errs, err = rs.ValidateBytes(ctx, invalidPerson)
	if err != nil {
		panic(err)
	}
	if len(errs) > 0 {
		fmt.Println(errs[0].Error())
	}

	var invalidFriend = []byte(`{
    "firstName" : "Jay",
    "lastName" : "Z",
    "friends" : [{
      "firstName" : "Nas"
      }]
    }`)
	errs, err = rs.ValidateBytes(ctx, invalidFriend)
	if err != nil {
		panic(err)
	}
	if len(errs) > 0 {
		fmt.Println(errs[0].Error())
	}
}

// Output:
// /: {"firstName":"Prince... "lastName" value is required
// /friends/0: {"firstName":"Nas"} "lastName" value is required

Custom Keywords

The godoc gives an example of how to supply your own validators to extend the standard keywords supported by the spec.

It involves three steps that should happen before allocating any Schema instances that use the validator:

  1. create a custom type that implements the Keyword interface
  2. Load the appropriate draft keyword set (see draft2019_09_keywords.go)
  3. call RegisterKeyword with the keyword you’d like to detect in JSON, and a KeyMaker function.
package main

import (
    "context"
    "encoding/json"
    "fmt"

    jptr "github.com/qri-io/jsonpointer"
    "github.com/qri-io/jsonschema"
)

// your custom validator
type IsFoo bool

// newIsFoo is a jsonschama.KeyMaker
func newIsFoo() jsonschema.Keyword {
    return new(IsFoo)
}

// Validate implements jsonschema.Keyword
func (f *IsFoo) Validate(propPath string, data interface{}, errs *[]jsonschema.KeyError) {}

// Register implements jsonschema.Keyword
func (f *IsFoo) Register(uri string, registry *jsonschema.SchemaRegistry) {}

// Resolve implements jsonschema.Keyword
func (f *IsFoo) Resolve(pointer jptr.Pointer, uri string) *jsonschema.Schema {
    return nil
}

// ValidateKeyword implements jsonschema.Keyword
func (f *IsFoo) ValidateKeyword(ctx context.Context, currentState *jsonschema.ValidationState, data interface{}) {
    if str, ok := data.(string); ok {
        if str != "foo" {
            currentState.AddError(data, fmt.Sprintf("should be foo. plz make '%s' == foo. plz", str))
        }
    }
}

func main() {
    // register a custom validator by supplying a function
    // that creates new instances of your Validator.
    jsonschema.RegisterKeyword("foo", newIsFoo)

    // If you register a custom validator, you'll need to manually register
    // any other JSON Schema validators you need.
    jsonschema.LoadDraft2019_09()

    schBytes := []byte(`{ "foo": true }`)

    rs := new(jsonschema.Schema)
    if err := json.Unmarshal(schBytes, rs); err != nil {
        // Real programs handle errors.
        panic(err)
    }

    errs, err := rs.ValidateBytes(context.Background(), []byte(`"bar"`))
    if err != nil {
        panic(err)
    }
    fmt.Println(errs[0].Error())
    // Output: /: "bar" should be foo. plz make 'bar' == foo. plz
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MaxKeywordErrStringLen = 20

MaxKeywordErrStringLen sets how long a value can be before it's length is truncated when printing error strings a special value of -1 disables output trimming

Functions

func DataType

func DataType(data interface{}) string

DataType attempts to parse the underlying data type from the raw data interface

func DataTypeWithHint

func DataTypeWithHint(data interface{}, hint string) string

DataTypeWithHint attempts to parse the underlying data type by leveraging the schema expectations for better results

func FetchSchema

func FetchSchema(ctx context.Context, uri string, schema *Schema) error

FetchSchema downloads and loads a schema from a remote location

func FileSchemaLoader added in v1.0.0

func FileSchemaLoader(ctx context.Context, uri *url.URL, schema *Schema) error

FileSchemaLoader loads a schema from a file URI

func HTTPSchemaLoader added in v1.0.0

func HTTPSchemaLoader(ctx context.Context, uri *url.URL, schema *Schema) error

HTTPSchemaLoader loads a schema from a http or https URI

func InvalidValueString

func InvalidValueString(data interface{}) string

InvalidValueString returns the errored value as a string

func IsLocalSchemaID

func IsLocalSchemaID(id string) bool

IsLocalSchemaID validates if a given id is a local id

func LoadDraft2019_09

func LoadDraft2019_09()

LoadDraft2019_09 loads the keywords for schema validation based on draft2019_09 this is also the default keyword set loaded automatically if no other is loaded

func RegisterKeyword

func RegisterKeyword(prop string, maker KeyMaker)

RegisterKeyword registers a keyword with the registry

func ResetSchemaRegistry

func ResetSchemaRegistry()

ResetSchemaRegistry resets the main SchemaRegistry

func SafeResolveURL

func SafeResolveURL(ctxURL, resURL string) (string, error)

SafeResolveURL resolves a string url against the current context url

func SetKeywordOrder

func SetKeywordOrder(prop string, order int)

SetKeywordOrder assigns a given order to a keyword

Types

type AdditionalItems

type AdditionalItems Schema

AdditionalItems defines the additionalItems JSON Schema keyword

func (*AdditionalItems) Register

func (ai *AdditionalItems) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for AdditionalItems

func (*AdditionalItems) Resolve

func (ai *AdditionalItems) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for AdditionalItems

func (*AdditionalItems) UnmarshalJSON

func (ai *AdditionalItems) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for AdditionalItems

func (*AdditionalItems) ValidateKeyword

func (ai *AdditionalItems) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for AdditionalItems

type AdditionalProperties

type AdditionalProperties Schema

AdditionalProperties defines the additionalProperties JSON Schema keyword

func (*AdditionalProperties) Register

func (ap *AdditionalProperties) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for AdditionalProperties

func (*AdditionalProperties) Resolve

func (ap *AdditionalProperties) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for AdditionalProperties

func (*AdditionalProperties) UnmarshalJSON

func (ap *AdditionalProperties) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for AdditionalProperties

func (*AdditionalProperties) ValidateKeyword

func (ap *AdditionalProperties) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for AdditionalProperties

type AllOf

type AllOf []*Schema

AllOf defines the allOf JSON Schema keyword

func (AllOf) JSONChildren

func (a AllOf) JSONChildren() (res map[string]JSONPather)

JSONChildren implements the JSONContainer interface for AllOf

func (AllOf) JSONProp

func (a AllOf) JSONProp(name string) interface{}

JSONProp implements the JSONPather for AllOf

func (*AllOf) Register

func (a *AllOf) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for AllOf

func (*AllOf) Resolve

func (a *AllOf) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for AllOf

func (*AllOf) ValidateKeyword

func (a *AllOf) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for AllOf

type Anchor

type Anchor string

Anchor defines the $anchor JSON Schema keyword

func (*Anchor) Register

func (a *Anchor) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Anchor

func (*Anchor) Resolve

func (a *Anchor) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Anchor

func (*Anchor) ValidateKeyword

func (a *Anchor) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Anchor

type AnyOf

type AnyOf []*Schema

AnyOf defines the anyOf JSON Schema keyword

func (AnyOf) JSONChildren

func (a AnyOf) JSONChildren() (res map[string]JSONPather)

JSONChildren implements the JSONContainer interface for AnyOf

func (AnyOf) JSONProp

func (a AnyOf) JSONProp(name string) interface{}

JSONProp implements the JSONPather for AnyOf

func (*AnyOf) Register

func (a *AnyOf) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for AnyOf

func (*AnyOf) Resolve

func (a *AnyOf) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for AnyOf

func (*AnyOf) ValidateKeyword

func (a *AnyOf) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for AnyOf

type Comment

type Comment string

Comment defines the comment JSON Schema keyword

func (*Comment) Register

func (c *Comment) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Comment

func (*Comment) Resolve

func (c *Comment) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Comment

func (*Comment) ValidateKeyword

func (c *Comment) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Comment

type Const

type Const json.RawMessage

Const defines the const JSON Schema keyword

func (Const) JSONProp

func (c Const) JSONProp(name string) interface{}

JSONProp implements the JSONPather for Const

func (Const) MarshalJSON

func (c Const) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for Const

func (*Const) Register

func (c *Const) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Const

func (*Const) Resolve

func (c *Const) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Const

func (Const) String

func (c Const) String() string

String implements the Stringer for Const

func (*Const) UnmarshalJSON

func (c *Const) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for Const

func (Const) ValidateKeyword

func (c Const) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Const

type Contains

type Contains Schema

Contains defines the contains JSON Schema keyword

func (Contains) JSONChildren

func (c Contains) JSONChildren() (res map[string]JSONPather)

JSONChildren implements the JSONContainer interface for Contains

func (Contains) JSONProp

func (c Contains) JSONProp(name string) interface{}

JSONProp implements the JSONPather for Contains

func (*Contains) Register

func (c *Contains) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Contains

func (*Contains) Resolve

func (c *Contains) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Contains

func (*Contains) UnmarshalJSON

func (c *Contains) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for Contains

func (*Contains) ValidateKeyword

func (c *Contains) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Contains

type Default

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

Default defines the default JSON Schema keyword

func (*Default) Register

func (d *Default) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Default

func (*Default) Resolve

func (d *Default) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Default

func (*Default) UnmarshalJSON

func (d *Default) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for Default

func (*Default) ValidateKeyword

func (d *Default) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Default

type Defs

type Defs map[string]*Schema

Defs defines the $defs JSON Schema keyword

func (Defs) JSONChildren

func (d Defs) JSONChildren() (res map[string]JSONPather)

JSONChildren implements the JSONContainer interface for Defs

func (Defs) JSONProp

func (d Defs) JSONProp(name string) interface{}

JSONProp implements the JSONPather for Defs

func (*Defs) Register

func (d *Defs) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Defs

func (*Defs) Resolve

func (d *Defs) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Defs

func (Defs) ValidateKeyword

func (d Defs) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Defs

type DependentRequired

type DependentRequired map[string]PropertyDependency

DependentRequired defines the dependentRequired JSON Schema keyword

func (DependentRequired) JSONChildren

func (d DependentRequired) JSONChildren() (r map[string]JSONPather)

JSONChildren implements the JSONContainer interface for DependentRequired

func (DependentRequired) JSONProp

func (d DependentRequired) JSONProp(name string) interface{}

JSONProp implements the JSONPather for DependentRequired

func (DependentRequired) MarshalJSON

func (d DependentRequired) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for DependentRequired

func (*DependentRequired) Register

func (d *DependentRequired) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for DependentRequired

func (*DependentRequired) Resolve

func (d *DependentRequired) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for DependentRequired

func (*DependentRequired) UnmarshalJSON

func (d *DependentRequired) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for DependentRequired

func (*DependentRequired) ValidateKeyword

func (d *DependentRequired) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for DependentRequired

type DependentSchemas

type DependentSchemas map[string]SchemaDependency

DependentSchemas defines the dependentSchemas JSON Schema keyword

func (DependentSchemas) JSONChildren

func (d DependentSchemas) JSONChildren() (r map[string]JSONPather)

JSONChildren implements the JSONContainer interface for DependentSchemas

func (DependentSchemas) JSONProp

func (d DependentSchemas) JSONProp(name string) interface{}

JSONProp implements the JSONPather for DependentSchemas

func (*DependentSchemas) Register

func (d *DependentSchemas) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for DependentSchemas

func (*DependentSchemas) Resolve

func (d *DependentSchemas) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for DependentSchemas

func (*DependentSchemas) UnmarshalJSON

func (d *DependentSchemas) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for DependentSchemas

func (*DependentSchemas) ValidateKeyword

func (d *DependentSchemas) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for DependentSchemas

type Description

type Description string

Description defines the description JSON Schema keyword

func (*Description) Register

func (d *Description) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Description

func (*Description) Resolve

func (d *Description) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Description

func (*Description) ValidateKeyword

func (d *Description) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Description

type Else

type Else Schema

Else defines the else JSON Schema keyword

func (Else) JSONChildren

func (e Else) JSONChildren() (res map[string]JSONPather)

JSONChildren implements the JSONContainer interface for Else

func (Else) JSONProp

func (e Else) JSONProp(name string) interface{}

JSONProp implements the JSONPather for Else

func (Else) MarshalJSON

func (e Else) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for Else

func (*Else) Register

func (e *Else) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Else

func (*Else) Resolve

func (e *Else) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Else

func (*Else) UnmarshalJSON

func (e *Else) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for Else

func (*Else) ValidateKeyword

func (e *Else) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Else

type Enum

type Enum []Const

Enum defines the enum JSON Schema keyword

func (Enum) JSONChildren

func (e Enum) JSONChildren() (res map[string]JSONPather)

JSONChildren implements the JSONContainer interface for Enum

func (Enum) JSONProp

func (e Enum) JSONProp(name string) interface{}

JSONProp implements the JSONPather for Enum

func (*Enum) Register

func (e *Enum) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Enum

func (*Enum) Resolve

func (e *Enum) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Enum

func (Enum) String

func (e Enum) String() string

String implements the Stringer for Enum

func (Enum) ValidateKeyword

func (e Enum) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Enum

type Examples

type Examples []interface{}

Examples defines the examples JSON Schema keyword

func (*Examples) Register

func (e *Examples) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Examples

func (*Examples) Resolve

func (e *Examples) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Examples

func (*Examples) ValidateKeyword

func (e *Examples) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Examples

type ExclusiveMaximum

type ExclusiveMaximum float64

ExclusiveMaximum defines the exclusiveMaximum JSON Schema keyword

func (*ExclusiveMaximum) Register

func (m *ExclusiveMaximum) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for ExclusiveMaximum

func (*ExclusiveMaximum) Resolve

func (m *ExclusiveMaximum) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for ExclusiveMaximum

func (ExclusiveMaximum) ValidateKeyword

func (m ExclusiveMaximum) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for ExclusiveMaximum

type ExclusiveMinimum

type ExclusiveMinimum float64

ExclusiveMinimum defines the exclusiveMinimum JSON Schema keyword

func (*ExclusiveMinimum) Register

func (m *ExclusiveMinimum) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for ExclusiveMinimum

func (*ExclusiveMinimum) Resolve

func (m *ExclusiveMinimum) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for ExclusiveMinimum

func (ExclusiveMinimum) ValidateKeyword

func (m ExclusiveMinimum) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for ExclusiveMinimum

type Format

type Format string

Format defines the format JSON Schema keyword

func (*Format) Register

func (f *Format) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Format

func (*Format) Resolve

func (f *Format) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Format

func (Format) ValidateKeyword

func (f Format) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Format

type ID

type ID string

ID defines the $id JSON Schema keyword

func (*ID) Register

func (i *ID) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for ID

func (*ID) Resolve

func (i *ID) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for ID

func (*ID) ValidateKeyword

func (i *ID) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for ID

type If

type If Schema

If defines the if JSON Schema keyword

func (If) JSONChildren

func (f If) JSONChildren() (res map[string]JSONPather)

JSONChildren implements the JSONContainer interface for If

func (If) JSONProp

func (f If) JSONProp(name string) interface{}

JSONProp implements the JSONPather for If

func (If) MarshalJSON

func (f If) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for If

func (*If) Register

func (f *If) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for If

func (*If) Resolve

func (f *If) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for If

func (*If) UnmarshalJSON

func (f *If) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for If

func (*If) ValidateKeyword

func (f *If) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for If

type Items

type Items struct {
	Schemas []*Schema
	// contains filtered or unexported fields
}

Items defines the items JSON Schema keyword

func (Items) JSONChildren

func (it Items) JSONChildren() (res map[string]JSONPather)

JSONChildren implements the JSONContainer interface for Items

func (Items) JSONProp

func (it Items) JSONProp(name string) interface{}

JSONProp implements the JSONPather for Items

func (Items) MarshalJSON

func (it Items) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for Items

func (*Items) Register

func (it *Items) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Items

func (*Items) Resolve

func (it *Items) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Items

func (*Items) UnmarshalJSON

func (it *Items) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for Items

func (Items) ValidateKeyword

func (it Items) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Items

type JSONContainer

type JSONContainer interface {
	// JSONChildren should return all immidiate children of this element
	JSONChildren() map[string]JSONPather
}

JSONContainer is an interface that enables tree traversal by listing the immideate children of an object

type JSONPather

type JSONPather interface {
	// JSONProp take a string references for a given JSON property
	// implementations must return any matching property of that name
	// or nil if no such subproperty exists.
	// Note this also applies to array values, which are expected to interpret
	// valid numbers as an array index
	JSONProp(name string) interface{}
}

JSONPather makes validators traversible by JSON-pointers, which is required to support references in JSON schemas.

type KeyError

type KeyError struct {
	// PropertyPath is a string path that leads to the
	// property that produced the error
	PropertyPath string `json:"propertyPath,omitempty"`
	// InvalidValue is the value that returned the error
	InvalidValue interface{} `json:"invalidValue,omitempty"`
	// Message is a human-readable description of the error
	Message string `json:"message"`
}

KeyError represents a single error in an instance of a schema The only absolutely-required property is Message.

func (KeyError) Error

func (v KeyError) Error() string

Error implements the error interface for KeyError

type KeyMaker

type KeyMaker func() Keyword

KeyMaker is a function that generates instances of a Keyword. Calls to KeyMaker will be passed directly to json.Marshal, so the returned value should be a pointer

type Keyword

type Keyword interface {
	// ValidateKeyword checks decoded JSON data and writes
	// validation errors (if any) to an outparam slice of KeyErrors
	ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

	// Register builds up the schema tree by evaluating the current key
	// and the current location pointer which is later used with resolve to
	// navigate the schema tree and substitute the propper schema for a given
	// reference.
	Register(uri string, registry *SchemaRegistry)
	// Resolve unraps a pointer to the destination schema
	// It usually starts with a $ref validation call which
	// uses the pointer token by token to navigate the
	// schema tree to get to the last schema in the chain.
	// Since every keyword can have it's specifics around resolving
	// each keyword need to implement it's own version of Resolve.
	// Terminal keywords should respond with nil as it's not a schema
	// Keywords that wrap a schema should return the appropriate schema.
	// In case of a non-existing location it will fail to resolve, return nil
	// on ref resolution and error out.
	Resolve(pointer jptr.Pointer, uri string) *Schema
}

Keyword is an interface for anything that can validate. JSON-Schema keywords are all examples of Keyword

func NewAdditionalItems

func NewAdditionalItems() Keyword

NewAdditionalItems allocates a new AdditionalItems keyword

func NewAdditionalProperties

func NewAdditionalProperties() Keyword

NewAdditionalProperties allocates a new AdditionalProperties keyword

func NewAllOf

func NewAllOf() Keyword

NewAllOf allocates a new AllOf keyword

func NewAnchor

func NewAnchor() Keyword

NewAnchor allocates a new Anchor keyword

func NewAnyOf

func NewAnyOf() Keyword

NewAnyOf allocates a new AnyOf keyword

func NewComment

func NewComment() Keyword

NewComment allocates a new Comment keyword

func NewConst

func NewConst() Keyword

NewConst allocates a new Const keyword

func NewContains

func NewContains() Keyword

NewContains allocates a new Contains keyword

func NewDefault

func NewDefault() Keyword

NewDefault allocates a new Default keyword

func NewDefs

func NewDefs() Keyword

NewDefs allocates a new Defs keyword

func NewDependentRequired

func NewDependentRequired() Keyword

NewDependentRequired allocates a new DependentRequired keyword

func NewDependentSchemas

func NewDependentSchemas() Keyword

NewDependentSchemas allocates a new DependentSchemas keyword

func NewDescription

func NewDescription() Keyword

NewDescription allocates a new Description keyword

func NewElse

func NewElse() Keyword

NewElse allocates a new Else keyword

func NewEnum

func NewEnum() Keyword

NewEnum allocates a new Enum keyword

func NewExamples

func NewExamples() Keyword

NewExamples allocates a new Examples keyword

func NewExclusiveMaximum

func NewExclusiveMaximum() Keyword

NewExclusiveMaximum allocates a new ExclusiveMaximum keyword

func NewExclusiveMinimum

func NewExclusiveMinimum() Keyword

NewExclusiveMinimum allocates a new ExclusiveMinimum keyword

func NewFormat

func NewFormat() Keyword

NewFormat allocates a new Format keyword

func NewID

func NewID() Keyword

NewID allocates a new Id keyword

func NewIf

func NewIf() Keyword

NewIf allocates a new If keyword

func NewItems

func NewItems() Keyword

NewItems allocates a new Items keyword

func NewMaxContains

func NewMaxContains() Keyword

NewMaxContains allocates a new MaxContains keyword

func NewMaxItems

func NewMaxItems() Keyword

NewMaxItems allocates a new MaxItems keyword

func NewMaxLength

func NewMaxLength() Keyword

NewMaxLength allocates a new MaxLength keyword

func NewMaxProperties

func NewMaxProperties() Keyword

NewMaxProperties allocates a new MaxProperties keyword

func NewMaximum

func NewMaximum() Keyword

NewMaximum allocates a new Maximum keyword

func NewMinContains

func NewMinContains() Keyword

NewMinContains allocates a new MinContains keyword

func NewMinItems

func NewMinItems() Keyword

NewMinItems allocates a new MinItems keyword

func NewMinLength

func NewMinLength() Keyword

NewMinLength allocates a new MinLength keyword

func NewMinProperties

func NewMinProperties() Keyword

NewMinProperties allocates a new MinProperties keyword

func NewMinimum

func NewMinimum() Keyword

NewMinimum allocates a new Minimum keyword

func NewMultipleOf

func NewMultipleOf() Keyword

NewMultipleOf allocates a new MultipleOf keyword

func NewNot

func NewNot() Keyword

NewNot allocates a new Not keyword

func NewOneOf

func NewOneOf() Keyword

NewOneOf allocates a new OneOf keyword

func NewPattern

func NewPattern() Keyword

NewPattern allocates a new Pattern keyword

func NewPatternProperties

func NewPatternProperties() Keyword

NewPatternProperties allocates a new PatternProperties keyword

func NewProperties

func NewProperties() Keyword

NewProperties allocates a new Properties keyword

func NewPropertyNames

func NewPropertyNames() Keyword

NewPropertyNames allocates a new PropertyNames keyword

func NewReadOnly

func NewReadOnly() Keyword

NewReadOnly allocates a new ReadOnly keyword

func NewRecursiveAnchor

func NewRecursiveAnchor() Keyword

NewRecursiveAnchor allocates a new RecursiveAnchor keyword

func NewRecursiveRef

func NewRecursiveRef() Keyword

NewRecursiveRef allocates a new RecursiveRef keyword

func NewRef

func NewRef() Keyword

NewRef allocates a new Ref keyword

func NewRequired

func NewRequired() Keyword

NewRequired allocates a new Required keyword

func NewSchema

func NewSchema() Keyword

NewSchema allocates a new Schema Keyword/Validator

func NewSchemaURI

func NewSchemaURI() Keyword

NewSchemaURI allocates a new SchemaURI keyword

func NewThen

func NewThen() Keyword

NewThen allocates a new Then keyword

func NewTitle

func NewTitle() Keyword

NewTitle allocates a new Title keyword

func NewType

func NewType() Keyword

NewType allocates a new Type keyword

func NewUnevaluatedItems

func NewUnevaluatedItems() Keyword

NewUnevaluatedItems allocates a new UnevaluatedItems keyword

func NewUnevaluatedProperties

func NewUnevaluatedProperties() Keyword

NewUnevaluatedProperties allocates a new UnevaluatedProperties keyword

func NewUniqueItems

func NewUniqueItems() Keyword

NewUniqueItems allocates a new UniqueItems keyword

func NewVoid

func NewVoid() Keyword

NewVoid allocates a new Void keyword

func NewWriteOnly

func NewWriteOnly() Keyword

NewWriteOnly allocates a new WriteOnly keyword

type KeywordRegistry added in v1.0.0

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

KeywordRegistry contains a mapping of jsonschema keywords and their expected behavior.

func (*KeywordRegistry) Copy added in v1.0.0

func (r *KeywordRegistry) Copy() *KeywordRegistry

Copy creates a new KeywordRegistry populated with the same data.

func (*KeywordRegistry) DefaultIfEmpty added in v1.0.0

func (r *KeywordRegistry) DefaultIfEmpty()

DefaultIfEmpty populates the KeywordRegistry with the 2019_09 jsonschema draft specification only if the registry is empty.

func (*KeywordRegistry) GetKeyword added in v1.0.0

func (r *KeywordRegistry) GetKeyword(prop string) Keyword

GetKeyword returns a new instance of the keyword

func (*KeywordRegistry) GetKeywordInsertOrder added in v1.0.0

func (r *KeywordRegistry) GetKeywordInsertOrder(prop string) int

GetKeywordInsertOrder returns the insert index of the given keyword

func (*KeywordRegistry) GetKeywordOrder added in v1.0.0

func (r *KeywordRegistry) GetKeywordOrder(prop string) int

GetKeywordOrder returns the order index of the given keyword or defaults to 1

func (*KeywordRegistry) IsNotSupportedKeyword added in v1.0.0

func (r *KeywordRegistry) IsNotSupportedKeyword(prop string) bool

IsNotSupportedKeyword is a utility function to clarify when a given keyword, while expected is not supported

func (*KeywordRegistry) IsRegisteredKeyword added in v1.0.0

func (r *KeywordRegistry) IsRegisteredKeyword(prop string) bool

IsRegisteredKeyword validates if a given prop string is a registered keyword

func (*KeywordRegistry) IsRegistryLoaded added in v1.0.0

func (r *KeywordRegistry) IsRegistryLoaded() bool

IsRegistryLoaded checks if any keywords are present

func (*KeywordRegistry) LoadDraft2019_09 added in v1.0.0

func (r *KeywordRegistry) LoadDraft2019_09()

LoadDraft2019_09 loads the keywords for schema validation based on draft2019_09 this is also the default keyword set loaded automatically if no other is loaded

func (*KeywordRegistry) RegisterKeyword added in v1.0.0

func (r *KeywordRegistry) RegisterKeyword(prop string, maker KeyMaker)

RegisterKeyword registers a keyword with the registry

func (*KeywordRegistry) SetKeywordOrder added in v1.0.0

func (r *KeywordRegistry) SetKeywordOrder(prop string, order int)

SetKeywordOrder assigns a given order to a keyword

type LoaderRegistry added in v1.0.0

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

LoaderRegistry maintains a lookup table between uri schemes and associated loader

func GetSchemaLoaderRegistry added in v1.0.0

func GetSchemaLoaderRegistry() *LoaderRegistry

GetSchemaLoaderRegistry provides an accessor to a globally available (schema) loader registry

func NewLoaderRegistry added in v1.0.0

func NewLoaderRegistry() *LoaderRegistry

NewLoaderRegistry allocates a new schema loader registry

func (*LoaderRegistry) Get added in v1.0.0

func (r *LoaderRegistry) Get(scheme string) (SchemaLoaderFunc, bool)

Get the schema loader func for a specific URI Scheme

func (*LoaderRegistry) Register added in v1.0.0

func (r *LoaderRegistry) Register(scheme string, loader SchemaLoaderFunc)

Register a new schema loader for a specific URI Scheme

type MaxContains

type MaxContains int

MaxContains defines the maxContains JSON Schema keyword

func (*MaxContains) Register

func (m *MaxContains) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for MaxContains

func (*MaxContains) Resolve

func (m *MaxContains) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for MaxContains

func (MaxContains) ValidateKeyword

func (m MaxContains) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for MaxContains

type MaxItems

type MaxItems int

MaxItems defines the maxItems JSON Schema keyword

func (*MaxItems) Register

func (m *MaxItems) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for MaxItems

func (*MaxItems) Resolve

func (m *MaxItems) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for MaxItems

func (MaxItems) ValidateKeyword

func (m MaxItems) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for MaxItems

type MaxLength

type MaxLength int

MaxLength defines the maxLenght JSON Schema keyword

func (*MaxLength) Register

func (m *MaxLength) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for MaxLength

func (*MaxLength) Resolve

func (m *MaxLength) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for MaxLength

func (MaxLength) ValidateKeyword

func (m MaxLength) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for MaxLength

type MaxProperties

type MaxProperties int

MaxProperties defines the maxProperties JSON Schema keyword

func (*MaxProperties) Register

func (m *MaxProperties) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for MaxProperties

func (*MaxProperties) Resolve

func (m *MaxProperties) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for MaxProperties

func (MaxProperties) ValidateKeyword

func (m MaxProperties) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for MaxProperties

type Maximum

type Maximum float64

Maximum defines the maximum JSON Schema keyword

func (*Maximum) Register

func (m *Maximum) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Maximum

func (*Maximum) Resolve

func (m *Maximum) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Maximum

func (Maximum) ValidateKeyword

func (m Maximum) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Maximum

type MinContains

type MinContains int

MinContains defines the minContains JSON Schema keyword

func (*MinContains) Register

func (m *MinContains) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for MinContains

func (*MinContains) Resolve

func (m *MinContains) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for MinContains

func (MinContains) ValidateKeyword

func (m MinContains) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for MinContains

type MinItems

type MinItems int

MinItems defines the minItems JSON Schema keyword

func (*MinItems) Register

func (m *MinItems) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for MinItems

func (*MinItems) Resolve

func (m *MinItems) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for MinItems

func (MinItems) ValidateKeyword

func (m MinItems) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for MinItems

type MinLength

type MinLength int

MinLength defines the maxLenght JSON Schema keyword

func (*MinLength) Register

func (m *MinLength) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for MinLength

func (*MinLength) Resolve

func (m *MinLength) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for MinLength

func (MinLength) ValidateKeyword

func (m MinLength) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for MinLength

type MinProperties

type MinProperties int

MinProperties defines the minProperties JSON Schema keyword

func (*MinProperties) Register

func (m *MinProperties) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for MinProperties

func (*MinProperties) Resolve

func (m *MinProperties) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for MinProperties

func (MinProperties) ValidateKeyword

func (m MinProperties) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for MinProperties

type Minimum

type Minimum float64

Minimum defines the minimum JSON Schema keyword

func (*Minimum) Register

func (m *Minimum) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Minimum

func (*Minimum) Resolve

func (m *Minimum) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Minimum

func (Minimum) ValidateKeyword

func (m Minimum) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Minimum

type MultipleOf

type MultipleOf float64

MultipleOf defines the multipleOf JSON Schema keyword

func (*MultipleOf) Register

func (m *MultipleOf) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for MultipleOf

func (*MultipleOf) Resolve

func (m *MultipleOf) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for MultipleOf

func (MultipleOf) ValidateKeyword

func (m MultipleOf) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for MultipleOf

type Not

type Not Schema

Not defines the not JSON Schema keyword

func (Not) JSONChildren

func (n Not) JSONChildren() (res map[string]JSONPather)

JSONChildren implements the JSONContainer interface for Not

func (Not) JSONProp

func (n Not) JSONProp(name string) interface{}

JSONProp implements the JSONPather for Not

func (Not) MarshalJSON

func (n Not) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for Not

func (*Not) Register

func (n *Not) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Not

func (*Not) Resolve

func (n *Not) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Not

func (*Not) UnmarshalJSON

func (n *Not) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for Not

func (*Not) ValidateKeyword

func (n *Not) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Not

type OneOf

type OneOf []*Schema

OneOf defines the oneOf JSON Schema keyword

func (OneOf) JSONChildren

func (o OneOf) JSONChildren() (res map[string]JSONPather)

JSONChildren implements the JSONContainer interface for OneOf

func (OneOf) JSONProp

func (o OneOf) JSONProp(name string) interface{}

JSONProp implements the JSONPather for OneOf

func (*OneOf) Register

func (o *OneOf) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for OneOf

func (*OneOf) Resolve

func (o *OneOf) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for OneOf

func (*OneOf) ValidateKeyword

func (o *OneOf) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for OneOf

type Pattern

type Pattern regexp.Regexp

Pattern defines the pattern JSON Schema keyword

func (Pattern) MarshalJSON

func (p Pattern) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for Pattern

func (*Pattern) Register

func (p *Pattern) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Pattern

func (*Pattern) Resolve

func (p *Pattern) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Pattern

func (*Pattern) UnmarshalJSON

func (p *Pattern) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for Pattern

func (Pattern) ValidateKeyword

func (p Pattern) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Pattern

type PatternProperties

type PatternProperties []patternSchema

PatternProperties defines the patternProperties JSON Schema keyword

func (PatternProperties) JSONChildren

func (p PatternProperties) JSONChildren() (res map[string]JSONPather)

JSONChildren implements the JSONContainer interface for PatternProperties

func (PatternProperties) JSONProp

func (p PatternProperties) JSONProp(name string) interface{}

JSONProp implements the JSONPather for PatternProperties

func (PatternProperties) MarshalJSON

func (p PatternProperties) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for PatternProperties

func (*PatternProperties) Register

func (p *PatternProperties) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for PatternProperties

func (*PatternProperties) Resolve

func (p *PatternProperties) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for PatternProperties

func (*PatternProperties) UnmarshalJSON

func (p *PatternProperties) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for PatternProperties

func (PatternProperties) ValidateKeyword

func (p PatternProperties) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for PatternProperties

type Properties

type Properties map[string]*Schema

Properties defines the properties JSON Schema keyword

func (Properties) JSONChildren

func (p Properties) JSONChildren() (res map[string]JSONPather)

JSONChildren implements the JSONContainer interface for Properties

func (Properties) JSONProp

func (p Properties) JSONProp(name string) interface{}

JSONProp implements the JSONPather for Properties

func (*Properties) Register

func (p *Properties) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Properties

func (*Properties) Resolve

func (p *Properties) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Properties

func (Properties) ValidateKeyword

func (p Properties) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Properties

type PropertyDependency

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

PropertyDependency is the internal representation of a dependent property

func (PropertyDependency) JSONProp

func (p PropertyDependency) JSONProp(name string) interface{}

JSONProp implements the JSONPather for PropertyDependency

func (*PropertyDependency) Register

func (p *PropertyDependency) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for PropertyDependency

func (*PropertyDependency) Resolve

func (p *PropertyDependency) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for PropertyDependency

func (*PropertyDependency) ValidateKeyword

func (p *PropertyDependency) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for PropertyDependency

type PropertyNames

type PropertyNames Schema

PropertyNames defines the propertyNames JSON Schema keyword

func (PropertyNames) JSONChildren

func (p PropertyNames) JSONChildren() (res map[string]JSONPather)

JSONChildren implements the JSONContainer interface for PropertyNames

func (PropertyNames) JSONProp

func (p PropertyNames) JSONProp(name string) interface{}

JSONProp implements the JSONPather for PropertyNames

func (PropertyNames) MarshalJSON

func (p PropertyNames) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for PropertyNames

func (*PropertyNames) Register

func (p *PropertyNames) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for PropertyNames

func (*PropertyNames) Resolve

func (p *PropertyNames) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for PropertyNames

func (*PropertyNames) UnmarshalJSON

func (p *PropertyNames) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for PropertyNames

func (*PropertyNames) ValidateKeyword

func (p *PropertyNames) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for PropertyNames

type ReadOnly

type ReadOnly bool

ReadOnly defines the readOnly JSON Schema keyword

func (*ReadOnly) Register

func (r *ReadOnly) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for ReadOnly

func (*ReadOnly) Resolve

func (r *ReadOnly) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for ReadOnly

func (*ReadOnly) ValidateKeyword

func (r *ReadOnly) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for ReadOnly

type RecursiveAnchor

type RecursiveAnchor Schema

RecursiveAnchor defines the $recursiveAnchor JSON Schema keyword

func (*RecursiveAnchor) Register

func (r *RecursiveAnchor) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for RecursiveAnchor

func (*RecursiveAnchor) Resolve

func (r *RecursiveAnchor) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for RecursiveAnchor

func (*RecursiveAnchor) UnmarshalJSON

func (r *RecursiveAnchor) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for RecursiveAnchor

func (*RecursiveAnchor) ValidateKeyword

func (r *RecursiveAnchor) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for RecursiveAnchor

type RecursiveRef

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

RecursiveRef defines the $recursiveRef JSON Schema keyword

func (RecursiveRef) MarshalJSON

func (r RecursiveRef) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for RecursiveRef

func (*RecursiveRef) Register

func (r *RecursiveRef) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for RecursiveRef

func (*RecursiveRef) Resolve

func (r *RecursiveRef) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for RecursiveRef

func (*RecursiveRef) UnmarshalJSON

func (r *RecursiveRef) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for RecursiveRef

func (*RecursiveRef) ValidateKeyword

func (r *RecursiveRef) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for RecursiveRef

type Ref

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

Ref defines the $ref JSON Schema keyword

func (Ref) MarshalJSON

func (r Ref) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for Ref

func (*Ref) Register

func (r *Ref) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Ref

func (*Ref) Resolve

func (r *Ref) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Ref

func (*Ref) UnmarshalJSON

func (r *Ref) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for Ref

func (*Ref) ValidateKeyword

func (r *Ref) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Ref

type Required

type Required []string

Required defines the required JSON Schema keyword

func (Required) JSONProp

func (r Required) JSONProp(name string) interface{}

JSONProp implements the JSONPather for Required

func (*Required) Register

func (r *Required) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Required

func (*Required) Resolve

func (r *Required) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Required

func (Required) ValidateKeyword

func (r Required) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Required

type Schema

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

Schema is the top-level structure defining a json schema

func Must

func Must(jsonString string) *Schema

Must turns a JSON string into a *Schema, panicing if parsing fails. Useful for declaring Schemas in Go code.

func (*Schema) HasKeyword

func (s *Schema) HasKeyword(key string) bool

HasKeyword is a utility function for checking if the given schema has an instance of the required keyword

func (Schema) JSONChildren

func (s Schema) JSONChildren() map[string]JSONPather

JSONChildren implements the JSONContainer interface for Schema

func (Schema) JSONProp

func (s Schema) JSONProp(name string) interface{}

JSONProp implements the JSONPather for Schema

func (Schema) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface for Schema

func (*Schema) Register

func (s *Schema) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Schema

func (*Schema) Resolve

func (s *Schema) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Schema

func (*Schema) TopLevelType

func (s *Schema) TopLevelType() string

TopLevelType returns a string representing the schema's top-level type.

func (*Schema) UnmarshalJSON

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

UnmarshalJSON implements the json.Unmarshaler interface for Schema

func (*Schema) Validate

func (s *Schema) Validate(ctx context.Context, data interface{}) *ValidationState

Validate initiates a fresh validation state and triggers the evaluation

func (*Schema) ValidateBytes

func (s *Schema) ValidateBytes(ctx context.Context, data []byte) ([]KeyError, error)

ValidateBytes performs schema validation against a slice of json byte data

func (*Schema) ValidateKeyword

func (s *Schema) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword uses the schema to check an instance, collecting validation errors in a slice

type SchemaDependency

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

SchemaDependency is the internal representation of a dependent schema

func (SchemaDependency) JSONProp

func (d SchemaDependency) JSONProp(name string) interface{}

JSONProp implements the JSONPather for SchemaDependency

func (SchemaDependency) MarshalJSON

func (d SchemaDependency) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for SchemaDependency

func (*SchemaDependency) Register

func (d *SchemaDependency) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for SchemaDependency

func (*SchemaDependency) Resolve

func (d *SchemaDependency) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for SchemaDependency

func (*SchemaDependency) ValidateKeyword

func (d *SchemaDependency) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for SchemaDependency

type SchemaLoaderFunc added in v1.0.0

type SchemaLoaderFunc func(ctx context.Context, uri *url.URL, schema *Schema) error

SchemaLoaderFunc is a function that loads a schema for a specific URI Scheme

type SchemaRegistry

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

SchemaRegistry maintains a lookup table between schema string references and actual schemas

func GetSchemaRegistry

func GetSchemaRegistry() *SchemaRegistry

GetSchemaRegistry provides an accessor to a globally available schema registry

func (*SchemaRegistry) Get

func (sr *SchemaRegistry) Get(ctx context.Context, uri string) *Schema

Get fetches a schema from the top level context registry or fetches it from a remote

func (*SchemaRegistry) GetKnown

func (sr *SchemaRegistry) GetKnown(uri string) *Schema

GetKnown fetches a schema from the top level context registry

func (*SchemaRegistry) GetLocal

func (sr *SchemaRegistry) GetLocal(uri string) *Schema

GetLocal fetches a schema from the local context registry

func (*SchemaRegistry) Register

func (sr *SchemaRegistry) Register(sch *Schema)

Register registers a schema to the top level context

func (*SchemaRegistry) RegisterLocal

func (sr *SchemaRegistry) RegisterLocal(sch *Schema)

RegisterLocal registers a schema to a local context

type SchemaURI

type SchemaURI string

SchemaURI defines the $schema JSON Schema keyword

func (*SchemaURI) Register

func (s *SchemaURI) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for SchemaURI

func (*SchemaURI) Resolve

func (s *SchemaURI) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for SchemaURI

func (*SchemaURI) ValidateKeyword

func (s *SchemaURI) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for SchemaURI

type Then

type Then Schema

Then defines the then JSON Schema keyword

func (Then) JSONChildren

func (t Then) JSONChildren() (res map[string]JSONPather)

JSONChildren implements the JSONContainer interface for Then

func (Then) JSONProp

func (t Then) JSONProp(name string) interface{}

JSONProp implements the JSONPather for Then

func (Then) MarshalJSON

func (t Then) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for Then

func (*Then) Register

func (t *Then) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Then

func (*Then) Resolve

func (t *Then) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Then

func (*Then) UnmarshalJSON

func (t *Then) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for Then

func (*Then) ValidateKeyword

func (t *Then) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Then

type Title

type Title string

Title defines the title JSON Schema keyword

func (*Title) Register

func (t *Title) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Title

func (*Title) Resolve

func (t *Title) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Title

func (*Title) ValidateKeyword

func (t *Title) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Title

type Type

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

Type defines the type JSON Schema keyword

func (Type) JSONProp

func (t Type) JSONProp(name string) interface{}

JSONProp implements the JSONPather for Type

func (Type) MarshalJSON

func (t Type) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for Type

func (*Type) Register

func (t *Type) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Type

func (*Type) Resolve

func (t *Type) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Type

func (Type) String

func (t Type) String() string

String implements the Stringer for Type

func (*Type) UnmarshalJSON

func (t *Type) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for Type

func (Type) ValidateKeyword

func (t Type) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Type

type UnevaluatedItems

type UnevaluatedItems Schema

UnevaluatedItems defines the unevaluatedItems JSON Schema keyword

func (*UnevaluatedItems) Register

func (ui *UnevaluatedItems) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for UnevaluatedItems

func (*UnevaluatedItems) Resolve

func (ui *UnevaluatedItems) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for UnevaluatedItems

func (*UnevaluatedItems) UnmarshalJSON

func (ui *UnevaluatedItems) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for UnevaluatedItems

func (*UnevaluatedItems) ValidateKeyword

func (ui *UnevaluatedItems) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for UnevaluatedItems

type UnevaluatedProperties

type UnevaluatedProperties Schema

UnevaluatedProperties defines the unevaluatedProperties JSON Schema keyword

func (*UnevaluatedProperties) Register

func (up *UnevaluatedProperties) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for UnevaluatedProperties

func (*UnevaluatedProperties) Resolve

func (up *UnevaluatedProperties) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for UnevaluatedProperties

func (*UnevaluatedProperties) UnmarshalJSON

func (up *UnevaluatedProperties) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for UnevaluatedProperties

func (*UnevaluatedProperties) ValidateKeyword

func (up *UnevaluatedProperties) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for UnevaluatedProperties

type UniqueItems

type UniqueItems bool

UniqueItems defines the uniqueItems JSON Schema keyword

func (*UniqueItems) Register

func (u *UniqueItems) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for UniqueItems

func (*UniqueItems) Resolve

func (u *UniqueItems) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for UniqueItems

func (UniqueItems) ValidateKeyword

func (u UniqueItems) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for UniqueItems

type ValidationState

type ValidationState struct {
	Local                *Schema
	Root                 *Schema
	RecursiveAnchor      *Schema
	BaseURI              string
	InstanceLocation     *jptr.Pointer
	RelativeLocation     *jptr.Pointer
	BaseRelativeLocation *jptr.Pointer

	LocalRegistry        *SchemaRegistry
	LocalKeywordRegistry *KeywordRegistry

	EvaluatedPropertyNames      *map[string]bool
	LocalEvaluatedPropertyNames *map[string]bool
	LastEvaluatedIndex          int
	LocalLastEvaluatedIndex     int
	Misc                        map[string]interface{}

	Errs *[]KeyError
}

ValidationState holds the schema validation state The aim is to have one global validation state and use local sub states when evaluating parallel branches TODO(arqu): make sure this is safe for concurrent use

func NewValidationState

func NewValidationState(s *Schema) *ValidationState

NewValidationState creates a new ValidationState with the provided location pointers and data instance

func (*ValidationState) AddError

func (vs *ValidationState) AddError(data interface{}, msg string)

AddError creates and appends a KeyError to errs of the current state

func (*ValidationState) AddSubErrors

func (vs *ValidationState) AddSubErrors(errs ...KeyError)

AddSubErrors appends a list of KeyError to the current state

func (*ValidationState) ClearState

func (vs *ValidationState) ClearState()

ClearState resets a schema to it's core elements

func (*ValidationState) DescendBase

func (vs *ValidationState) DescendBase(token ...string)

DescendBase descends the base relative pointer relative to itself

func (*ValidationState) DescendBaseFromState

func (vs *ValidationState) DescendBaseFromState(base *ValidationState, token ...string)

DescendBaseFromState descends the base relative pointer relative to the provided state

func (*ValidationState) DescendInstance

func (vs *ValidationState) DescendInstance(token ...string)

DescendInstance descends the instance pointer relative to itself

func (*ValidationState) DescendInstanceFromState

func (vs *ValidationState) DescendInstanceFromState(base *ValidationState, token ...string)

DescendInstanceFromState descends the instance pointer relative to the provided state

func (*ValidationState) DescendRelative

func (vs *ValidationState) DescendRelative(token ...string)

DescendRelative descends the relative pointer relative to itself

func (*ValidationState) DescendRelativeFromState

func (vs *ValidationState) DescendRelativeFromState(base *ValidationState, token ...string)

DescendRelativeFromState descends the relative pointer relative to the provided state

func (*ValidationState) IsEvaluatedKey

func (vs *ValidationState) IsEvaluatedKey(key string) bool

IsEvaluatedKey checks if the key is evaluated against the state context

func (*ValidationState) IsLocallyEvaluatedKey

func (vs *ValidationState) IsLocallyEvaluatedKey(key string) bool

IsLocallyEvaluatedKey checks if the key is evaluated against the local state context

func (*ValidationState) IsValid

func (vs *ValidationState) IsValid() bool

IsValid returns if the current state is valid

func (*ValidationState) NewSubState

func (vs *ValidationState) NewSubState() *ValidationState

NewSubState creates a new ValidationState from an existing ValidationState

func (*ValidationState) SetEvaluatedIndex

func (vs *ValidationState) SetEvaluatedIndex(i int)

SetEvaluatedIndex sets the evaluation index for the current state

func (*ValidationState) SetEvaluatedKey

func (vs *ValidationState) SetEvaluatedKey(key string)

SetEvaluatedKey updates the evaluation properties of the current state

func (*ValidationState) UpdateEvaluatedPropsAndItems

func (vs *ValidationState) UpdateEvaluatedPropsAndItems(subState *ValidationState)

UpdateEvaluatedPropsAndItems is a utility function to join evaluated properties and set the current evaluation position index

type Void

type Void struct{}

Void is a placeholder definition for a keyword

func (*Void) Register

func (vo *Void) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for Void

func (*Void) Resolve

func (vo *Void) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for Void

func (*Void) ValidateKeyword

func (vo *Void) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for Void

type WriteOnly

type WriteOnly bool

WriteOnly defines the writeOnly JSON Schema keyword

func (*WriteOnly) Register

func (w *WriteOnly) Register(uri string, registry *SchemaRegistry)

Register implements the Keyword interface for WriteOnly

func (*WriteOnly) Resolve

func (w *WriteOnly) Resolve(pointer jptr.Pointer, uri string) *Schema

Resolve implements the Keyword interface for WriteOnly

func (*WriteOnly) ValidateKeyword

func (w *WriteOnly) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{})

ValidateKeyword implements the Keyword interface for WriteOnly

Jump to

Keyboard shortcuts

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