Documentation
¶
Overview ¶
Package jsonschema provides JSON Schema Draft 4 encoding support for schema.Schema. Note that the current implementation is incomlete, and not all FieldValidator types are yet supported. Custom validators are also not supported at the momenet.
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 ¶
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 ¶
NewEncoder returns a new JSONSchema Encoder that writes to w.
Click to show internal directories.
Click to hide internal directories.