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, []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, oneOfTypes, and properties are found in the 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 { AdditionalProperties bool `json:"additionalProperties,omitempty"` AllOf []Instance `json:"allOf,omitempty"` AnyOf []Instance `json:"anyOf,omitempty"` // TODO unsupported Description string `json:"description,omitempty"` Definitions json.RawMessage `json:"definitions,omitempty"` Format string `json:"format,omitempty"` FromRef string `json:"fromRef,omitempty"` // Added as a way of tracking the ref which was already expanded Items json.RawMessage `json:"items,omitempty"` OneOf []Instance `json:"oneOf,omitempty"` Properties map[string]json.RawMessage `json:"properties,omitempty"` Ref string `json:"$ref,omitempty"` Required []string `json:"required,omitempty"` Schema string `json:"$schema,omitempty"` Type string `json:"type"` }
Instance represents a JSON Schema instance.
type Schema ¶
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. Also note AnyOf fields are not supported at this time.
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 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. AnyOf fields are currently ignored.
Referenced files are recursively processed. At this time only definition and file references are supported.
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.