jsonschema

package
v1.6.3 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2024 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 FieldType added in v1.3.1

func FieldType(data []byte) (string, bool, error)

FieldType returns the type name of a field and whether the field is nullable.

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, []string, error)

SchemaTypes will parse the given file and report which top level allOfTypes, oneOfTypes, and properties are found in the 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 {
	AdditionalProperties bool                       `json:"additionalProperties,omitempty"`
	AllOf                []Instance                 `json:"allOf,omitempty"`
	AnyOf                []Instance                 `json:"anyOf,omitempty"` // TODO unsupported
	Description          string                     `json:"description,omitempty"`
	Definitions          json.RawMessage            `json:"definitions,omitempty"`
	Format               string                     `json:"format,omitempty"`
	FromRef              string                     `json:"fromRef,omitempty"`           // Added as a way of tracking the ref which was already expanded
	GraphQLArguments     []string                   `json:"graphql-arguments,omitempty"` // For type="graphql-hydration" to also require query arguments.
	Items                json.RawMessage            `json:"items,omitempty"`
	OneOf                []Instance                 `json:"oneOf,omitempty"`
	Properties           map[string]json.RawMessage `json:"properties,omitempty"`
	Ref                  string                     `json:"$ref,omitempty"`
	Required             []string                   `json:"required,omitempty"`
	Schema               string                     `json:"$schema,omitempty"`
	Target               string                     `json:"target,omitempty"` // For type="graphql-hydration" or GraphQL schema type overrides.
	Type                 []string                   `json:"type"`
}

Instance represents a JSON Schema instance.

func (*Instance) UnmarshalJSON added in v1.3.0

func (i *Instance) UnmarshalJSON(data []byte) error

type Schema

type Schema struct {
	Instance

	Validator Validator `json:"-"`
}

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. Also note AnyOf fields are not supported at this time.

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 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. AnyOf fields are currently ignored.

Referenced files are recursively processed. At this time only definition and file references are supported.

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