jsonschema

package
v0.0.0-...-18043a3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 29, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdditionalProperties

type AdditionalProperties Schema

AdditionalProperties handles additional properties present in the JSON schema.

func (*AdditionalProperties) UnmarshalJSON

func (ap *AdditionalProperties) UnmarshalJSON(data []byte) error

UnmarshalJSON handles unmarshalling AdditionalProperties from JSON.

type Schema

type Schema struct {
	// SchemaType identifies the schema version.
	// http://json-schema.org/draft-07/json-schema-core.html#rfc.section.7
	SchemaType string `json:"$schema"`

	// ID{04,06} is the schema URI identifier.
	// http://json-schema.org/draft-07/json-schema-core.html#rfc.section.8.2
	ID04 string `json:"id"`  // up to draft-04
	ID06 string `json:"$id"` // from draft-06 onwards

	// Title and Description state the intent of the schema.
	Title       string
	Description string

	// TypeValue is the schema instance type.
	// http://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.1.1
	TypeValue interface{} `json:"type"`

	// Definitions are inline re-usable schemas.
	// http://json-schema.org/draft-07/json-schema-validation.html#rfc.section.9
	Definitions map[string]*Schema

	// Properties, Required and AdditionalProperties describe an object's child instances.
	// http://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.5
	Properties map[string]*Schema
	Required   []string

	// "additionalProperties": {...}
	AdditionalProperties *AdditionalProperties

	// "additionalProperties": false
	AdditionalPropertiesBool *bool `json:"-"`

	AnyOf []*Schema
	AllOf []*Schema
	OneOf []*Schema

	// Default can be used to supply a default JSON value associated with a particular schema.
	// http://json-schema.org/draft-07/json-schema-validation.html#rfc.section.10.2
	Default interface{}

	// Examples ...
	// http://json-schema.org/draft-07/json-schema-validation.html#rfc.section.10.4
	Examples []interface{}

	// Reference is a URI reference to a schema.
	// http://json-schema.org/draft-07/json-schema-core.html#rfc.section.8
	Reference string `json:"$ref"`

	// Items represents the types that are permitted in the array.
	// http://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.4
	Items *Schema

	// NameCount is the number of times the instance name was encountered across the schema.
	NameCount int `json:"-" `

	// Parent schema
	Parent *Schema `json:"-" `

	// Key of this schema i.e. { "JSONKey": { "type": "object", ....
	JSONKey string `json:"-" `

	// path element - for creating a path by traversing back to the root element
	PathElement string `json:"-"`

	// calculated struct name of this object, cached here
	GeneratedType string `json:"-"`
}

Schema represents JSON schema.

func Parse

func Parse(schema string, uri *url.URL) (*Schema, error)

Parse parses a JSON schema from a string.

func ParseWithSchemaKeyRequired

func ParseWithSchemaKeyRequired(schema string, uri *url.URL, schemaKeyRequired bool) (*Schema, error)

ParseWithSchemaKeyRequired parses a JSON schema from a string with a flag to set whether the schema key is required.

func (*Schema) FixMissingTypeValue

func (schema *Schema) FixMissingTypeValue()

FixMissingTypeValue is backwards compatible, guessing the users intention when they didn't specify a type.

func (*Schema) GetRoot

func (schema *Schema) GetRoot() *Schema

GetRoot returns the root schema.

func (*Schema) ID

func (schema *Schema) ID() string

ID returns the schema URI id.

func (*Schema) Init

func (schema *Schema) Init()

Init schema.

func (*Schema) IsRoot

func (schema *Schema) IsRoot() bool

IsRoot returns true when the schema is the root.

func (*Schema) MultiType

func (schema *Schema) MultiType() ([]string, bool)

MultiType returns "type" as an array

func (*Schema) Type

func (schema *Schema) Type() (firstOrDefault string, multiple bool)

Type returns the type which is permitted or an empty string if the type field is missing. The 'type' field in JSON schema also allows for a single string value or an array of strings. Examples:

"a" => "a", false
[] => "", false
["a"] => "a", false
["a", "b"] => "a", true

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL