Documentation ¶
Overview ¶
Package schematypes provides types for constructing JSON schemas programmatically. This is useful when having a plugin architecture that accepts JSON matching a given schema as input. As this will allow nesting of plugins.
Index ¶
- Variables
- func MustMap(schema Schema, data, target interface{}) error
- func MustValidate(schema Schema, data interface{})
- func MustValidateAndMap(schema Schema, data, target interface{})
- type AllOf
- type AnyOf
- type Array
- type Boolean
- type DateTime
- type Duration
- type Integer
- type IntegerEnum
- type Map
- type Number
- type Object
- type OneOf
- type Properties
- type Schema
- type String
- type StringEnum
- type URI
- type ValidationError
- type ValidationIssue
Constants ¶
This section is empty.
Variables ¶
var ErrTypeMismatch = errors.New("Type does not match the schema")
ErrTypeMismatch is returned when trying to map to a type that doesn't match or which isn't writable (for example passed by value and not pointer).
Functions ¶
func MustMap ¶
MustMap will map data into target using schema and panic, if it returns ErrTypeMismatch
func MustValidate ¶
func MustValidate(schema Schema, data interface{})
MustValidate panics if data doesn't validate against schema
func MustValidateAndMap ¶
func MustValidateAndMap(schema Schema, data, target interface{})
MustValidateAndMap panics if data doesn't validate or maps into target
Types ¶
type AllOf ¶
type AllOf []Schema
An AllOf instance represents the allOf JSON schema construction.
type AnyOf ¶
type AnyOf []Schema
An AnyOf instance represents the anyOf JSON schema construction.
type Array ¶
An Array struct represents the JSON schema for an array.
type Boolean ¶
Boolean schema type.
type DateTime ¶
DateTime schema type for strings with format: date-time.
type Duration ¶
Duration schema type for duration as integer seconds or string on the form:
/[-+]? (\d+ d(ays?)?)? (\d+ h((ours?)?|r))? (\d+ m(in(untes?)?)?)?/
This allows for a lot of human readable time durations, examples:
'31 days 2 hours 5 min' '5 min' '2 hours 5 min' '+ 3 minutes' '- 2 hr' '- 3 days' '1d2h3m' ' 1 day 2 hour 1 minutes '
type Integer ¶
The Integer struct represents a JSON schema for an integer.
type IntegerEnum ¶
IntegerEnum schema type for enums of integers.
func (IntegerEnum) Map ¶
func (s IntegerEnum) Map(data interface{}, target interface{}) error
Map takes data, validates and maps it into the target reference.
func (IntegerEnum) Schema ¶
func (s IntegerEnum) Schema() map[string]interface{}
Schema returns a JSON representation of the schema.
func (IntegerEnum) Validate ¶
func (s IntegerEnum) Validate(data interface{}) error
Validate the given data, this will return nil if data satisfies this schema. Otherwise, Validate(data) returns a ValidationError instance.
type Map ¶
type Map struct { Title string Description string Values Schema MinimumProperties int64 MaximumProperties int64 }
Map specifies schema for a map from string to values.
type Number ¶
Number schema type.
type Object ¶
type Object struct { Title string Description string Properties Properties AdditionalProperties bool Required []string }
Object specifies schema for an object.
func Merge ¶
Merge multiple object schemas, this will create an object schema with all the properties from the schemas given, and all the required properties as the given object schemas have.
This will fail if any schema has AdditionalProperties: true, or if any two schemas specifies the same key with different schemas.
When using this to merge multiple schemas into one schema, the Object.Filter method may be useful to strip forbidden properties such that the subset of values matching a specific schema used can be extract for use with Object.Validate or Object.Map.
func (Object) Filter ¶
Filter will create a new map with any additional properties that aren't allowed by the schema removed. This doesn't modify the data parameter, but returns a new map.
Note: Naturally this have no effect if AdditionalProperties is true.
type OneOf ¶
type OneOf []Schema
A OneOf instance represents the oneOf JSON schema construction.
type Properties ¶
Properties defines the properties for a object schema.
type Schema ¶
type Schema interface { Schema() map[string]interface{} Validate(data interface{}) error Map(data, target interface{}) error }
A Schema is implemented by any object that can represent a JSON schema.
type String ¶
type String struct { Title string Description string MinimumLength int MaximumLength int Pattern string }
String schema type.
type StringEnum ¶
StringEnum schema type for enums of strings.
func (StringEnum) Map ¶
func (s StringEnum) Map(data interface{}, target interface{}) error
Map takes data, validates and maps it into the target reference.
func (StringEnum) Schema ¶
func (s StringEnum) Schema() map[string]interface{}
Schema returns a JSON representation of the schema.
func (StringEnum) Validate ¶
func (s StringEnum) Validate(data interface{}) error
Validate the given data, this will return nil if data satisfies this schema. Otherwise, Validate(data) returns a ValidationError instance.
type URI ¶
URI schema type for strings with format: uri.
type ValidationError ¶
type ValidationError struct {
// contains filtered or unexported fields
}
ValidationError represents a validation failure as a list of validation issues.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
func (*ValidationError) Issues ¶
func (e *ValidationError) Issues(rootName string) []ValidationIssue
Issues returns the validation issues with given rootName as the start of the Path or "root" if rootName is the empty string.
type ValidationIssue ¶
type ValidationIssue struct {
// contains filtered or unexported fields
}
A ValidationIssue is any error found validating a JSON object.
func (*ValidationIssue) Path ¶
func (v *ValidationIssue) Path() string
Path returns the a path to the issue, on the form:
rootName.dictionary["other-key"].array[44].property
func (*ValidationIssue) String ¶
func (v *ValidationIssue) String() string
String returns a human readable string representation of the issue