jsonschema

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: May 29, 2019 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package jsonschema includes tools for walking JSON schema files and running a custom function for each instance.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewValidator

func NewValidator(schemaPath string) (*validator, error)

NewValidator returns a Validator for the schema at the given file path. The Validate method on the Validator allows for verifying a JSON file matches the JSON schema.

func SchemaTypes

func SchemaTypes(schemaPath string) ([]string, []string, error)

SchemaTypes will parse the given file and report which top level allOfTypes and oneOfTypes are found in that schema.

func Walk

func Walk(s *Schema, walkFn WalkInstanceFunc) error

Walk runs each instance in the JSON schema through the defined walk function, keeping track of the JSONPath. It assumes the JSON schema is valid and does not check for many errors such as bad property names. For instances that are objects or arrays the WalkFunc will be called for each child instance.

func WalkRaw

func WalkRaw(s *Schema, walkFn WalkRawFunc) error

WalkRaw works nearly identical to the Walk function but rather than calling a WalkFunc calls a WalkRawFunc. By skipping the JSON unmarshaling of the Instance it runs nearly 10 times faster then WalkFunc.

Types

type Instance

type Instance struct {
	Ref                  string                     `json:"$ref,omitempty"`
	Schema               string                     `json:"$schema,omitempty"`
	AdditionalProperties bool                       `json:"additionalProperties,omitempty"`
	Description          string                     `json:"description,omitempty"`
	Type                 string                     `json:"type"`
	Format               string                     `json:"format,omitempty"`
	Items                json.RawMessage            `json:"items,omitempty"`
	Properties           map[string]json.RawMessage `json:"properties,omitempty"`
	AllOf                []Instance                 `json:"allOf,omitempty"`
	OneOf                []Instance                 `json:"oneOf,omitempty"`
	Required             []string                   `json:"Required,omitempty"`
}

Instance represents a JSON Schema instance.

type Schema

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

Schema represents a JSON Schema with the AllOf and OneOf references parsed and squashed into a single representation. This is not a fully spec compatible representation but a basic representation useful for walking through the schema instances within a schema.

A fully spec compatible version of the schema is kept for validation purposes.

func SchemaFromFile

func SchemaFromFile(schemaPath string, oneOfType string) (*Schema, error)

SchemaFromFile parses a file at the given path and returns a schema based on its contents. The function traverses top level allOf fields within the schema. For oneOf fields the reference base name minus any extension is compared to the value of the oneOfType argument and if they match that file is also traversed.

Only file references are supported.

Note: A top level array will use the items from the first file to define them.

func (*Schema) Validate

func (s *Schema) Validate(raw json.RawMessage) (bool, error)

Validate will check that the given json is validate according the schema.

type Validator

type Validator interface {
	Validate(raw json.RawMessage) (bool, error)
}

Validator defines an interface for validating JSON matches a JSON schema.

type WalkInstanceFunc

type WalkInstanceFunc func(path string, i Instance) error

WalkFunc processes a single Instance within a JSON schema file returning an error on any problems. The path corresponds to the JSONPath (http://goessner.net/articles/JsonPath/) of the instance within the JSON format described by the JSON Schema.

type WalkRawFunc

type WalkRawFunc func(path string, value json.RawMessage) error

WalkRawFunc works similar to WalkFunc but rather than accepting an instance it accepts the raw JSON for each level of the schema.

Jump to

Keyboard shortcuts

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