schema

package module
v0.0.0-...-bb2b97e Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2016 License: MIT Imports: 9 Imported by: 21

README

schema

Package schema provides json-schema reading, validation support

schema

-- import "github.com/cihangir/schema"

Package schema provides json-schema reading, validation, support

Usage

var Helpers = template.FuncMap{
	"AsComment":               stringext.AsComment,
	"JSONTag":                 stringext.JSONTag,
	"Params":                  Params,
	"Args":                    Args,
	"Values":                  Values,
	"goType":                  goType,
	"GenerateValidator":       generateValidator,
	"ToLowerFirst":            stringext.ToLowerFirst,
	"ToUpperFirst":            stringext.ToUpperFirst,
	"DepunctWithInitialUpper": stringext.DepunctWithInitialUpper,
	"DepunctWithInitialLower": stringext.DepunctWithInitialLower,
}

Helpers holds helpers for templates

func Args
func Args(h *HRef) string

Args creates arguments string

func Params
func Params(l *Link) string

Params creates the parameter string for the given link

func Parse
func Parse(t *template.Template) (*template.Template, error)

Parse parses declared templates.

func Required
func Required(n string, def *Schema) bool

Required checks if the given n is a required property

func Values
func Values(n string, s *Schema, l *Link) string

Values creates the value string

type HRef
type HRef struct {
	Order   []string
	Schemas map[string]*Schema
}

HRef represents a Link href.

func NewHRef
func NewHRef(href string) *HRef

NewHRef creates a new HRef struct based on a href value.

func (*HRef) MarshalJSON
func (h *HRef) MarshalJSON() ([]byte, error)

MarshalJSON returns *h as the JSON encoding of h.

func (*HRef) Resolve
func (h *HRef) Resolve(r *Schema)

Resolve resolves a href inside a Schema.

func (*HRef) String
func (h *HRef) String() string
func (*HRef) URL
func (h *HRef) URL() (*url.URL, error)

URL returns a usable URL for the href.

func (*HRef) UnmarshalJSON
func (h *HRef) UnmarshalJSON(data []byte) error

UnmarshalJSON sets *h to a copy of data.

type Link struct {
	Title       string  `json:"title,omitempty"`
	Description string  `json:"description,omitempty"`
	HRef        *HRef   `json:"href,omitempty"`
	Rel         string  `json:"rel,omitempty"`
	Method      string  `json:"method,omitempty"`
	Schema      *Schema `json:"schema,omitempty"`
}

Link represents a Link description.

func (l *Link) GoType() string

GoType returns Go type for the given schema as string.

func (l *Link) Parameters() ([]string, map[string]string)

Parameters returns function parameters names and types.

func (l *Link) Resolve(r *Schema)

Resolve resolve link schema and href.

type Reference
type Reference string

Reference represents a JSON Reference.

func (Reference) Resolve
func (rf Reference) Resolve(r *Schema) *Schema

Resolve resolves reference inside a Schema.

type Schema
type Schema struct {
	ID          string `json:"id,omitempty"`
	Title       string `json:"title,omitempty"`
	Description string `json:"description,omitempty"`
	Version     string `json:"version,omitempty"`

	Default  interface{} `json:"default,omitempty"`
	ReadOnly bool        `json:"readOnly,omitempty"`
	Example  interface{} `json:"example,omitempty"`
	Format   string      `json:"format,omitempty"`

	Type interface{} `json:"type,omitempty"`

	Ref    *Reference `json:"$ref,omitempty"`
	Schema *Reference `json:"$schema,omitempty"`

	Definitions map[string]*Schema `json:"definitions,omitempty"`

	// Numbers
	MultipleOf       float64 `json:"multipleOf,omitempty"`
	Maximum          float64 `json:"maximum,omitempty"`
	ExclusiveMaximum bool    `json:"exclusiveMaximum,omitempty"`
	Minimum          float64 `json:"minimum,omitempty"`
	ExclusiveMinimum bool    `json:"exclusiveMinimum,omitempty"`

	// Strings
	MinLength int    `json:"minLength,omitempty"`
	MaxLength int    `json:"maxLength,omitempty"`
	Pattern   string `json:"pattern,omitempty"`

	// Objects
	MinProperties        int                    `json:"minProperties,omitempty"`
	MaxProperties        int                    `json:"maxProperties,omitempty"`
	Required             []string               `json:"required,omitempty"`
	Properties           map[string]*Schema     `json:"properties,omitempty"`
	Dependencies         map[string]interface{} `json:"dependencies,omitempty"`
	AdditionalProperties interface{}            `json:"additionalProperties,omitempty"`
	PatternProperties    map[string]*Schema     `json:"patternProperties,omitempty"`

	// Arrays
	Items           *Schema     `json:"items,omitempty"`
	MinItems        int         `json:"minItems,omitempty"`
	MaxItems        int         `json:"maxItems,omitempty"`
	UniqueItems     bool        `json:"uniqueItems,omitempty"`
	AdditionalItems interface{} `json:"additionalItems,omitempty"`

	// All
	Enum []string `json:"enum,omitempty"`

	// Schemas
	OneOf []Schema `json:"oneOf,omitempty"`
	AnyOf []Schema `json:"anyOf,omitempty"`
	AllOf []Schema `json:"allOf,omitempty"`
	Not   *Schema  `json:"not,omitempty"`

	// Links
	Links []Link `json:"links,omitempty"`
}

Schema represents a JSON Schema.

func (*Schema) GoType
func (s *Schema) GoType() string

GoType returns the Go type for the given schema as string.

func (*Schema) IsCustomType
func (s *Schema) IsCustomType() bool

IsCustomType returns true if the schema declares a custom type.

func (*Schema) Resolve
func (s *Schema) Resolve(r *Schema) *Schema

Resolve resolves reference inside the schema.

func (*Schema) Types
func (s *Schema) Types() (types []string)

Types returns the array of types described by this schema.

func (*Schema) URL
func (s *Schema) URL() string

URL returns schema base URL.

func (*Schema) Values
func (s *Schema) Values(name string, l *Link) []string

Values returns function return values types.

Documentation

Overview

Package schema provides json-schema reading, validation, support

Index

Constants

This section is empty.

Variables

View Source
var Helpers = template.FuncMap{
	"AsComment":               stringext.AsComment,
	"JSONTagWithIgnored":      stringext.JSONTagWithIgnored,
	"goType":                  goType,
	"GenerateValidator":       generateValidator,
	"ToLowerFirst":            stringext.ToLowerFirst,
	"ToUpperFirst":            stringext.ToUpperFirst,
	"DepunctWithInitialUpper": stringext.DepunctWithInitialUpper,
	"DepunctWithInitialLower": stringext.DepunctWithInitialLower,
}

Helpers holds helpers for templates

Functions

func Argumentize

func Argumentize(s interface{}) string

Argumentize returns a string that can be used as an argument into a function

func Parse

func Parse(t *template.Template) (*template.Template, error)

Parse parses declared templates.

func Required

func Required(n string, def *Schema) bool

Required checks if the given n is a required property

func SortedKeys

func SortedKeys(m map[string]*Schema) (keys []string)

SortedKeys sorts given schema map according to PropertyOrder property, 0 valued properties are sort with strings.Sort

Types

type Generator

type Generator map[string]interface{}

Generator holds arbitary data about a generator's settings

func (*Generator) Get

func (g *Generator) Get(key string) interface{}

Get returns the setting data, if exits

func (*Generator) GetWithDefault

func (g *Generator) GetWithDefault(key string, def interface{}) interface{}

GetWithDefault returns the existing setting if exists, if not returns the given setting

func (*Generator) Set

func (g *Generator) Set(key string, val interface{})

Set sets a value to the settings, overrides any previous value

func (*Generator) SetNX

func (g *Generator) SetNX(key string, val interface{})

SetNX sets a value to the settings if only value does not exists

type Generators

type Generators []map[string]Generator

Generators acts as a container for generators

func (*Generators) Get

func (g *Generators) Get(s string) (Generator, bool)

Get returns the generator with given key

func (Generators) Has

func (g Generators) Has(s string) bool

Has checks if the given generator exists

type HRef

type HRef struct {
	Order   []string
	Schemas map[string]*Schema
	// contains filtered or unexported fields
}

HRef represents a Link href.

func NewHRef

func NewHRef(href string) *HRef

NewHRef creates a new HRef struct based on a href value.

func (*HRef) MarshalJSON

func (h *HRef) MarshalJSON() ([]byte, error)

MarshalJSON returns *h as the JSON encoding of h.

func (*HRef) Resolve

func (h *HRef) Resolve(r *Schema)

Resolve resolves a href inside a Schema.

func (*HRef) String

func (h *HRef) String() string

func (*HRef) URL

func (h *HRef) URL() (*url.URL, error)

URL returns a usable URL for the href.

func (*HRef) UnmarshalJSON

func (h *HRef) UnmarshalJSON(data []byte) error

UnmarshalJSON sets *h to a copy of data.

type Link struct {
	Title        string  `json:"title,omitempty"`
	Description  string  `json:"description,omitempty"`
	HRef         *HRef   `json:"href,omitempty"`
	Rel          string  `json:"rel,omitempty"`
	Method       string  `json:"method,omitempty"`
	Schema       *Schema `json:"schema,omitempty"`
	TargetSchema *Schema `json:"targetSchema,omitempty"`
}

Link represents a Link description.

func (*Link) GoType

func (l *Link) GoType() string

GoType returns Go type for the given schema as string.

func (*Link) Resolve

func (l *Link) Resolve(r *Schema)

Resolve resolve link schema and href.

type Parameter

type Parameter struct {
	Description string `json:"description,omitempty"`
	Format      string `json:"format,omitempty"`
	Maximum     int    `json:"maximum,omitempty"`
	Minimum     int    `json:"minimum,omitempty"`
	Type        string `json:"type,omitempty"`

	In       string  `json:"in,omitempty"`
	Name     string  `json:"name,omitempty"`
	Required bool    `json:"required,omitempty"`
	Schema   *Schema `json:"schema,omitempty"`
}

Parameter holds parameter data

type Path

type Path struct {
	Consumes    []string              `json:"consumes,omitempty"`
	Description string                `json:"description,omitempty"`
	OperationID string                `json:"operationId,omitempty"`
	Parameters  []*Parameter          `json:"parameters,omitempty"`
	Produces    []string              `json:"produces,omitempty"`
	Responses   map[string]*Schema    `json:"responses,omitempty"`
	Security    []map[string][]string `json:"security,omitempty"`
	Summary     string                `json:"summary,omitempty"`
	Tags        []string              `json:"tags,omitempty"`
}

Path holds path data

type Reference

type Reference string

Reference represents a JSON Reference.

func (Reference) Resolve

func (rf Reference) Resolve(r *Schema) *Schema

Resolve resolves reference inside a Schema.

type Schema

type Schema struct {
	ID          string `json:"id,omitempty"`
	Title       string `json:"title,omitempty"`
	Description string `json:"description,omitempty"`
	Version     string `json:"version,omitempty"`

	Default  interface{} `json:"default,omitempty"`
	ReadOnly bool        `json:"readOnly,omitempty"`
	Example  interface{} `json:"example,omitempty"`
	Format   string      `json:"format,omitempty"`

	Tags string      `json:"tags,omitempty"`
	Type interface{} `json:"type,omitempty"`

	Ref    *Reference `json:"$ref,omitempty"`
	Schema *Reference `json:"$schema,omitempty"`

	Definitions map[string]*Schema `json:"definitions,omitempty"`

	// Numbers
	MultipleOf       float64 `json:"multipleOf,omitempty"`
	Maximum          float64 `json:"maximum,omitempty"`
	ExclusiveMaximum bool    `json:"exclusiveMaximum,omitempty"`
	Minimum          float64 `json:"minimum,omitempty"`
	ExclusiveMinimum bool    `json:"exclusiveMinimum,omitempty"`

	// Strings
	MinLength int    `json:"minLength,omitempty"`
	MaxLength int    `json:"maxLength,omitempty"`
	Pattern   string `json:"pattern,omitempty"`

	// Objects
	MinProperties        int                    `json:"minProperties,omitempty"`
	MaxProperties        int                    `json:"maxProperties,omitempty"`
	Required             []string               `json:"required,omitempty"`
	Properties           map[string]*Schema     `json:"properties,omitempty"`
	Dependencies         map[string]interface{} `json:"dependencies,omitempty"`
	AdditionalProperties interface{}            `json:"additionalProperties,omitempty"`
	PatternProperties    map[string]*Schema     `json:"patternProperties,omitempty"`

	// Arrays
	Items           []*Schema   `json:"items,omitempty"`
	MinItems        int         `json:"minItems,omitempty"`
	MaxItems        int         `json:"maxItems,omitempty"`
	UniqueItems     bool        `json:"uniqueItems,omitempty"`
	AdditionalItems interface{} `json:"additionalItems,omitempty"`

	// All
	Enum []string `json:"enum,omitempty"`

	// Schemas
	OneOf []Schema `json:"oneOf,omitempty"`
	AnyOf []Schema `json:"anyOf,omitempty"`
	AllOf []Schema `json:"allOf,omitempty"`
	Not   *Schema  `json:"not,omitempty"`

	// Links
	Links []Link `json:"links,omitempty"`

	// Holds the order of property, for more info https://github.com/json-schema/json-schema/issues/87
	PropertyOrder int `json:"propertyOrder,omitempty"`

	// Functions holds the functions of a schema,
	Functions map[string]*Schema `json:"functions,omitempty"`

	// if the struct, property or the function is exported or not
	Private bool `json:"private,omitempty"`

	// Generators holds the generator plugins for current schema
	Generators Generators `json:"generators,omitempty"`

	// rest endpoint paths
	Paths map[string]map[string]*Path `json:"paths,omitempty"`
}

Schema represents a JSON Schema.

func SortedSchema

func SortedSchema(m map[string]*Schema) []*Schema

SortedSchema sorts given map[string]Schema and returns a slice of Schema using SortedKeys function

func (*Schema) GoType

func (s *Schema) GoType() string

GoType returns the Go type for the given schema as string.

func (*Schema) IsCustomType

func (s *Schema) IsCustomType() bool

IsCustomType returns true if the schema declares a custom type.

func (*Schema) Resolve

func (s *Schema) Resolve(r *Schema) *Schema

Resolve resolves reference inside the schema.

func (*Schema) Types

func (s *Schema) Types() (types []string)

Types returns the array of types described by this schema.

func (*Schema) URL

func (s *Schema) URL() string

URL returns schema base URL.

Jump to

Keyboard shortcuts

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