jsonschema

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2019 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package jsonschema provides JSON Schema Draft 4 encoding support for schema.Schema. Note that the current implementation is incomplete, and not all FieldValidator types are yet supported. Custom validators are also not supported at the moment.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	//ErrKeysValidatorNotSupported is returned when Dict.KeysValidator is not a
	//*schema.String instance or nil.
	ErrKeysValidatorNotSupported = errors.New("KeysValidator type not supported")
)
View Source
var (
	//ErrNoSchema is returned when trying to JSON Encode a schema.Object with
	//the Schema property set to nil.
	ErrNoSchema = errors.New("no schema defined for object")
)
View Source
var (
	//ErrNoSchemaList is returned when trying to JSON Encode an empty
	//schema.AnyOf or schema.AllOf slice.
	ErrNoSchemaList = errors.New("at least one schema must be specified")
)
View Source
var (
	// ErrNotImplemented is returned when the JSON schema encoding logic for a
	// schema.FieldValidator has not (yet) been implemented.
	ErrNotImplemented = errors.New("not implemented")
)

Functions

This section is empty.

Types

type Builder added in v0.2.0

type Builder interface {
	// BuildJSONSchema should return a map containing JSON Schema Draft 4
	// properties that can be set based on FieldValidator data. Application
	// specific properties can be added as well, but should not conflict with
	// any legal JSON Schema keys.
	BuildJSONSchema() (map[string]interface{}, error)
}

The Builder interface should be implemented by custom schema.FieldValidator implementations to allow JSON Schema serialization.

func ValidatorBuilder added in v0.2.0

func ValidatorBuilder(v schema.FieldValidator) (Builder, error)

ValidatorBuilder type-casts v to a valid Builder implementation or returns an error.

type Encoder

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

Encoder writes the JSON Schema representation of a schema.Schema to an output stream. Note that only a sub-set of the FieldValidator types in the schema package is supported at the moment. Custom validators are also not yet handled. Attempting to encode a schema containing such fields will result in a ErrNotImplemented error.

Example
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"math"

	"github.com/rs/rest-layer/schema"
	"github.com/rs/rest-layer/schema/encoding/jsonschema"
)

func main() {
	s := schema.Schema{
		Fields: schema.Fields{
			"foo": schema.Field{
				Required:  true,
				Validator: &schema.Float{Boundaries: &schema.Boundaries{Min: 0, Max: math.Inf(1)}},
			},
			"bar": schema.Field{
				Validator: &schema.Integer{},
			},
			"baz": schema.Field{
				Description: "baz can not be set by the user",
				ReadOnly:    true,
				Validator:   &schema.String{MaxLen: 42},
			},
			"foobar": schema.Field{
				Description: "foobar can hold any valid JSON value",
			},
		},
	}
	b := new(bytes.Buffer)
	enc := jsonschema.NewEncoder(b)
	enc.Encode(&s)
	b2 := new(bytes.Buffer)
	json.Indent(b2, b.Bytes(), "", "| ")
	fmt.Println(b2)
}
Output:

{
| "additionalProperties": false,
| "properties": {
| | "bar": {
| | | "type": "integer"
| | },
| | "baz": {
| | | "description": "baz can not be set by the user",
| | | "maxLength": 42,
| | | "readOnly": true,
| | | "type": "string"
| | },
| | "foo": {
| | | "minimum": 0,
| | | "type": "number"
| | },
| | "foobar": {
| | | "description": "foobar can hold any valid JSON value"
| | }
| },
| "required": [
| | "foo"
| ],
| "type": "object"
}

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new JSONSchema Encoder that writes to w.

func (*Encoder) Encode

func (e *Encoder) Encode(s *schema.Schema) error

Encode writes the JSON Schema representation of s to the stream, followed by a newline character.

Jump to

Keyboard shortcuts

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