schemas

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package schemas defines JSON schema types.

Code borrowed from https://github.com/alecthomas/jsonschema/

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

View Source
const (
	TypeNameString  = "string"
	TypeNameArray   = "array"
	TypeNameNumber  = "number"
	TypeNameInteger = "integer"
	TypeNameObject  = "object"
	TypeNameBoolean = "boolean"
	TypeNameNull    = "null"
	PrefixEnumValue = "enumValues_"
)

Variables

View Source
var (
	ErrCannotResolveSchema      = errors.New("cannot resolve schema")
	ErrCannotLoadSchema         = errors.New("cannot load schema")
	ErrUnsupportedContentType   = errors.New("unsupported content type")
	ErrUnsupportedFileExtension = errors.New("unsupported file extension")
	ErrUnsupportedURL           = errors.New("unsupported URL")
)
View Source
var (
	ErrCannotMergeTypes = fmt.Errorf("cannot merge types")
	ErrEmptyTypesList   = fmt.Errorf("types list is empty")
)
View Source
var (
	ErrEmptyReference       = errors.New("reference is empty")
	ErrUnsupportedRefFormat = errors.New("unsupported $ref format")
	ErrCannotParseRef       = errors.New("cannot parse $ref")
	ErrUnsupportedRefSchema = errors.New("unsupported $ref schema")
	ErrGetRefType           = errors.New("cannot get $ref type")
)

Functions

func CleanNameForSorting

func CleanNameForSorting(name string) string

func IsPrimitiveType

func IsPrimitiveType(t string) bool

func QualifiedFileName

func QualifiedFileName(fileName, parentFileName string, resolveExtensions []string) (string, error)

Types

type CachedLoader

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

func NewCachedLoader

func NewCachedLoader(loader Loader, cache map[string]*Schema) *CachedLoader

func NewDefaultCacheLoader added in v0.0.2

func NewDefaultCacheLoader(resolveExtensions, yamlExtensions []string) *CachedLoader

func (*CachedLoader) Load

func (l *CachedLoader) Load(uri, parentURI string) (*Schema, error)

type Definitions

type Definitions map[string]*Type

Definitions hold schema definitions. http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.26 RFC draft-wright-json-schema-validation-00, section 5.26.

type FileLoader

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

func NewFileLoader

func NewFileLoader(resolveExtensions, yamlExtensions []string) *FileLoader

func (*FileLoader) Load

func (l *FileLoader) Load(fileName, parentFileName string) (*Schema, error)

type GoJSONSchemaExtension

type GoJSONSchemaExtension struct {
	Type       *string  `json:"type,omitempty"`
	Identifier *string  `json:"identifier,omitempty"`
	Nillable   bool     `json:"nillable,omitempty"`
	Imports    []string `json:"imports,omitempty"`
}

type HTTPLoader added in v0.0.2

type HTTPLoader struct {
	YAMLExtensions map[string]bool
}

func NewHTTPLoader added in v0.0.2

func NewHTTPLoader(yamlExtensions []string) *HTTPLoader

func (*HTTPLoader) Load added in v0.0.2

func (l *HTTPLoader) Load(uri, parentURI string) (*Schema, error)

type Loader

type Loader interface {
	Load(uri, parentURI string) (*Schema, error)
}

type MultiLoader

type MultiLoader map[RefType]Loader

func NewDefaultMultiLoader added in v0.0.2

func NewDefaultMultiLoader(resolveExtensions, yamlExtensions []string) MultiLoader

func (MultiLoader) Load

func (l MultiLoader) Load(uri, parentURI string) (*Schema, error)

type ObjectAsType

type ObjectAsType Type

type RefType

type RefType string
const (
	RefTypeFile    RefType = "file"
	RefTypeHTTP    RefType = "http"
	RefTypeHTTPS   RefType = "https"
	RefTypeUnknown RefType = "unknown"
)

func GetRefType

func GetRefType(ref string) (RefType, error)

type Schema

type Schema struct {
	*ObjectAsType
	ID          string      `json:"$id"` // RFC draft-wright-json-schema-01, section-9.2.
	LegacyID    string      `json:"id"`  // RFC draft-wright-json-schema-00, section 4.5.
	Definitions Definitions `json:"$defs,omitempty"`
}

Schema is the root schema.

func FromJSONFile

func FromJSONFile(fileName string) (*Schema, error)

func FromJSONReader

func FromJSONReader(r io.Reader) (*Schema, error)

func FromYAMLFile

func FromYAMLFile(fileName string) (*Schema, error)

func FromYAMLReader

func FromYAMLReader(r io.Reader) (*Schema, error)

func (*Schema) UnmarshalJSON

func (s *Schema) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler for Schema struct.

type SubSchemaType added in v0.0.2

type SubSchemaType string
const (
	SubSchemaTypeAllOf SubSchemaType = "allOf"
	SubSchemaTypeAnyOf SubSchemaType = "anyOf"
	SubSchemaTypeOneOf SubSchemaType = "oneOf"
	SubSchemaTypeNot   SubSchemaType = "not"
)

type Type

type Type struct {
	// RFC draft-wright-json-schema-00.
	Version string `json:"$schema,omitempty"` // Section 6.1.
	Ref     string `json:"$ref,omitempty"`    // Section 7.
	// RFC draft-wright-json-schema-validation-00, section 5.
	MultipleOf           *float64         `json:"multipleOf,omitempty"`           // Section 5.1.
	Maximum              *float64         `json:"maximum,omitempty"`              // Section 5.2.
	ExclusiveMaximum     *any             `json:"exclusiveMaximum,omitempty"`     // Section 5.3. Changed in draft 4.
	Minimum              *float64         `json:"minimum,omitempty"`              // Section 5.4.
	ExclusiveMinimum     *any             `json:"exclusiveMinimum,omitempty"`     // Section 5.5. Changed in draft 4.
	MaxLength            int              `json:"maxLength,omitempty"`            // Section 5.6.
	MinLength            int              `json:"minLength,omitempty"`            // Section 5.7.
	Pattern              string           `json:"pattern,omitempty"`              // Section 5.8.
	AdditionalItems      *Type            `json:"additionalItems,omitempty"`      // Section 5.9.
	Items                *Type            `json:"items,omitempty"`                // Section 5.9.
	MaxItems             int              `json:"maxItems,omitempty"`             // Section 5.10.
	MinItems             int              `json:"minItems,omitempty"`             // Section 5.11.
	UniqueItems          bool             `json:"uniqueItems,omitempty"`          // Section 5.12.
	MaxProperties        int              `json:"maxProperties,omitempty"`        // Section 5.13.
	MinProperties        int              `json:"minProperties,omitempty"`        // Section 5.14.
	Required             []string         `json:"required,omitempty"`             // Section 5.15.
	Properties           map[string]*Type `json:"properties,omitempty"`           // Section 5.16.
	PatternProperties    map[string]*Type `json:"patternProperties,omitempty"`    // Section 5.17.
	AdditionalProperties *Type            `json:"additionalProperties,omitempty"` // Section 5.18.
	Enum                 []interface{}    `json:"enum,omitempty"`                 // Section 5.20.
	Type                 TypeList         `json:"type,omitempty"`                 // Section 5.21.
	// RFC draft-bhutton-json-schema-01, section 10.
	AllOf []*Type `json:"allOf,omitempty"` // Section 10.2.1.1.
	AnyOf []*Type `json:"anyOf,omitempty"` // Section 10.2.1.2.
	OneOf []*Type `json:"oneOf,omitempty"` // Section 10.2.1.3.
	Not   *Type   `json:"not,omitempty"`   // Section 10.2.1.4.
	// RFC draft-wright-json-schema-validation-00, section 6, 7.
	Title       string      `json:"title,omitempty"`       // Section 6.1.
	Description string      `json:"description,omitempty"` // Section 6.1.
	Default     interface{} `json:"default,omitempty"`     // Section 6.2.
	Format      string      `json:"format,omitempty"`      // Section 7.
	// RFC draft-wright-json-schema-hyperschema-00, section 4.
	Media          *Type  `json:"media,omitempty"`          // Section 4.3.
	BinaryEncoding string `json:"binaryEncoding,omitempty"` // Section 4.3.
	// RFC draft-handrews-json-schema-validation-02, section 6.
	DependentRequired map[string][]string `json:"dependentRequired,omitempty"` // Section 6.5.4.
	// RFC draft-handrews-json-schema-validation-02, appendix A.
	Definitions      Definitions      `json:"$defs,omitempty"`
	DependentSchemas map[string]*Type `json:"dependentSchemas,omitempty"`

	// TODO: add correct section where "readOnly" is mentioned in the spec
	//       I'm not sure which section I should put here, but I did notice in the 2020-12 validation schema changelog,
	//       under the "draft-handrews-json-schema-validation-00" item it mentions "readOnly" as having been moved
	//       from hyper-schema to validation meta-data...
	ReadOnly bool `json:"readOnly,omitempty"`

	// ExtGoCustomType is the name of a (qualified or not) custom Go type
	// to use for the field.
	GoJSONSchemaExtension *GoJSONSchemaExtension `json:"goJSONSchema,omitempty"` //nolint:tagliatelle // breaking change

	// Flags.
	Dereferenced bool `json:"-"` // Marks that his type has been dereferenced.
	// contains filtered or unexported fields
}

Type represents a JSON Schema object type.

func AllOf added in v0.0.2

func AllOf(types []*Type) (*Type, error)

func AnyOf added in v0.0.2

func AnyOf(types []*Type) (*Type, error)

func MergeTypes added in v0.0.2

func MergeTypes(types []*Type) (*Type, error)

func (*Type) GetSubSchemaType added in v0.0.2

func (value *Type) GetSubSchemaType() SubSchemaType

func (*Type) GetSubSchemasCount added in v0.0.2

func (value *Type) GetSubSchemasCount() int

func (*Type) IsSubSchemaTypeElem added in v0.0.2

func (value *Type) IsSubSchemaTypeElem() bool

func (*Type) SetSubSchemaType added in v0.0.2

func (value *Type) SetSubSchemaType(sst SubSchemaType)

func (*Type) SetSubSchemaTypeElem added in v0.0.2

func (value *Type) SetSubSchemaTypeElem()

func (*Type) SetSubSchemasCount added in v0.0.2

func (value *Type) SetSubSchemasCount(ssc int)

func (*Type) UnmarshalJSON

func (value *Type) UnmarshalJSON(raw []byte) error

UnmarshalJSON accepts booleans as schemas where `true` is equivalent to `{}` and `false` is equivalent to `{"not": {}}`.

type TypeList

type TypeList []string

TypeList is a list of type names.

func (*TypeList) Equals added in v0.0.2

func (t *TypeList) Equals(b TypeList) bool

func (*TypeList) UnmarshalJSON

func (t *TypeList) UnmarshalJSON(value []byte) error

UnmarshalJSON implements json.Unmarshaler.

Jump to

Keyboard shortcuts

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