Documentation ¶
Overview ¶
Package jsonschema includes tools for walking JSON schema files and running a custom function for each instance.
Index ¶
- func NewValidator(schemaPath string) (*validator, error)
- func SchemaTypes(schemaPath string) ([]string, []string, error)
- func Walk(s *Schema, walkFn WalkInstanceFunc) error
- func WalkRaw(s *Schema, walkFn WalkRawFunc) error
- type Instance
- type Schema
- type Validator
- type WalkInstanceFunc
- type WalkRawFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewValidator ¶
NewValidator returns a Validator for the schema at the given file path. The Validate method on the Validator allows for verifying a JSON file matches the JSON schema.
func SchemaTypes ¶
SchemaTypes will parse the given file and report which top level allOfTypes and oneOfTypes are found in that schema.
func Walk ¶
func Walk(s *Schema, walkFn WalkInstanceFunc) error
Walk runs each instance in the JSON schema through the defined walk function, keeping track of the JSONPath. It assumes the JSON schema is valid and does not check for many errors such as bad property names. For instances that are objects or arrays the WalkFunc will be called for each child instance.
func WalkRaw ¶
func WalkRaw(s *Schema, walkFn WalkRawFunc) error
WalkRaw works nearly identical to the Walk function but rather than calling a WalkFunc calls a WalkRawFunc. By skipping the JSON unmarshaling of the Instance it runs nearly 10 times faster then WalkFunc.
Types ¶
type Instance ¶
type Instance struct { Ref string `json:"$ref,omitempty"` Schema string `json:"$schema,omitempty"` AdditionalProperties bool `json:"additionalProperties,omitempty"` Description string `json:"description,omitempty"` Type string `json:"type"` Format string `json:"format,omitempty"` Items json.RawMessage `json:"items,omitempty"` Properties map[string]json.RawMessage `json:"properties,omitempty"` AllOf []Instance `json:"allOf,omitempty"` OneOf []Instance `json:"oneOf,omitempty"` Required []string `json:"Required,omitempty"` }
Instance represents a JSON Schema instance.
type Schema ¶
type Schema struct { Instance // contains filtered or unexported fields }
Schema represents a JSON Schema with the AllOf and OneOf references parsed and squashed into a single representation. This is not a fully spec compatible representation but a basic representation useful for walking through the schema instances within a schema.
A fully spec compatible version of the schema is kept for validation purposes.
func SchemaFromFile ¶
SchemaFromFile parses a file at the given path and returns a schema based on its contents. The function traverses top level allOf fields within the schema. For oneOf fields the reference base name minus any extension is compared to the value of the oneOfType argument and if they match that file is also traversed.
Only file references are supported.
Note: A top level array will use the items from the first file to define them.
type Validator ¶
type Validator interface {
Validate(raw json.RawMessage) (bool, error)
}
Validator defines an interface for validating JSON matches a JSON schema.
type WalkInstanceFunc ¶
WalkFunc processes a single Instance within a JSON schema file returning an error on any problems. The path corresponds to the JSONPath (http://goessner.net/articles/JsonPath/) of the instance within the JSON format described by the JSON Schema.
type WalkRawFunc ¶
type WalkRawFunc func(path string, value json.RawMessage) error
WalkRawFunc works similar to WalkFunc but rather than accepting an instance it accepts the raw JSON for each level of the schema.