Documentation
¶
Overview ¶
Example ¶
s, err := schema.ReadFile("schema.json")
if err != nil {
log.Printf("failed to read schema: %s", err)
return
}
for name, pdef := range s.Properties {
// Do what you will with `pdef`, which contain
// Schema information for `name` property
_ = name
_ = pdef
}
// Create a validator
v := validator.New(s)
// You can also validate an arbitrary piece of data
var p interface{} // initialize using json.Unmarshal...
if err := v.Validate(p); err != nil {
log.Printf("failed to validate data: %s", err)
}
Index ¶
- Constants
- Variables
- type AdditionalItems
- type AdditionalProperties
- type Bool
- type DependencyMap
- type Format
- type Integer
- type ItemSpec
- type Number
- type PrimitiveType
- type PrimitiveTypes
- type Schema
- func (s *Schema) BaseURL() *url.URL
- func (s *Schema) Decode(in io.Reader) error
- func (s *Schema) DeleteProp(propertyPath string) bool
- func (s *Schema) Extract(m map[string]interface{}) error
- func (s *Schema) GetAllProps() []string
- func (s *Schema) IsPropRequired(pname string) bool
- func (s *Schema) IsResolved() bool
- func (s *Schema) MarshalJSON() ([]byte, error)
- func (s *Schema) Resolve(ctx interface{}) (ref *Schema, err error)
- func (s *Schema) ResolveURL(v string) (u *url.URL, err error)
- func (s *Schema) Root() *Schema
- func (s *Schema) Scope() string
- func (s *Schema) UnmarshalJSON(data []byte) error
- type SchemaList
Examples ¶
Constants ¶
const ( // SchemaURL contains the JSON Schema URL SchemaURL = `http://json-schema.org/draft-04/schema` // HyperSchemaURL contains the JSON Hyper Schema URL HyperSchemaURL = `http://json-schema.org/draft-03/hyper-schema` // MIMEType contains the MIME used for a JSON Schema MIMEType = "application/schema+json" )
Variables ¶
var ErrExpectedArrayOfString = errors.New("invalid value: expected array of string")
ErrExpectedArrayOfString is returned when we encounter something other than array of strings
var ErrInvalidStringArray = ErrExpectedArrayOfString
ErrInvalidStringArray is the same as ErrExpectedArrayOfString. This is here only for backwards compatibility
Functions ¶
This section is empty.
Types ¶
type AdditionalItems ¶
type AdditionalItems struct {
*Schema
}
AdditionalItems represents schema for additonalItems
type AdditionalProperties ¶
type AdditionalProperties struct {
*Schema
}
AdditionalProperties represents schema for additonalProperties
type Bool ¶
Bool represents a "boolean" value in a JSON Schema, such as "exclusiveMinimum", "exclusiveMaximum", etc.
type DependencyMap ¶
DependencyMap contains the dependencies defined within this schema. for a given dependency name, you can have either a schema or a list of property names
type Integer ¶
Integer represents a "integer" value in a JSON Schema, such as "minLength", "maxLength", etc.
type ItemSpec ¶
type ItemSpec struct {
TupleMode bool // If this is true, the positions mean something. if false, len(Schemas) should be 1, and we should apply the same schema validation to all elements
Schemas SchemaList
}
ItemSpec represents a specification for `item` field
type Number ¶
Number represents a "number" value in a JSON Schema, such as "minimum", "maximum", etc.
type PrimitiveType ¶
type PrimitiveType int
PrimitiveType represents a JSON Schema primitive type such as "string", "integer", etc.
const ( UnspecifiedType PrimitiveType = iota NullType IntegerType StringType ObjectType ArrayType BooleanType NumberType )
The list of primitive types
func (PrimitiveType) MarshalJSON ¶
func (t PrimitiveType) MarshalJSON() ([]byte, error)
MarshalJSON seriealises the primitive type into a JSON string
func (PrimitiveType) String ¶
func (t PrimitiveType) String() string
String returns the string representation of this primitive type
func (*PrimitiveType) UnmarshalJSON ¶
func (t *PrimitiveType) UnmarshalJSON(data []byte) error
UnmarshalJSON initializes the primitive type from a JSON string.
type PrimitiveTypes ¶
type PrimitiveTypes []PrimitiveType
PrimitiveTypes is a list of PrimitiveType
func (PrimitiveTypes) Contains ¶
func (pt PrimitiveTypes) Contains(p PrimitiveType) bool
Contains returns true if the list of primitive types contains `p`
func (PrimitiveTypes) Len ¶
func (pt PrimitiveTypes) Len() int
Len returns the length of the list of primitive types
func (PrimitiveTypes) Less ¶
func (pt PrimitiveTypes) Less(i, j int) bool
Less returns true if the i-th element in the list is listed before the j-th element.
func (PrimitiveTypes) Swap ¶
func (pt PrimitiveTypes) Swap(i, j int)
Swap swaps the elements in positions i and j
func (*PrimitiveTypes) UnmarshalJSON ¶
func (pt *PrimitiveTypes) UnmarshalJSON(data []byte) error
UnmarshalJSON initializes the list of primitive types
type Schema ¶
type Schema struct {
ID string `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Default interface{} `json:"default,omitempty"`
Type PrimitiveTypes `json:"type,omitempty"`
SchemaRef string `json:"$schema,omitempty"`
Definitions map[string]*Schema `json:"definitions,omitempty"`
Reference string `json:"$ref,omitempty"`
Format Format `json:"format,omitempty"`
// NumericValidations
MultipleOf Number `json:"multipleOf,omitempty"`
Minimum Number `json:"minimum,omitempty"`
Maximum Number `json:"maximum,omitempty"`
ExclusiveMinimum Bool `json:"exclusiveMinimum,omitempty"`
ExclusiveMaximum Bool `json:"exclusiveMaximum,omitempty"`
// StringValidation
MaxLength Integer `json:"maxLength,omitempty"`
MinLength Integer `json:"minLength,omitempty"`
Pattern *regexp.Regexp `json:"pattern,omitempty"`
// ArrayValidations
AdditionalItems *AdditionalItems
Items *ItemSpec
MinItems Integer
MaxItems Integer
UniqueItems Bool
// ObjectValidations
MaxProperties Integer `json:"maxProperties,omitempty"`
MinProperties Integer `json:"minProperties,omitempty"`
Required []string `json:"required,omitempty"`
Dependencies DependencyMap `json:"dependencies,omitempty"`
Properties map[string]*Schema `json:"properties,omitempty"`
AdditionalProperties *AdditionalProperties `json:"additionalProperties,omitempty"`
PatternProperties map[*regexp.Regexp]*Schema `json:"patternProperties,omitempty"`
Enum []interface{} `json:"enum,omitempty"`
AllOf SchemaList `json:"allOf,omitempty"`
AnyOf SchemaList `json:"anyOf,omitempty"`
OneOf SchemaList `json:"oneOf,omitempty"`
Not *Schema `json:"not,omitempty"`
Extras map[string]interface{} `json:"-"`
// contains filtered or unexported fields
}
Schema represents a JSON Schema object
func (*Schema) Decode ¶
Decode reads from `in` and parses its content to initialize the schema object
func (*Schema) DeleteProp ¶
DeleteProp deletes property via the split of the attribute path Return whether the property has been deleted
func (*Schema) GetAllProps ¶
GetAllProps returns all properties in an array of string
func (*Schema) IsPropRequired ¶
IsPropRequired can be used to query this schema if a given property name is required.
func (*Schema) IsResolved ¶
IsResolved returns true if this schema has no Reference.
func (*Schema) MarshalJSON ¶
MarshalJSON serializes the schema into a JSON string
func (*Schema) Resolve ¶
Resolve returns the schema after it has been resolved. If s.Reference is the empty string, the current schema is returned.
`ctx` is an optional context to resolve the reference with. If not specified, the root schema as returned by `Root` will be used.
func (*Schema) ResolveURL ¶
ResolveURL takes a url string, and resolves it if it's a relative URL
func (*Schema) Root ¶
Root returns the upmost parent schema object within the hierarchy of schemas. For example, the `item` element in a schema for an array is also a schema, and you could reference elements in parent schemas.
func (*Schema) UnmarshalJSON ¶
UnmarshalJSON takes a JSON string and initializes the schema
type SchemaList ¶
type SchemaList []*Schema
SchemaList is a list of Schemas
func (*SchemaList) Extract ¶
func (l *SchemaList) Extract(v interface{}) error
Extract takes either a list of `map[string]interface{}` or a single `map[string]interface{}` to initialize this list of schemas