rest-layer: github.com/rs/rest-layer/schema/encoding/jsonschema Index | Examples | Files

package jsonschema

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

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

Package Files

allof.go anyof.go array.go bool.go dict.go doc.go encoder.go float.go integer.go ip.go null.go object.go password.go schema.go string.go time.go url.go

Variables

var (
    //ErrKeysValidatorNotSupported is returned when Dict.KeysValidator is not a
    //*schema.String instance or nil.
    ErrKeysValidatorNotSupported = errors.New("KeysValidator type not supported")
)
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")
)
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")
)
var (
    // ErrNotImplemented is returned when the JSON schema encoding logic for a
    // schema.FieldValidator has not (yet) been implemented.
    ErrNotImplemented = errors.New("not implemented")
)

type Builder Uses

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 Uses

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

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

type Encoder Uses

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.

Code:

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 Uses

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new JSONSchema Encoder that writes to w.

func (*Encoder) Encode Uses

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

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

Package jsonschema imports 8 packages (graph). Updated 2017-07-11. Refresh now. Tools for package owners.