jsonschema

package
v0.0.0-...-4397e64 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2016 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

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 Encoder

type Encoder struct {
	io.Writer
}

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,
				// NOTE: Min is currently encoded as '0E+00', not '0'.
				Validator: &schema.Float{Boundaries: &schema.Boundaries{Min: 0, Max: math.Inf(1)}},
			},
			"bar": schema.Field{
				Validator: &schema.Integer{},
			},
			"baz": schema.Field{
				ReadOnly:  true,
				Validator: &schema.String{MaxLen: 42},
			},
			"foobar": schema.Field{},
		},
	}
	b := new(bytes.Buffer)
	enc := jsonschema.NewEncoder(b)
	enc.Encode(&s)
	b2 := new(bytes.Buffer)
	json.Indent(b2, b.Bytes(), "", "| ")
	fmt.Println(b2)
}
Output:

{
| "type": "object",
| "additionalProperties": false,
| "properties": {
| | "bar": {
| | | "type": "integer"
| | },
| | "baz": {
| | | "readOnly": true,
| | | "type": "string",
| | | "maxLength": 42
| | },
| | "foo": {
| | | "type": "number",
| | | "minimum": 0E+00
| | },
| | "foobar": {}
| },
| "required": [
| | "foo"
| ]
}

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