Documentation ¶
Overview ¶
Package jsonschema reads and describes JSON Schema documents.
Compatible with JSON Schema draft-07 as specified in [draft-handrews-json-schema-01](https://tools.ietf.org/html/draft-handrews-json-schema-01).
Index ¶
Constants ¶
const ( UnspecifiedType PrimitiveType = "unspecified" NullType = "null" BooleanType = "boolean" ObjectType = "object" ArrayType = "array" NumberType = "number" StringType = "string" IntegerType = "integer" )
Variables ¶
This section is empty.
Functions ¶
func EncodeReferenceTokens ¶
func EncodeReferenceTokens(tokens []ReferenceToken) string
EncodeReferenceTokens encodes the reference tokens to a string.
TODO(sqs): Fully implement the encoding specified in https://tools.ietf.org/html/rfc6901#section-6.
func Walk ¶
Walk traverses a JSON Schema in depth-first order. It starts by calling v.Visit(schema); schema must not be nil. If the visitor w returned by v.Visit(schema) is not nil, Walk is invoked recursively with visitor w for each of the non-nil children of schema, followed by a call of w.Visit(nil).
Types ¶
type DependencyValue ¶
func (*DependencyValue) MarshalJSON ¶
func (v *DependencyValue) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
func (*DependencyValue) UnmarshalJSON ¶
func (v *DependencyValue) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler.
type ID ¶
type ID struct { // Base is the base URI that the reference tokens are resolved relative to. Base *url.URL // ReferenceTokens describe a value in the JSON document identified by the base URI. // // See [RFC 6091](https://tools.ietf.org/html/rfc6901). ReferenceTokens []ReferenceToken }
An ID identifies a JSON Schema.
The identifier consists of the base URI and any JSON Pointer reference-tokens (see [RFC 6091](https://tools.ietf.org/html/rfc6901)).
func (ID) ResolveReference ¶
func (id ID) ResolveReference(ref []ReferenceToken) ID
ResolveReference returns a copy of id with the provided reference tokens appended.
type PrimitiveType ¶
type PrimitiveType string
func (PrimitiveType) String ¶
func (t PrimitiveType) String() string
type PrimitiveTypeList ¶
type PrimitiveTypeList []PrimitiveType
func (PrimitiveTypeList) Len ¶
func (l PrimitiveTypeList) Len() int
func (PrimitiveTypeList) Less ¶
func (l PrimitiveTypeList) Less(i, j int) bool
func (*PrimitiveTypeList) MarshalJSON ¶
func (l *PrimitiveTypeList) MarshalJSON() ([]byte, error)
func (PrimitiveTypeList) Swap ¶
func (l PrimitiveTypeList) Swap(i, j int)
func (*PrimitiveTypeList) UnmarshalJSON ¶
func (l *PrimitiveTypeList) UnmarshalJSON(buf []byte) error
type ReferenceToken ¶
type ReferenceToken struct { Name string // dereference object's named property Keyword bool // if Name != "", whether the Name is a JSON Schema keyword (e.g., "properties", "items", etc.) Index int // dereference array's index }
A ReferenceToken describes a one-level traversal in a JSON document. See [RFC 6091](https://tools.ietf.org/html/rfc6901).
type Schema ¶
type Schema struct { Comment *string `json:"$comment,omitempty"` ID *string `json:"$id,omitempty"` Reference *string `json:"$ref,omitempty"` SchemaRef *string `json:"$schema,omitempty"` AdditionalItems *Schema `json:"additionalItems,omitempty"` AdditionalProperties *Schema `json:"additionalProperties,omitempty"` AllOf []*Schema `json:"allOf,omitempty"` AnyOf []*Schema `json:"anyOf,omitempty"` Const *any `json:"const,omitempty"` Contains *Schema `json:"contains,omitempty"` Default *any `json:"default,omitempty"` Definitions *map[string]*Schema `json:"definitions,omitempty"` Dependencies *map[string]*DependencyValue `json:"dependencies,omitempty"` Description *string `json:"description,omitempty"` Else *Schema `json:"else,omitempty"` Enum EnumList `json:"enum,omitempty"` Examples []any `json:"examples,omitempty"` ExclusiveMaximum *float64 `json:"exclusiveMaximum,omitempty"` ExclusiveMinimum *float64 `json:"exclusiveMinimum,omitempty"` Format *Format `json:"format,omitempty"` If *Schema `json:"if,omitempty"` Items *SchemaOrSchemaList `json:"items,omitempty"` MaxItems *int64 `json:"maxItems,omitempty"` MaxLength *int64 `json:"maxLength,omitempty"` MaxProperties *int64 `json:"maxProperties,omitempty"` Maximum *float64 `json:"maximum,omitempty"` MinItems *int64 `json:"minItems,omitempty"` MinLength *int64 `json:"minLength,omitempty"` MinProperties *int64 `json:"minProperties,omitempty"` Minimum *float64 `json:"minimum,omitempty"` MultipleOf *float64 `json:"multipleOf,omitempty"` Not *Schema `json:"not,omitempty"` OneOf []*Schema `json:"oneOf,omitempty"` Pattern *string `json:"pattern,omitempty"` PatternProperties *map[string]*Schema `json:"patternProperties,omitempty"` Properties *map[string]*Schema `json:"properties,omitempty"` PropertyNames *Schema `json:"propertyNames,omitempty"` Required []string `json:"required,omitempty"` Then *Schema `json:"then,omitempty"` Title *string `json:"title,omitempty"` Type PrimitiveTypeList `json:"type,omitempty"` UniqueItems *bool `json:"uniqueItems,omitempty"` // Raw is the raw JSON document that this schema was unmarshaled from, if any. It can be used to // retrieve and set custom properties (such as for extensions to JSON Schema). It is omitted // from the JSON encoding of this value. Raw *json.RawMessage `json:"-"` IsEmpty bool `json:"-"` // the schema is "true" IsNegated bool `json:"-"` // the schema is "false" // Go contains Go-specific extensions that JSON Schema authors can specify. Go *struct { TaggedUnionType bool `json:"taggedUnionType,omitempty"` Pointer bool `json:"pointer,omitempty"` } `json:"!go,omitempty"` }
Schema is a JSON Schema draft-07 document (as specified in [draft-handrews-json-schema-01](https://tools.ietf.org/html/draft-handrews-json-schema-01)).
func (*Schema) IsRequiredProperty ¶
IsRequiredProperty reports whether propertyName is a required property for instances of this schema.
func (*Schema) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*Schema) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type SchemaOrSchemaList ¶
SchemaOrSchemaList represents a value that can be either a valid JSON Schema or an array of valid JSON Schemas.
Exactly 1 field (Schema or Schemas) is set.
The ["items" keyword](https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4.1) is the only keyword that this is used for.
func (*SchemaOrSchemaList) MarshalJSON ¶
func (s *SchemaOrSchemaList) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
func (*SchemaOrSchemaList) UnmarshalJSON ¶
func (s *SchemaOrSchemaList) UnmarshalJSON(data []byte) error
UnmarshalJSON implements json.Unmarshaler.
type Visitor ¶
type Visitor interface {
Visit(schema *Schema, rel []ReferenceToken) (w Visitor)
}
A Visitor's Visit method is invoked for each schema (with the relative reference tokens identifying it) to encountered by Walk. If the result visitor w is not nil, Walk visits each of the subschemas of schema with the visitor w, followed by a call of w.Visit(nil).