jsonschema

package
v0.0.0-...-bbe6032 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

jsonschema

This directory contains code for reading, writing, and manipulating JSON schemas.

Documentation

Overview

Package jsonschema supports the reading, writing, and manipulation of JSON Schemas.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(node *yaml.Node) string

Render renders a yaml.Node as JSON

Types

type NamedSchema

type NamedSchema struct {
	Name  string
	Value *Schema
}

NamedSchema is a name-value pair that is used to emulate maps with ordered keys.

func NewNamedSchema

func NewNamedSchema(name string, value *Schema) *NamedSchema

NewNamedSchema creates and returns a new object

type NamedSchemaOrStringArray

type NamedSchemaOrStringArray struct {
	Name  string
	Value *SchemaOrStringArray
}

NamedSchemaOrStringArray is a name-value pair that is used to emulate maps with ordered keys.

type Schema

type Schema struct {
	Schema    *string // $schema
	ID        *string // id keyword used for $ref resolution scope
	Ref       *string // $ref, i.e. JSON Pointers
	ReadOnly  *bool
	WriteOnly *bool

	// http://json-schema.org/latest/json-schema-validation.html
	// 5.1.  Validation keywords for numeric instances (number and integer)
	MultipleOf       *SchemaNumber
	Maximum          *SchemaNumber
	ExclusiveMaximum *bool
	Minimum          *SchemaNumber
	ExclusiveMinimum *bool

	// 5.2.  Validation keywords for strings
	MaxLength *int64
	MinLength *int64
	Pattern   *string

	// 5.3.  Validation keywords for arrays
	AdditionalItems *SchemaOrBoolean
	Items           *SchemaOrSchemaArray
	MaxItems        *int64
	MinItems        *int64
	UniqueItems     *bool

	// 5.4.  Validation keywords for objects
	MaxProperties        *int64
	MinProperties        *int64
	Required             *[]string
	AdditionalProperties *SchemaOrBoolean
	Properties           *[]*NamedSchema
	PatternProperties    *[]*NamedSchema
	Dependencies         *[]*NamedSchemaOrStringArray

	// 5.5.  Validation keywords for any instance type
	Enumeration *[]SchemaEnumValue
	Type        *StringOrStringArray
	AllOf       *[]*Schema
	AnyOf       *[]*Schema
	OneOf       *[]*Schema
	Not         *Schema
	Definitions *[]*NamedSchema

	// 6.  Metadata keywords
	Title       *string
	Description *string
	Default     *yaml.Node

	// 7.  Semantic validation with "format"
	Format *string
}

The Schema struct models a JSON Schema and, because schemas are defined hierarchically, contains many references to itself. All fields are pointers and are nil if the associated values are not specified.

func NewBaseSchema

func NewBaseSchema() (schema *Schema, err error)

NewBaseSchema builds a schema object from an embedded json representation.

func NewSchemaFromFile

func NewSchemaFromFile(filename string) (schema *Schema, err error)

NewSchemaFromFile reads a schema from a file. Currently this assumes that schemas are stored in the source distribution of this project.

func NewSchemaFromObject

func NewSchemaFromObject(jsonData *yaml.Node) *Schema

NewSchemaFromObject constructs a schema from a parsed JSON object. Due to the complexity of the schema representation, this is a custom reader and not the standard Go JSON reader (encoding/json).

func (*Schema) AddProperty

func (s *Schema) AddProperty(name string, property *Schema)

AddProperty adds a named property.

func (*Schema) CopyOfficialSchemaProperties

func (schema *Schema) CopyOfficialSchemaProperties(names []string)

CopyOfficialSchemaProperties copies named properties from the official JSON Schema definition

func (*Schema) CopyOfficialSchemaProperty

func (schema *Schema) CopyOfficialSchemaProperty(name string)

CopyOfficialSchemaProperty copies a named property from the official JSON Schema definition

func (*Schema) CopyProperties

func (schema *Schema) CopyProperties(source *Schema)

CopyProperties copies all non-nil properties from the source Schema to the schema Schema.

func (*Schema) DefinitionWithName

func (s *Schema) DefinitionWithName(name string) *Schema

DefinitionWithName returns the selected element.

func (*Schema) IsEmpty

func (schema *Schema) IsEmpty() bool

IsEmpty returns true if no members of the Schema are specified.

func (*Schema) IsEqual

func (schema *Schema) IsEqual(schema2 *Schema) bool

IsEqual returns true if two schemas are equal.

func (*Schema) JSONString

func (schema *Schema) JSONString() string

JSONString returns a json representation of a schema.

func (*Schema) PatternPropertyWithName

func (s *Schema) PatternPropertyWithName(name string) *Schema

PatternPropertyWithName returns the selected element.

func (*Schema) PropertyWithName

func (s *Schema) PropertyWithName(name string) *Schema

PropertyWithName returns the selected element.

func (*Schema) ResolveAllOfs

func (schema *Schema) ResolveAllOfs()

ResolveAllOfs replaces "allOf" elements by merging their properties into the parent Schema.

func (*Schema) ResolveAnyOfs

func (schema *Schema) ResolveAnyOfs()

ResolveAnyOfs replaces all "anyOf" elements with "oneOf".

func (*Schema) ResolveRefs

func (schema *Schema) ResolveRefs()

ResolveRefs resolves "$ref" elements in a Schema and its children. But if a reference refers to an object type, is inside a oneOf, or contains a oneOf, the reference is kept and we expect downstream tools to separately model these referenced schemas.

func (*Schema) String

func (schema *Schema) String() string

Returns a string representation of a Schema.

func (*Schema) TypeIs

func (schema *Schema) TypeIs(typeName string) bool

TypeIs returns true if the Type of a Schema includes the specified type

type SchemaEnumValue

type SchemaEnumValue struct {
	String *string
	Bool   *bool
}

SchemaEnumValue represents a value that can be part of an enumeration in a Schema.

type SchemaNumber

type SchemaNumber struct {
	Integer *int64
	Float   *float64
}

SchemaNumber represents a value that can be either an Integer or a Float.

func NewSchemaNumberWithFloat

func NewSchemaNumberWithFloat(f float64) *SchemaNumber

NewSchemaNumberWithFloat creates and returns a new object

func NewSchemaNumberWithInteger

func NewSchemaNumberWithInteger(i int64) *SchemaNumber

NewSchemaNumberWithInteger creates and returns a new object

type SchemaOperation

type SchemaOperation func(schema *Schema, context string)

SchemaOperation represents a function that can be applied to a Schema.

type SchemaOrBoolean

type SchemaOrBoolean struct {
	Schema  *Schema
	Boolean *bool
}

SchemaOrBoolean represents a value that can be either a Schema or a Boolean.

func NewSchemaOrBooleanWithBoolean

func NewSchemaOrBooleanWithBoolean(b bool) *SchemaOrBoolean

NewSchemaOrBooleanWithBoolean creates and returns a new object

func NewSchemaOrBooleanWithSchema

func NewSchemaOrBooleanWithSchema(s *Schema) *SchemaOrBoolean

NewSchemaOrBooleanWithSchema creates and returns a new object

type SchemaOrSchemaArray

type SchemaOrSchemaArray struct {
	Schema      *Schema
	SchemaArray *[]*Schema
}

SchemaOrSchemaArray represents a value that can be either a Schema or an Array of Schemas.

func NewSchemaOrSchemaArrayWithSchema

func NewSchemaOrSchemaArrayWithSchema(s *Schema) *SchemaOrSchemaArray

NewSchemaOrSchemaArrayWithSchema creates and returns a new object

func NewSchemaOrSchemaArrayWithSchemaArray

func NewSchemaOrSchemaArrayWithSchemaArray(a []*Schema) *SchemaOrSchemaArray

NewSchemaOrSchemaArrayWithSchemaArray creates and returns a new object

type SchemaOrStringArray

type SchemaOrStringArray struct {
	Schema      *Schema
	StringArray *[]string
}

SchemaOrStringArray represents a value that can be either a Schema or an Array of Strings.

type StringOrStringArray

type StringOrStringArray struct {
	String      *string
	StringArray *[]string
}

StringOrStringArray represents a value that can be either a String or an Array of Strings.

func NewStringOrStringArrayWithString

func NewStringOrStringArrayWithString(s string) *StringOrStringArray

NewStringOrStringArrayWithString creates and returns a new object

func NewStringOrStringArrayWithStringArray

func NewStringOrStringArrayWithStringArray(a []string) *StringOrStringArray

NewStringOrStringArrayWithStringArray creates and returns a new object

func (*StringOrStringArray) Description

func (s *StringOrStringArray) Description() string

Description returns a string representation of a string or string array.

Jump to

Keyboard shortcuts

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