jsonschema

package
v0.51.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2022 License: Apache-2.0 Imports: 15 Imported by: 3

Documentation

Overview

Package jsonschema contains parser for JSON Schema.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdditionalProperties

type AdditionalProperties struct {
	Bool   *bool
	Schema RawSchema
}

AdditionalProperties is JSON Schema additionalProperties validator description.

func (AdditionalProperties) MarshalJSON

func (p AdditionalProperties) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (AdditionalProperties) MarshalYAML added in v0.44.0

func (p AdditionalProperties) MarshalYAML() (any, error)

MarshalYAML implements yaml.Marshaler.

func (*AdditionalProperties) UnmarshalJSON

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

UnmarshalJSON implements json.Unmarshaler.

func (*AdditionalProperties) UnmarshalYAML added in v0.44.0

func (p *AdditionalProperties) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler.

type Default added in v0.43.0

type Default = RawValue

Default is a default value.

type Discriminator

type Discriminator struct {
	// REQUIRED. The name of the property in the payload that will hold the discriminator value.
	PropertyName string `json:"propertyName" yaml:"propertyName"`
	// An object to hold mappings between payload values and schema names or references.
	Mapping map[string]string `json:"mapping,omitempty" yaml:"mapping,omitempty"`

	Common OpenAPICommon `json:"-" yaml:",inline"`
}

Discriminator discriminates types for OneOf, AllOf, AnyOf.

See https://spec.openapis.org/oas/v3.1.0#discriminator-object.

type Enum added in v0.38.0

type Enum []json.RawMessage

Enum is JSON Schema enum validator description.

func (Enum) MarshalYAML added in v0.44.0

func (n Enum) MarshalYAML() (any, error)

MarshalYAML implements yaml.Marshaler.

func (*Enum) UnmarshalYAML added in v0.43.0

func (n *Enum) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler.

type Example added in v0.43.0

type Example = RawValue

Example is an example value.

type Extensions added in v0.49.0

type Extensions map[string]yaml.Node

Extensions map is "^x-" fields list.

func (Extensions) MarshalJSON added in v0.49.0

func (p Extensions) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Extensions) MarshalYAML added in v0.49.0

func (p Extensions) MarshalYAML() (any, error)

MarshalYAML implements yaml.Marshaler.

func (*Extensions) UnmarshalJSON added in v0.49.0

func (p *Extensions) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Extensions) UnmarshalYAML added in v0.49.0

func (p *Extensions) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler.

type ExternalResolver added in v0.36.0

type ExternalResolver interface {
	Get(ctx context.Context, loc string) ([]byte, error)
}

ExternalResolver resolves external links.

type Infer added in v0.41.0

type Infer struct {
	// contains filtered or unexported fields
}

Infer returns a JSON Schema that is inferred from the given JSON.

func (*Infer) Apply added in v0.41.0

func (i *Infer) Apply(data []byte) error

Apply applies given data to the schema state.

func (Infer) Target added in v0.41.0

func (i Infer) Target() RawSchema

Target returns the target schema.

type LocationError added in v0.43.0

type LocationError = location.Error

LocationError is a wrapper for an error that has a location.

type NoExternal added in v0.37.0

type NoExternal struct{}

NoExternal is ExternalResolver that always returns error.

func (NoExternal) Get added in v0.37.0

func (n NoExternal) Get(context.Context, string) ([]byte, error)

Get implements ExternalResolver.

type Num added in v0.16.0

type Num json.RawMessage

Num represents JSON number.

func (Num) MarshalJSON added in v0.17.1

func (n Num) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Num) MarshalYAML added in v0.46.2

func (n Num) MarshalYAML() (any, error)

MarshalYAML implements yaml.Marshaler.

func (*Num) UnmarshalJSON added in v0.16.0

func (n *Num) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Num) UnmarshalYAML added in v0.43.0

func (n *Num) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler.

type OpenAPICommon added in v0.49.0

type OpenAPICommon struct {
	Extensions
	location.Locator
}

OpenAPICommon is common fields for OpenAPI objects.

func (OpenAPICommon) MarshalJSON added in v0.49.0

func (p OpenAPICommon) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (OpenAPICommon) MarshalYAML added in v0.49.0

func (p OpenAPICommon) MarshalYAML() (any, error)

MarshalYAML implements yaml.Marshaler.

func (*OpenAPICommon) UnmarshalJSON added in v0.49.0

func (p *OpenAPICommon) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*OpenAPICommon) UnmarshalYAML added in v0.49.0

func (p *OpenAPICommon) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses JSON schemas.

func NewParser

func NewParser(s Settings) *Parser

NewParser creates new Parser.

func (*Parser) Parse

func (p *Parser) Parse(schema *RawSchema) (*Schema, error)

Parse parses given RawSchema and returns parsed Schema.

func (*Parser) ParseWithContext added in v0.48.0

func (p *Parser) ParseWithContext(schema *RawSchema, ctx *jsonpointer.ResolveCtx) (*Schema, error)

ParseWithContext parses given RawSchema and returns parsed Schema.

func (*Parser) Resolve added in v0.27.0

func (p *Parser) Resolve(ref string, ctx *jsonpointer.ResolveCtx) (*Schema, error)

Resolve resolves Schema by given ref.

type PatternProperty added in v0.23.0

type PatternProperty struct {
	Pattern *regexp.Regexp
	Schema  *Schema
}

PatternProperty is a property pattern.

type Property

type Property struct {
	Name        string  // Property name.
	Description string  // Property description.
	Schema      *Schema // Property schema.
	Required    bool    // Whether the field is required or not.
}

Property is a JSON Schema Object property.

type RawPatternProperties added in v0.23.0

type RawPatternProperties []RawPatternProperty

RawPatternProperties is unparsed JSON Schema patternProperties validator description.

func (RawPatternProperties) MarshalJSON added in v0.23.0

func (p RawPatternProperties) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (RawPatternProperties) MarshalYAML added in v0.44.0

func (p RawPatternProperties) MarshalYAML() (any, error)

MarshalYAML implements yaml.Marshaler.

func (*RawPatternProperties) UnmarshalJSON added in v0.23.0

func (p *RawPatternProperties) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*RawPatternProperties) UnmarshalYAML added in v0.44.0

func (p *RawPatternProperties) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler.

type RawPatternProperty added in v0.23.0

type RawPatternProperty struct {
	Pattern string
	Schema  *RawSchema
}

RawPatternProperty is item of RawPatternProperties.

type RawProperties

type RawProperties []RawProperty

RawProperties is unparsed JSON Schema properties validator description.

func (RawProperties) MarshalJSON

func (p RawProperties) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (RawProperties) MarshalYAML added in v0.44.0

func (p RawProperties) MarshalYAML() (any, error)

MarshalYAML implements yaml.Marshaler.

func (*RawProperties) UnmarshalJSON

func (p *RawProperties) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*RawProperties) UnmarshalYAML added in v0.44.0

func (p *RawProperties) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler.

type RawProperty

type RawProperty struct {
	Name   string
	Schema *RawSchema
}

RawProperty is item of RawProperties.

type RawSchema

type RawSchema struct {
	Ref                  string                `json:"$ref,omitempty" yaml:"$ref,omitempty"`
	Summary              string                `json:"summary,omitempty" yaml:"summary,omitempty"`
	Description          string                `json:"description,omitempty" yaml:"description,omitempty"`
	Type                 string                `json:"type,omitempty" yaml:"type,omitempty"`
	Format               string                `json:"format,omitempty" yaml:"format,omitempty"`
	Properties           RawProperties         `json:"properties,omitempty" yaml:"properties,omitempty"`
	AdditionalProperties *AdditionalProperties `json:"additionalProperties,omitempty" yaml:"additionalProperties,omitempty"`
	PatternProperties    RawPatternProperties  `json:"patternProperties,omitempty" yaml:"patternProperties,omitempty"`
	Required             []string              `json:"required,omitempty" yaml:"required,omitempty"`
	Items                *RawSchema            `json:"items,omitempty" yaml:"items,omitempty"`
	Nullable             bool                  `json:"nullable,omitempty" yaml:"nullable,omitempty"`
	AllOf                []*RawSchema          `json:"allOf,omitempty" yaml:"allOf,omitempty"`
	OneOf                []*RawSchema          `json:"oneOf,omitempty" yaml:"oneOf,omitempty"`
	AnyOf                []*RawSchema          `json:"anyOf,omitempty" yaml:"anyOf,omitempty"`
	Enum                 Enum                  `json:"enum,omitempty" yaml:"enum,omitempty"`
	MultipleOf           Num                   `json:"multipleOf,omitempty" yaml:"multipleOf,omitempty"`
	Maximum              Num                   `json:"maximum,omitempty" yaml:"maximum,omitempty"`
	ExclusiveMaximum     bool                  `json:"exclusiveMaximum,omitempty" yaml:"exclusiveMaximum,omitempty"`
	Minimum              Num                   `json:"minimum,omitempty" yaml:"minimum,omitempty"`
	ExclusiveMinimum     bool                  `json:"exclusiveMinimum,omitempty" yaml:"exclusiveMinimum,omitempty"`
	MaxLength            *uint64               `json:"maxLength,omitempty" yaml:"maxLength,omitempty"`
	MinLength            *uint64               `json:"minLength,omitempty" yaml:"minLength,omitempty"`
	Pattern              string                `json:"pattern,omitempty" yaml:"pattern,omitempty"`
	MaxItems             *uint64               `json:"maxItems,omitempty" yaml:"maxItems,omitempty"`
	MinItems             *uint64               `json:"minItems,omitempty" yaml:"minItems,omitempty"`
	UniqueItems          bool                  `json:"uniqueItems,omitempty" yaml:"uniqueItems,omitempty"`
	MaxProperties        *uint64               `json:"maxProperties,omitempty" yaml:"maxProperties,omitempty"`
	MinProperties        *uint64               `json:"minProperties,omitempty" yaml:"minProperties,omitempty"`
	Default              Default               `json:"default,omitempty" yaml:"default,omitempty"`
	Deprecated           bool                  `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
	ContentEncoding      string                `json:"contentEncoding,omitempty" yaml:"contentEncoding,omitempty"`
	ContentMediaType     string                `json:"contentMediaType,omitempty" yaml:"contentMediaType,omitempty"`

	Discriminator *Discriminator `json:"discriminator,omitempty" yaml:"discriminator,omitempty"`
	XML           *XML           `json:"xml,omitempty" yaml:"xml,omitempty"`
	Example       Example        `json:"example,omitempty" yaml:"example,omitempty"`

	Common OpenAPICommon `json:"-" yaml:",inline"`
}

RawSchema is unparsed JSON Schema.

type RawValue added in v0.43.0

type RawValue json.RawMessage

RawValue is a raw JSON value.

func (RawValue) MarshalJSON added in v0.43.1

func (n RawValue) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (RawValue) MarshalYAML added in v0.44.0

func (n RawValue) MarshalYAML() (any, error)

MarshalYAML implements yaml.Marshaler.

func (*RawValue) UnmarshalJSON added in v0.43.1

func (n *RawValue) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*RawValue) UnmarshalYAML added in v0.43.0

func (n *RawValue) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler.

type ReferenceResolver

type ReferenceResolver interface {
	ResolveReference(ref string) (*RawSchema, error)
}

ReferenceResolver resolves JSON schema references.

type RootResolver added in v0.22.0

type RootResolver struct {
	// contains filtered or unexported fields
}

RootResolver is ReferenceResolver implementation.

func NewRootResolver added in v0.22.0

func NewRootResolver(root *yaml.Node) *RootResolver

NewRootResolver creates new RootResolver.

func (*RootResolver) ResolveReference added in v0.22.0

func (r *RootResolver) ResolveReference(ref string) (rawSchema *RawSchema, err error)

ResolveReference implements ReferenceResolver.

type Schema

type Schema struct {
	XOgenName string // Annotation to set type name.

	Ref string // Whether schema is referenced.

	Type             SchemaType
	Format           string // Schema format, optional.
	ContentEncoding  string
	ContentMediaType string

	Summary     string // Schema summary from Reference Object, optional.
	Description string // Schema description, optional.
	Deprecated  bool

	Item                 *Schema           // Only for Array and Object with additional properties.
	AdditionalProperties *bool             // Whether Object has additional properties.
	PatternProperties    []PatternProperty // Only for Object.
	Enum                 []any             // Only for Enum.
	Properties           []Property        // Only for Object.

	Nullable bool // Whether schema is nullable or not. Any types.

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

	Discriminator *Discriminator
	XML           *XML

	// Numeric validation (Integer, Number).
	Maximum          Num
	ExclusiveMaximum bool
	Minimum          Num
	ExclusiveMinimum bool
	MultipleOf       Num

	// String validation.
	MaxLength *uint64
	MinLength *uint64
	Pattern   string

	// Array validation.
	MaxItems    *uint64
	MinItems    *uint64
	UniqueItems bool

	// Object validation.
	MaxProperties *uint64
	MinProperties *uint64

	Examples []Example
	// Default schema value.
	Default    any
	DefaultSet bool

	location.Locator `json:"-" yaml:"-"`
}

Schema is a JSON Schema.

func (*Schema) AddExample

func (s *Schema) AddExample(r Example)

AddExample adds example for this Schema.

type SchemaType

type SchemaType string

SchemaType is a JSON Schema type.

const (
	// Empty is empty (unset) schema type.
	Empty SchemaType = "" // OneOf, AnyOf, AllOf.
	// Object is "object" schema type.
	Object SchemaType = "object"
	// Array is "array" schema type.
	Array SchemaType = "array"
	// Integer is "integer" schema type.
	Integer SchemaType = "integer"
	// Number is "number" schema type.
	Number SchemaType = "number"
	// String is "string" schema type.
	String SchemaType = "string"
	// Boolean is "boolean" schema type.
	Boolean SchemaType = "boolean"
	// Null is "null" schema type.
	Null SchemaType = "null"
)

type Settings

type Settings struct {
	// External is external resolver. If nil, NoExternal resolver is used.
	External ExternalResolver

	// Resolver is a root resolver.
	Resolver ReferenceResolver

	// Filename is a name of the file being parsed.
	//
	// Used for error messages.
	Filename string

	// Enables type inference.
	//
	// For example:
	//
	//	{
	//		"items": {
	//			"type": "string"
	//		}
	//	}
	//
	// In that case schemaParser will handle that schema as "array" schema, because it has "items" field.
	InferTypes bool
}

Settings is parser settings.

type XML added in v0.44.0

type XML struct {
	// Replaces the name of the element/attribute used for the described schema property.
	//
	// When defined within items, it will affect the name of the individual XML elements within the list.
	//
	// When defined alongside type being array (outside the items), it will affect the wrapping element
	// and only if wrapped is true.
	//
	// If wrapped is false, it will be ignored.
	Name string `json:"name,omitempty" yaml:"name,omitempty"`
	// The URI of the namespace definition.
	//
	// This MUST be in the form of an absolute URI.
	Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
	// The prefix to be used for the name.
	Prefix string `json:"prefix,omitempty" yaml:"prefix,omitempty"`
	// Declares whether the property definition translates to an attribute instead of an element.
	//
	// Default value is false.
	Attribute bool `json:"attribute,omitempty" yaml:"attribute,omitempty"`
	// MAY be used only for an array definition. Signifies whether the array is wrapped
	// (for example, `<books><book/><book/></books>`) or unwrapped (`<book/><book/>`).
	//
	// The definition takes effect only when defined alongside type being array (outside the items).
	//
	// Default value is false.
	Wrapped bool `json:"wrapped,omitempty" yaml:"wrapped,omitempty"`
}

XML is a metadata object that allows for more fine-tuned XML model definitions.

See https://spec.openapis.org/oas/v3.1.0#xml-object.

Jump to

Keyboard shortcuts

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