jsonschema

package
v0.42.0 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2022 License: Apache-2.0 Imports: 13 Imported by: 4

Documentation

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) MarshalNextJSON added in v0.42.0

func (p AdditionalProperties) MarshalNextJSON(opts json.MarshalOptions, e *json.Encoder) error

MarshalNextJSON implements json.MarshalerV2.

func (*AdditionalProperties) UnmarshalNextJSON added in v0.42.0

func (p *AdditionalProperties) UnmarshalNextJSON(opts json.UnmarshalOptions, d *json.Decoder) error

UnmarshalNextJSON implements json.UnmarshalerV2.

type Discriminator

type Discriminator struct {
	PropertyName string            `json:"propertyName"`
	Mapping      map[string]string `json:"mapping,omitzero"`
}

Discriminator is JSON Schema discriminator description.

type Enum added in v0.38.0

type Enum []json.RawValue

Enum is JSON Schema enum validator description.

func (*Enum) UnmarshalNextJSON added in v0.42.0

func (n *Enum) UnmarshalNextJSON(opts json.UnmarshalOptions, d *json.Decoder) error

UnmarshalNextJSON implements json.UnmarshalerV2.

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 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.RawValue

Num represents JSON number.

func (Num) MarshalNextJSON added in v0.42.0

func (n Num) MarshalNextJSON(opts json.MarshalOptions, e *json.Encoder) error

MarshalNextJSON implements json.MarshalerV2.

func (*Num) UnmarshalNextJSON added in v0.42.0

func (n *Num) UnmarshalNextJSON(opts json.UnmarshalOptions, d *json.Decoder) error

UnmarshalNextJSON implements json.UnmarshalerV2.

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) Resolve added in v0.27.0

func (p *Parser) Resolve(ref string) (*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) MarshalNextJSON added in v0.42.0

func (r RawPatternProperties) MarshalNextJSON(opts json.MarshalOptions, e *json.Encoder) error

MarshalNextJSON implements json.MarshalerV2.

func (*RawPatternProperties) UnmarshalNextJSON added in v0.42.0

func (r *RawPatternProperties) UnmarshalNextJSON(opts json.UnmarshalOptions, d *json.Decoder) error

UnmarshalNextJSON implements json.UnmarshalerV2.

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) MarshalNextJSON added in v0.42.0

func (p RawProperties) MarshalNextJSON(opts json.MarshalOptions, e *json.Encoder) error

MarshalNextJSON implements json.MarshalerV2.

func (*RawProperties) UnmarshalNextJSON added in v0.42.0

func (p *RawProperties) UnmarshalNextJSON(opts json.UnmarshalOptions, d *json.Decoder) error

UnmarshalNextJSON implements json.UnmarshalerV2.

type RawProperty

type RawProperty struct {
	Name   string
	Schema *RawSchema
}

RawProperty is item of RawProperties.

type RawSchema

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

	XAnnotations map[string]json.RawValue `json:",inline"`

	ogenjson.Locator `json:"-"`
}

RawSchema is unparsed JSON Schema.

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 []byte) *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                 []interface{}     // 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

	// 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 []json.RawValue
	// Default schema value.
	Default    interface{}
	DefaultSet bool
}

Schema is a JSON Schema.

func (*Schema) AddExample

func (s *Schema) AddExample(r json.RawValue)

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

	// DepthLimit limits the number of nested references. Default is 1000.
	DepthLimit int

	// 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.

Jump to

Keyboard shortcuts

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