jsonschema

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2021 License: MIT Imports: 18 Imported by: 113

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 added in v0.2.0

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 added in v0.2.0

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

FetchSchema downloads and loads a schema from a remote location

func GetKeywordInsertOrder added in v0.2.0

func GetKeywordInsertOrder(prop string) int

GetKeywordInsertOrder returns the insert index of the given keyword

func GetKeywordOrder added in v0.2.0

func GetKeywordOrder(prop string) int

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

func InvalidValueString

func InvalidValueString(data interface{}) string

InvalidValueString returns the errored value as a string

func IsLocalSchemaID added in v0.2.0

func IsLocalSchemaID(id string) bool

IsLocalSchemaID validates if a given id is a local id

func IsNotSupportedKeyword added in v0.2.0

func IsNotSupportedKeyword(prop string) bool

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

func IsRegisteredKeyword added in v0.2.0

func IsRegisteredKeyword(prop string) bool

IsRegisteredKeyword validates if a given prop string is a registered keyword

func IsRegistryLoaded added in v0.2.0

func IsRegistryLoaded() bool

IsRegistryLoaded checks if any keywords are present

func LoadDraft2019_09 added in v0.2.0

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 added in v0.2.0

func RegisterKeyword(prop string, maker KeyMaker)

RegisterKeyword registers a keyword with the registry

func ResetSchemaRegistry added in v0.2.0

func ResetSchemaRegistry()

ResetSchemaRegistry resets the main SchemaRegistry

func SafeResolveURL added in v0.2.0

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

SafeResolveURL resolves a string url against the current context url

func SetKeywordOrder added in v0.2.0

func SetKeywordOrder(prop string, order int)

SetKeywordOrder assignes a given order to a keyword

Types

type AdditionalItems

type AdditionalItems Schema

AdditionalItems defines the additionalItems JSON Schema keyword

func (*AdditionalItems) Register added in v0.2.0

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

Register implements the Keyword interface for AdditionalItems

func (*AdditionalItems) Resolve added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for AdditionalProperties

func (*AdditionalProperties) Resolve added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for AllOf

func (*AllOf) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for AllOf

func (*AllOf) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for AllOf

type Anchor added in v0.2.0

type Anchor string

Anchor defines the $anchor JSON Schema keyword

func (*Anchor) Register added in v0.2.0

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

Register implements the Keyword interface for Anchor

func (*Anchor) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for Anchor

func (*Anchor) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for AnyOf

func (*AnyOf) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for AnyOf

func (*AnyOf) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for AnyOf

type Comment added in v0.2.0

type Comment string

Comment defines the comment JSON Schema keyword

func (*Comment) Register added in v0.2.0

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

Register implements the Keyword interface for Comment

func (*Comment) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for Comment

func (*Comment) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for Const

func (*Const) Resolve added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for Contains

func (*Contains) Resolve added in v0.2.0

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 added in v0.2.0

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

ValidateKeyword implements the Keyword interface for Contains

type Default added in v0.2.0

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

Default defines the default JSON Schema keyword

func (*Default) Register added in v0.2.0

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

Register implements the Keyword interface for Default

func (*Default) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for Default

func (*Default) UnmarshalJSON added in v0.2.0

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

UnmarshalJSON implements the json.Unmarshaler interface for Default

func (*Default) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for Default

type Defs added in v0.2.0

type Defs map[string]*Schema

Defs defines the $defs JSON Schema keyword

func (Defs) JSONChildren added in v0.2.0

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

JSONChildren implements the JSONContainer interface for Defs

func (Defs) JSONProp added in v0.2.0

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

JSONProp implements the JSONPather for Defs

func (*Defs) Register added in v0.2.0

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

Register implements the Keyword interface for Defs

func (*Defs) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for Defs

func (Defs) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for Defs

type DependentRequired added in v0.2.0

type DependentRequired map[string]PropertyDependency

DependentRequired defines the dependentRequired JSON Schema keyword

func (DependentRequired) JSONChildren added in v0.2.0

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

JSONChildren implements the JSONContainer interface for DependentRequired

func (DependentRequired) JSONProp added in v0.2.0

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

JSONProp implements the JSONPather for DependentRequired

func (DependentRequired) MarshalJSON added in v0.2.0

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

MarshalJSON implements the json.Marshaler interface for DependentRequired

func (*DependentRequired) Register added in v0.2.0

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

Register implements the Keyword interface for DependentRequired

func (*DependentRequired) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for DependentRequired

func (*DependentRequired) UnmarshalJSON added in v0.2.0

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

UnmarshalJSON implements the json.Unmarshaler interface for DependentRequired

func (*DependentRequired) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for DependentRequired

type DependentSchemas added in v0.2.0

type DependentSchemas map[string]SchemaDependency

DependentSchemas defines the dependentSchemas JSON Schema keyword

func (DependentSchemas) JSONChildren added in v0.2.0

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

JSONChildren implements the JSONContainer interface for DependentSchemas

func (DependentSchemas) JSONProp added in v0.2.0

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

JSONProp implements the JSONPather for DependentSchemas

func (*DependentSchemas) Register added in v0.2.0

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

Register implements the Keyword interface for DependentSchemas

func (*DependentSchemas) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for DependentSchemas

func (*DependentSchemas) UnmarshalJSON added in v0.2.0

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

UnmarshalJSON implements the json.Unmarshaler interface for DependentSchemas

func (*DependentSchemas) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for DependentSchemas

type Description added in v0.2.0

type Description string

Description defines the description JSON Schema keyword

func (*Description) Register added in v0.2.0

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

Register implements the Keyword interface for Description

func (*Description) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for Description

func (*Description) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for Else

func (*Else) Resolve added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for Enum

func (*Enum) Resolve added in v0.2.0

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 added in v0.2.0

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

ValidateKeyword implements the Keyword interface for Enum

type Examples added in v0.2.0

type Examples []interface{}

Examples defines the examples JSON Schema keyword

func (*Examples) Register added in v0.2.0

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

Register implements the Keyword interface for Examples

func (*Examples) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for Examples

func (*Examples) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for ExclusiveMaximum

func (*ExclusiveMaximum) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for ExclusiveMaximum

func (ExclusiveMaximum) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for ExclusiveMinimum

func (*ExclusiveMinimum) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for ExclusiveMinimum

func (ExclusiveMinimum) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for Format

func (*Format) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for Format

func (Format) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for Format

type ID added in v0.2.0

type ID string

ID defines the $id JSON Schema keyword

func (*ID) Register added in v0.2.0

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

Register implements the Keyword interface for ID

func (*ID) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for ID

func (*ID) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for If

func (*If) Resolve added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for Items

func (*Items) Resolve added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

func (v KeyError) Error() string

Error implements the error interface for KeyError

type KeyMaker added in v0.2.0

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 added in v0.2.0

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 GetKeyword added in v0.2.0

func GetKeyword(prop string) Keyword

GetKeyword returns a new instance of the 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 added in v0.2.0

func NewAnchor() Keyword

NewAnchor allocates a new Anchor keyword

func NewAnyOf

func NewAnyOf() Keyword

NewAnyOf allocates a new AnyOf keyword

func NewComment added in v0.2.0

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 added in v0.2.0

func NewDefault() Keyword

NewDefault allocates a new Default keyword

func NewDefs added in v0.2.0

func NewDefs() Keyword

NewDefs allocates a new Defs keyword

func NewDependentRequired added in v0.2.0

func NewDependentRequired() Keyword

NewDependentRequired allocates a new DependentRequired keyword

func NewDependentSchemas added in v0.2.0

func NewDependentSchemas() Keyword

NewDependentSchemas allocates a new DependentSchemas keyword

func NewDescription added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

func NewReadOnly() Keyword

NewReadOnly allocates a new ReadOnly keyword

func NewRecursiveAnchor added in v0.2.0

func NewRecursiveAnchor() Keyword

NewRecursiveAnchor allocates a new RecursiveAnchor keyword

func NewRecursiveRef added in v0.2.0

func NewRecursiveRef() Keyword

NewRecursiveRef allocates a new RecursiveRef keyword

func NewRef added in v0.2.0

func NewRef() Keyword

NewRef allocates a new Ref keyword

func NewRequired

func NewRequired() Keyword

NewRequired allocates a new Required keyword

func NewSchema added in v0.2.0

func NewSchema() Keyword

NewSchema allocates a new Schema Keyword/Validator

func NewSchemaURI added in v0.2.0

func NewSchemaURI() Keyword

NewSchemaURI allocates a new SchemaURI keyword

func NewThen

func NewThen() Keyword

NewThen allocates a new Then keyword

func NewTitle added in v0.2.0

func NewTitle() Keyword

NewTitle allocates a new Title keyword

func NewType

func NewType() Keyword

NewType allocates a new Type keyword

func NewUnevaluatedItems added in v0.2.0

func NewUnevaluatedItems() Keyword

NewUnevaluatedItems allocates a new UnevaluatedItems keyword

func NewUnevaluatedProperties added in v0.2.0

func NewUnevaluatedProperties() Keyword

NewUnevaluatedProperties allocates a new UnevaluatedProperties keyword

func NewUniqueItems

func NewUniqueItems() Keyword

NewUniqueItems allocates a new UniqueItems keyword

func NewVoid added in v0.2.0

func NewVoid() Keyword

NewVoid allocates a new Void keyword

func NewWriteOnly added in v0.2.0

func NewWriteOnly() Keyword

NewWriteOnly allocates a new WriteOnly keyword

type MaxContains added in v0.2.0

type MaxContains int

MaxContains defines the maxContains JSON Schema keyword

func (*MaxContains) Register added in v0.2.0

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

Register implements the Keyword interface for MaxContains

func (*MaxContains) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for MaxContains

func (MaxContains) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for MaxItems

func (*MaxItems) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for MaxItems

func (MaxItems) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for MaxLength

func (*MaxLength) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for MaxLength

func (MaxLength) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for MaxProperties

func (*MaxProperties) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for MaxProperties

func (MaxProperties) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for Maximum

func (*Maximum) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for Maximum

func (Maximum) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for Maximum

type MinContains added in v0.2.0

type MinContains int

MinContains defines the minContains JSON Schema keyword

func (*MinContains) Register added in v0.2.0

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

Register implements the Keyword interface for MinContains

func (*MinContains) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for MinContains

func (MinContains) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for MinItems

func (*MinItems) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for MinItems

func (MinItems) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for MinLength

func (*MinLength) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for MinLength

func (MinLength) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for MinLength

type MinProperties added in v0.2.0

type MinProperties int

MinProperties defines the minProperties JSON Schema keyword

func (*MinProperties) Register added in v0.2.0

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

Register implements the Keyword interface for MinProperties

func (*MinProperties) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for MinProperties

func (MinProperties) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for Minimum

func (*Minimum) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for Minimum

func (Minimum) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for MultipleOf

func (*MultipleOf) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for MultipleOf

func (MultipleOf) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for Not

func (*Not) Resolve added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for OneOf

func (*OneOf) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for OneOf

func (*OneOf) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for Pattern

func (*Pattern) Resolve added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for PatternProperties

func (*PatternProperties) Resolve added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for Properties

func (*Properties) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for Properties

func (Properties) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for Properties

type PropertyDependency added in v0.2.0

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

PropertyDependency is the internal representation of a dependent property

func (PropertyDependency) JSONProp added in v0.2.0

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

JSONProp implements the JSONPather for PropertyDependency

func (*PropertyDependency) Register added in v0.2.0

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

Register implements the Keyword interface for PropertyDependency

func (*PropertyDependency) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for PropertyDependency

func (*PropertyDependency) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for PropertyNames

func (*PropertyNames) Resolve added in v0.2.0

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 added in v0.2.0

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

ValidateKeyword implements the Keyword interface for PropertyNames

type ReadOnly added in v0.2.0

type ReadOnly bool

ReadOnly defines the readOnly JSON Schema keyword

func (*ReadOnly) Register added in v0.2.0

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

Register implements the Keyword interface for ReadOnly

func (*ReadOnly) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for ReadOnly

func (*ReadOnly) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for ReadOnly

type RecursiveAnchor added in v0.2.0

type RecursiveAnchor Schema

RecursiveAnchor defines the $recursiveAnchor JSON Schema keyword

func (*RecursiveAnchor) Register added in v0.2.0

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

Register implements the Keyword interface for RecursiveAnchor

func (*RecursiveAnchor) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for RecursiveAnchor

func (*RecursiveAnchor) UnmarshalJSON added in v0.2.0

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

UnmarshalJSON implements the json.Unmarshaler interface for RecursiveAnchor

func (*RecursiveAnchor) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for RecursiveAnchor

type RecursiveRef added in v0.2.0

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

RecursiveRef defines the $recursiveRef JSON Schema keyword

func (RecursiveRef) MarshalJSON added in v0.2.0

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

MarshalJSON implements the json.Marshaler interface for RecursiveRef

func (*RecursiveRef) Register added in v0.2.0

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

Register implements the Keyword interface for RecursiveRef

func (*RecursiveRef) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for RecursiveRef

func (*RecursiveRef) UnmarshalJSON added in v0.2.0

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

UnmarshalJSON implements the json.Unmarshaler interface for RecursiveRef

func (*RecursiveRef) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for RecursiveRef

type Ref added in v0.2.0

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

Ref defines the $ref JSON Schema keyword

func (Ref) MarshalJSON added in v0.2.0

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

MarshalJSON implements the json.Marshaler interface for Ref

func (*Ref) Register added in v0.2.0

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

Register implements the Keyword interface for Ref

func (*Ref) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for Ref

func (*Ref) UnmarshalJSON added in v0.2.0

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

UnmarshalJSON implements the json.Unmarshaler interface for Ref

func (*Ref) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for Required

func (*Required) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for Required

func (Required) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for Schema

func (*Schema) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for Schema

func (*Schema) TopLevelType added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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 added in v0.2.0

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

SchemaDependency is the internal representation of a dependent schema

func (SchemaDependency) JSONProp added in v0.2.0

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

JSONProp implements the JSONPather for SchemaDependency

func (SchemaDependency) MarshalJSON added in v0.2.0

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

MarshalJSON implements the json.Marshaler interface for SchemaDependency

func (*SchemaDependency) Register added in v0.2.0

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

Register implements the Keyword interface for SchemaDependency

func (*SchemaDependency) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for SchemaDependency

func (*SchemaDependency) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for SchemaDependency

type SchemaRegistry added in v0.2.0

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

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

func GetSchemaRegistry added in v0.2.0

func GetSchemaRegistry() *SchemaRegistry

GetSchemaRegistry provides an accessor to a globally available schema registry

func (*SchemaRegistry) Get added in v0.2.0

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 added in v0.2.0

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

GetKnown fetches a schema from the top level context registry

func (*SchemaRegistry) GetLocal added in v0.2.0

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

GetLocal fetches a schema from the local context registry

func (*SchemaRegistry) Register added in v0.2.0

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

Register registers a schema to the top level context

func (*SchemaRegistry) RegisterLocal added in v0.2.0

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

RegisterLocal registers a schema to a local context

type SchemaURI added in v0.2.0

type SchemaURI string

SchemaURI defines the $schema JSON Schema keyword

func (*SchemaURI) Register added in v0.2.0

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

Register implements the Keyword interface for SchemaURI

func (*SchemaURI) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for SchemaURI

func (*SchemaURI) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for Then

func (*Then) Resolve added in v0.2.0

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 added in v0.2.0

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

ValidateKeyword implements the Keyword interface for Then

type Title added in v0.2.0

type Title string

Title defines the title JSON Schema keyword

func (*Title) Register added in v0.2.0

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

Register implements the Keyword interface for Title

func (*Title) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for Title

func (*Title) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for Type

func (*Type) Resolve added in v0.2.0

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 added in v0.2.0

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

ValidateKeyword implements the Keyword interface for Type

type UnevaluatedItems added in v0.2.0

type UnevaluatedItems Schema

UnevaluatedItems defines the unevaluatedItems JSON Schema keyword

func (*UnevaluatedItems) Register added in v0.2.0

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

Register implements the Keyword interface for UnevaluatedItems

func (*UnevaluatedItems) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for UnevaluatedItems

func (*UnevaluatedItems) UnmarshalJSON added in v0.2.0

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

UnmarshalJSON implements the json.Unmarshaler interface for UnevaluatedItems

func (*UnevaluatedItems) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for UnevaluatedItems

type UnevaluatedProperties added in v0.2.0

type UnevaluatedProperties Schema

UnevaluatedProperties defines the unevaluatedProperties JSON Schema keyword

func (*UnevaluatedProperties) Register added in v0.2.0

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

Register implements the Keyword interface for UnevaluatedProperties

func (*UnevaluatedProperties) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for UnevaluatedProperties

func (*UnevaluatedProperties) UnmarshalJSON added in v0.2.0

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

UnmarshalJSON implements the json.Unmarshaler interface for UnevaluatedProperties

func (*UnevaluatedProperties) ValidateKeyword added in v0.2.0

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 added in v0.2.0

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

Register implements the Keyword interface for UniqueItems

func (*UniqueItems) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for UniqueItems

func (UniqueItems) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for UniqueItems

type ValidationState added in v0.2.0

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

	LocalRegistry *SchemaRegistry

	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 added in v0.2.0

func NewValidationState(s *Schema) *ValidationState

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

func (*ValidationState) AddError added in v0.2.0

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

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

func (*ValidationState) AddSubErrors added in v0.2.0

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

AddSubErrors appends a list of KeyError to the current state

func (*ValidationState) ClearState added in v0.2.0

func (vs *ValidationState) ClearState()

ClearState resets a schema to it's core elements

func (*ValidationState) DescendBase added in v0.2.0

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

DescendBase descends the base relative pointer relative to itself

func (*ValidationState) DescendBaseFromState added in v0.2.0

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

DescendBaseFromState descends the base relative pointer relative to the provided state

func (*ValidationState) DescendInstance added in v0.2.0

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

DescendInstance descends the instance pointer relative to itself

func (*ValidationState) DescendInstanceFromState added in v0.2.0

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

DescendInstanceFromState descends the instance pointer relative to the provided state

func (*ValidationState) DescendRelative added in v0.2.0

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

DescendRelative descends the relative pointer relative to itself

func (*ValidationState) DescendRelativeFromState added in v0.2.0

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

DescendRelativeFromState descends the relative pointer relative to the provided state

func (*ValidationState) IsEvaluatedKey added in v0.2.0

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

IsEvaluatedKey checks if the key is evaluated against the state context

func (*ValidationState) IsLocallyEvaluatedKey added in v0.2.0

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

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

func (*ValidationState) IsValid added in v0.2.0

func (vs *ValidationState) IsValid() bool

IsValid returns if the current state is valid

func (*ValidationState) NewSubState added in v0.2.0

func (vs *ValidationState) NewSubState() *ValidationState

NewSubState creates a new ValidationState from an existing ValidationState

func (*ValidationState) SetEvaluatedIndex added in v0.2.0

func (vs *ValidationState) SetEvaluatedIndex(i int)

SetEvaluatedIndex sets the evaluation index for the current state

func (*ValidationState) SetEvaluatedKey added in v0.2.0

func (vs *ValidationState) SetEvaluatedKey(key string)

SetEvaluatedKey updates the evaluation properties of the current state

func (*ValidationState) UpdateEvaluatedPropsAndItems added in v0.2.0

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

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

type Void added in v0.2.0

type Void struct{}

Void is a placeholder definition for a keyword

func (*Void) Register added in v0.2.0

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

Register implements the Keyword interface for Void

func (*Void) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for Void

func (*Void) ValidateKeyword added in v0.2.0

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

ValidateKeyword implements the Keyword interface for Void

type WriteOnly added in v0.2.0

type WriteOnly bool

WriteOnly defines the writeOnly JSON Schema keyword

func (*WriteOnly) Register added in v0.2.0

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

Register implements the Keyword interface for WriteOnly

func (*WriteOnly) Resolve added in v0.2.0

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

Resolve implements the Keyword interface for WriteOnly

func (*WriteOnly) ValidateKeyword added in v0.2.0

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