schema

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2020 License: Apache-2.0 Imports: 6 Imported by: 2

README

schema 👍

GoDoc Go Report Card Build Status Codecov

Simplified JSON-Schema

This is my first attempt to create a simplified, minimal implementation of JSON-Schema that is fast and has an easy API. If you're looking for a complete, rigorous implementation of JSON-Schema, you should try another tool.

What it Does

This library implements a sub-set of the JSON-Schema specification

What's Included
  • Unmarshal schema from JSON
  • Array, Boolean, Integer, Number, Object, and String type validators.
  • Custom Format rules
  • Happy API for accessing schema information, and walking a schema tree with a JSON-Pointer
What's Left Out
  • References
  • Loading remote Schemas by URI.

Pull Requests Welcome

This library is a work in progress, and will benefit from your experience reports, use cases, and contributions. If you have an idea for making Rosetta better, send in a pull request. We're all in this together! 👍

Documentation

Index

Constants

View Source
const TypeAny = Type("any")

TypeAny is the token used by JSON-Schema to designate that any kind of data

View Source
const TypeArray = Type("array")

TypeArray is the token used by JSON-Schema to designate that a schema describes an array.

View Source
const TypeBoolean = Type("boolean")

TypeBoolean is the token used by JSON-Schema to designate that a schema describes an boolean.

View Source
const TypeInteger = Type("integer")

TypeInteger is the token used by JSON-Schema to designate that a schema describes an integer.

View Source
const TypeNumber = Type("number")

TypeNumber is the token used by JSON-Schema to designate that a schema describes an number.

View Source
const TypeObject = Type("object")

TypeObject is the token used by JSON-Schema to designate that a schema describes an object.

View Source
const TypeString = Type("string")

TypeString is the token used by JSON-Schema to designate that a schema describes an string.

Variables

This section is empty.

Functions

This section is empty.

Types

type Any added in v0.4.0

type Any struct {
}

Any represents a any data type within a JSON-Schema.

func (Any) MarshalMap added in v0.5.0

func (any Any) MarshalMap() map[string]interface{}

MarshalMap populates object data into a map[string]interface{}

func (Any) Path added in v0.4.0

func (any Any) Path(p path.Path) (Element, error)

Path returns sub-schemas

func (Any) Type added in v0.4.0

func (any Any) Type() Type

Type returns the data type of this Element

func (*Any) UnmarshalMap added in v0.5.0

func (any *Any) UnmarshalMap(data map[string]interface{}) error

UnmarshalMap tries to populate this object using data from a map[string]interface{}

func (Any) Validate added in v0.4.0

func (any Any) Validate(value interface{}) error

Validate compares a generic data value using this Schema

type Array

type Array struct {
	Required bool
	Items    Element
}

Array represents an array data type within a JSON-Schema.

func (Array) MarshalMap added in v0.5.0

func (array Array) MarshalMap() map[string]interface{}

MarshalMap populates object data into a map[string]interface{}

func (Array) Path

func (array Array) Path(p path.Path) (Element, error)

Path returns sub-schemas of this array.

func (Array) Type

func (array Array) Type() Type

Type returns the data type of this Schema

func (*Array) UnmarshalMap added in v0.3.0

func (array *Array) UnmarshalMap(data map[string]interface{}) error

UnmarshalMap tries to populate this object using data from a map[string]interface{}

func (Array) Validate

func (array Array) Validate(value interface{}) error

Validate compares a generic data value using this Schema

type Boolean

type Boolean struct {
	Required bool      `json:"required"`
	Default  null.Bool `json:"default"`
}

Boolean represents a boolean data type within a JSON-Schema.

func (*Boolean) MarshalMap added in v0.5.0

func (boolean *Boolean) MarshalMap() map[string]interface{}

MarshalMap populates object data into a map[string]interface{}

func (*Boolean) Path

func (boolean *Boolean) Path(p path.Path) (Element, error)

Path returns sub-schemas

func (*Boolean) Type

func (boolean *Boolean) Type() Type

Type returns the data type of this Element

func (*Boolean) UnmarshalMap added in v0.5.0

func (boolean *Boolean) UnmarshalMap(data map[string]interface{}) error

UnmarshalMap tries to populate this object using data from a map[string]interface{}

func (*Boolean) Validate

func (boolean *Boolean) Validate(value interface{}) error

Validate compares a generic data value using this Schema

type Element added in v0.5.0

type Element interface {

	// Type returns the Type of this particular schema element
	Type() Type

	// Validate checks an arbitrary data structure against the rules in the schema
	Validate(interface{}) error

	// Path traverses this schema to find child element that matches the provided path
	Path(path.Path) (Element, error)

	// MarshalMap populates the object data into a map[string]interface{}
	MarshalMap() map[string]interface{}
}

Element interface wraps all of the methods required for schema elements.

func UnmarshalJSON added in v0.5.0

func UnmarshalJSON(data []byte) (Element, error)

UnmarshalJSON tries to parse a []byte into a schema.Element

func UnmarshalMap added in v0.5.0

func UnmarshalMap(data interface{}) (Element, error)

UnmarshalMap tries to parse a map[string]interface{} into a schema.Element

type Integer

type Integer struct {
	Required   bool     `json:"required"`
	Default    null.Int `json:"default"`
	Minimum    null.Int `json:"minimum"`
	Maximum    null.Int `json:"maximum"`
	MultipleOf null.Int `json:"multipleOf"`
}

Integer represents an integer data type within a JSON-Schema.

func (Integer) MarshalMap added in v0.5.0

func (integer Integer) MarshalMap() map[string]interface{}

MarshalMap populates object data into a map[string]interface{}

func (Integer) Path

func (integer Integer) Path(p path.Path) (Element, error)

Path returns sub-schemas

func (Integer) Type

func (integer Integer) Type() Type

Type returns the data type of this Schema

func (*Integer) UnmarshalMap added in v0.5.0

func (integer *Integer) UnmarshalMap(data map[string]interface{}) error

UnmarshalMap tries to populate this object using data from a map[string]interface{}

func (Integer) Validate

func (integer Integer) Validate(value interface{}) error

Validate compares a generic data value using this Schema

type Number

type Number struct {
	Required bool       `json:"required"`
	Default  null.Float `json:"default"`
	Minimum  null.Float `json:"minimum"`
	Maximum  null.Float `json:"maximum"`
}

Number represents a number data type within a JSON-Schema.

func (Number) MarshalMap added in v0.5.0

func (number Number) MarshalMap() map[string]interface{}

MarshalMap populates object data into a map[string]interface{}

func (Number) Path

func (number Number) Path(p path.Path) (Element, error)

Path returns sub-schemas

func (Number) Type

func (number Number) Type() Type

Type returns the data type of this Element

func (*Number) UnmarshalMap added in v0.5.0

func (number *Number) UnmarshalMap(data map[string]interface{}) error

UnmarshalMap tries to populate this object using data from a map[string]interface{}

func (Number) Validate

func (number Number) Validate(value interface{}) error

Validate compares a generic data value using this Schema

type Object

type Object struct {
	Required   bool
	Properties map[string]Element
}

Object represents an object data type within a JSON-Schema.

func (Object) MarshalMap added in v0.5.0

func (object Object) MarshalMap() map[string]interface{}

MarshalMap populates object data into a map[string]interface{}

func (Object) Path

func (object Object) Path(p path.Path) (Element, error)

Path returns sub-schemas

func (Object) Type

func (object Object) Type() Type

Type returns the data type of this Element

func (*Object) UnmarshalMap added in v0.5.0

func (object *Object) UnmarshalMap(data map[string]interface{}) error

UnmarshalMap tries to populate this object using data from a map[string]interface{}

func (Object) Validate

func (object Object) Validate(value interface{}) error

Validate compares a generic data value using this Schema

type Schema

type Schema struct {
	ID      string `json:"$id"`
	Comment string `json:"$comment"`
	// contains filtered or unexported fields
}

Schema defines a (simplified) JSON-Schema object, that can be Marshalled/Unmarshalled to JSON.

func (Schema) Element added in v0.5.0

func (schema Schema) Element() Element

Element returns the top-level element of this Schema

func (Schema) UnmarshalJSON added in v0.5.0

func (schema Schema) UnmarshalJSON(data []byte) error

UnmarshalJSON creates a new Schema object using a JSON-serialized byte array.

type String

type String struct {
	Required  bool
	Default   string
	MinLength null.Int
	MaxLength null.Int
	Pattern   string
	Format    string
}

String represents a string data type within a JSON-Schema.

func (String) MarshalMap added in v0.5.0

func (str String) MarshalMap() map[string]interface{}

MarshalMap populates object data into a map[string]interface{}

func (String) Path

func (str String) Path(p path.Path) (Element, error)

Path returns sub-schemas or an error

func (String) Type

func (str String) Type() Type

Type returns the data type of this Element

func (*String) UnmarshalMap added in v0.5.0

func (str *String) UnmarshalMap(data map[string]interface{}) error

UnmarshalMap tries to populate this object using data from a map[string]interface{}

func (String) Validate

func (str String) Validate(value interface{}) error

Validate compares a generic data value using this Schema

type StringFormat added in v0.3.0

type StringFormat func(string) error

StringFormat verifies that a string matches the desired format, and returns a non-nil error if it does not.

type Type added in v0.3.0

type Type string

Type enumerates all of the data types that can make up a schema

func (Type) String added in v0.3.0

func (schemaType Type) String() string

String implements the ubiquitous "Stringer" interface, so that these types can be represented as strings, if necessary

type WritableElement added in v0.6.0

type WritableElement interface {

	// UnmarshalMap tries to populate this object using data from a map[string]interface{}
	UnmarshalMap(map[string]interface{}) error

	Element
}

Jump to

Keyboard shortcuts

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