jsonschema

package
v1.0.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	XEnumLabels    = `x-enum-labels`
	XGoType        = `x-go-type`
	XGoVendorType  = `x-go-vendor-type`
	XGoStarLevel   = `x-go-star-level`
	XGoFieldName   = `x-go-field-name`
	XTagValidate   = `x-tag-validate`
	XPatternErrMsg = `x-pattern-err-msg`
)

Variables

View Source
var (
	ErrInvalidJSONSchemaObject = errors.New("invalid json schema object")
	ErrInvalidJSONSchemaType   = errors.New("invalid json schema type")
)

Functions

func Print

func Print(w io.Writer, fn func(p Printer))

func Unmarshal

func Unmarshal(data []byte, v any) error

Types

type Anchor

type Anchor string

Anchor openapi:anchor

func (Anchor) OpenAPISchema

func (Anchor) OpenAPISchema() Schema

func (*Anchor) UnmarshalText

func (s *Anchor) UnmarshalText(text []byte) error

type AnyType

type AnyType struct {
	Core
	Metadata
}

func Any

func Any() *AnyType

func (AnyType) PrintTo

func (AnyType) PrintTo(w io.Writer, optionFns ...SchemaPrintOption)

type ArrayType

type ArrayType struct {
	Type  string `json:"type,omitzero"`
	Items Schema `json:"items,omitzero"`

	// validate
	MaxItems    *uint64 `json:"maxItems,omitzero"`
	MinItems    *uint64 `json:"minItems,omitzero"`
	UniqueItems *bool   `json:"uniqueItems,omitzero"`

	Core
	Metadata
}

func ArrayOf

func ArrayOf(items Schema) *ArrayType

func (ArrayType) PrintTo

func (s ArrayType) PrintTo(w io.Writer, options ...SchemaPrintOption)

type BooleanType

type BooleanType struct {
	Type string `json:"type,omitzero"`

	Core
	Metadata
}

func Boolean

func Boolean() *BooleanType

func (BooleanType) PrintTo

func (BooleanType) PrintTo(w io.Writer, _ ...SchemaPrintOption)

type CanSwaggerDoc

type CanSwaggerDoc interface {
	SwaggerDoc() map[string]string
}

CanSwaggerDoc interface of k8s pkgs

type Core

type Core struct {

	// default https://json-schema.org/draft/2020-12/schema
	Schema *URI `json:"$schema,omitzero"`

	ID         *URIRef      `json:"$id,omitzero"`
	Comment    string       `json:"$comment,omitzero"`
	Vocabulary map[URI]bool `json:"$vocabulary,omitzero"`

	// https://json-schema.org/understanding-json-schema/structuring#anchor
	Anchor string `json:"$anchor,omitzero"`

	// for generics type
	// https://json-schema.org/blog/posts/dynamicref-and-generics
	DynamicAnchor string `json:"$dynamicAnchor,omitzero"`

	Defs map[string]Schema `json:"$defs,omitzero"`
}

func (*Core) GetCore

func (core *Core) GetCore() *Core

type Discriminator

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

type EnumType

type EnumType struct {
	Enum []any `json:"enum"`

	Core
	Metadata
}

func (EnumType) PrintTo

func (t EnumType) PrintTo(w io.Writer, _ ...SchemaPrintOption)

type Ext

type Ext struct {
	Extensions map[string]any `json:",inline"`
}

func (*Ext) AddExtension

func (v *Ext) AddExtension(key string, value any)

func (*Ext) DeepCopy

func (v *Ext) DeepCopy() *Ext

func (*Ext) DeepCopyInto

func (v *Ext) DeepCopyInto(out *Ext)

func (*Ext) GetExtension

func (v *Ext) GetExtension(key string) (any, bool)

func (Ext) Merge

func (v Ext) Merge(m Ext) Ext

type GoEnumValues

type GoEnumValues interface {
	EnumValues() []any
}

type GoTaggedUnionType

type GoTaggedUnionType = validation.UnionType

type GoUnionType

type GoUnionType interface {
	OneOf() []any
}

type IntersectionType

type IntersectionType struct {
	AllOf []Schema `json:"allOf"`

	Core
	Metadata
}

func AllOf

func AllOf(schemas ...Schema) *IntersectionType

func (IntersectionType) PrintTo

func (t IntersectionType) PrintTo(w io.Writer, options ...SchemaPrintOption)

type Metadata

type Metadata struct {
	Title       string `json:"title,omitzero"`
	Description string `json:"description,omitzero"`
	Default     any    `json:"default,omitzero"`
	WriteOnly   *bool  `json:"writeOnly,omitzero"`
	ReadOnly    *bool  `json:"readOnly,omitzero"`
	Examples    []any  `json:"examples,omitzero"`
	Deprecated  *bool  `json:"deprecated,omitzero"`

	Ext
}

func (*Metadata) DeepCopy

func (v *Metadata) DeepCopy() *Metadata

func (*Metadata) DeepCopyInto

func (v *Metadata) DeepCopyInto(out *Metadata)

func (*Metadata) GetMetadata

func (v *Metadata) GetMetadata() *Metadata

type NullType

type NullType struct {
	Type string `json:"type,omitzero"`

	Core
	Metadata
}

func (NullType) PrintTo

func (NullType) PrintTo(w io.Writer, optionFns ...SchemaPrintOption)

type NumberType

type NumberType struct {
	Type string `json:"type,omitzero"`

	// validate
	MultipleOf       *float64 `json:"multipleOf,omitzero"`
	Maximum          *float64 `json:"maximum,omitzero"`
	ExclusiveMaximum *float64 `json:"exclusiveMaximum,omitzero"`
	Minimum          *float64 `json:"minimum,omitzero"`
	ExclusiveMinimum *float64 `json:"exclusiveMinimum,omitzero"`

	Core
	Metadata
}

func (NumberType) PrintTo

func (t NumberType) PrintTo(w io.Writer, optionFns ...SchemaPrintOption)

type ObjectType

type ObjectType struct {
	Type string `json:"type,omitzero"`

	Properties           Props  `json:"properties,omitzero"`
	PropertyNames        Schema `json:"propertyNames,omitzero"`
	AdditionalProperties Schema `json:"additionalProperties,omitzero"`

	// validate
	Required      []string `json:"required,omitzero"`
	MaxProperties *uint64  `json:"maxProperties,omitzero"`
	MinProperties *uint64  `json:"minProperties,omitzero"`

	Core
	Metadata
}

func MapOf

func MapOf(v Schema) *ObjectType

func ObjectOf

func ObjectOf(props map[string]Schema, required ...string) *ObjectType

func RecordOf

func RecordOf(k, v Schema) *ObjectType

func (ObjectType) PrintTo

func (t ObjectType) PrintTo(w io.Writer, optionFns ...SchemaPrintOption)

func (*ObjectType) SetProperty

func (t *ObjectType) SetProperty(name string, schema Schema, required bool)

type OpenAPISchemaFormatGetter

type OpenAPISchemaFormatGetter interface {
	OpenAPISchemaFormat() string
}

type OpenAPISchemaGetter

type OpenAPISchemaGetter interface {
	OpenAPISchema() Schema
}

type OpenAPISchemaTypeGetter

type OpenAPISchemaTypeGetter interface {
	OpenAPISchemaType() []string
}

type Payload

type Payload struct {
	Schema
}

func (Payload) MarshalJSON

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

func (*Payload) UnmarshalJSON

func (p *Payload) UnmarshalJSON(data []byte) (err error)

type Printer

type Printer interface {
	io.Writer

	PrintFrom(pt PrinterTo, optionFns ...SchemaPrintOption)
	Print(values ...any)
	Printf(fmt string, values ...any)

	Indent() func()
	Return()
	PrintDoc(desc string)
}

type PrinterTo

type PrinterTo interface {
	PrintTo(w io.Writer, optionFns ...SchemaPrintOption)
}

type Props

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

func (*Props) Delete

func (p *Props) Delete(key string) (didDelete bool)

func (*Props) Get

func (p *Props) Get(key string) (Schema, bool)

func (Props) IsZero

func (p Props) IsZero() bool

func (*Props) KeyValues

func (p *Props) KeyValues() iter.Seq2[string, Schema]

func (*Props) Len

func (p *Props) Len() int

func (Props) MarshalJSONTo

func (p Props) MarshalJSONTo(encoder *jsontext.Encoder) error

func (*Props) Set

func (p *Props) Set(key string, value Schema) bool

func (*Props) UnmarshalJSONFrom

func (p *Props) UnmarshalJSONFrom(d *jsontext.Decoder) error

type RefType

type RefType struct {
	Ref        *URIRef `json:"$ref,omitzero"`
	DynamicRef *URIRef `json:"$dynamicRef,omitzero"`

	Core
	Metadata
}

func (RefType) PrintTo

func (t RefType) PrintTo(w io.Writer, _ ...SchemaPrintOption)

func (RefType) RefName

func (t RefType) RefName() string

type Schema

type Schema interface {
	GetCore() *Core
	GetMetadata() *Metadata
	PrintTo(w io.Writer, options ...SchemaPrintOption)
}

func Integer

func Integer() Schema

func Long

func Long() Schema

type SchemaPrintOption

type SchemaPrintOption func(o *printOption)

type StringType

type StringType struct {
	Type   string `json:"type,omitzero"`
	Format string `json:"format,omitzero"`

	MaxLength *uint64 `json:"maxLength,omitzero"`
	MinLength *uint64 `json:"minLength,omitzero"`
	Pattern   string  `json:"pattern,omitzero"`

	Core
	Metadata
}

func Binary

func Binary() *StringType

func Bytes

func Bytes() *StringType

func String

func String() *StringType

func (StringType) PrintTo

func (t StringType) PrintTo(w io.Writer, _ ...SchemaPrintOption)

type URI

type URI url.URL

URI uri

func (URI) MarshalText

func (u URI) MarshalText() ([]byte, error)

func (*URI) UnmarshalText

func (u *URI) UnmarshalText(b []byte) error

type URIRef

type URIRef url.URL

URIRef uri-reference

func ParseURIRef

func ParseURIRef(u string) (*URIRef, error)

func (URIRef) MarshalText

func (u URIRef) MarshalText() ([]byte, error)

func (*URIRef) RefName

func (u *URIRef) RefName() string

func (*URIRef) UnmarshalText

func (u *URIRef) UnmarshalText(b []byte) error

type UnionType

type UnionType struct {
	OneOf         []Schema       `json:"oneOf"`
	Discriminator *Discriminator `json:"discriminator,omitzero"`

	Core
	Metadata
}

func OneOf

func OneOf(schemas ...Schema) *UnionType

func (UnionType) PrintTo

func (t UnionType) PrintTo(w io.Writer, optionFns ...SchemaPrintOption)

type ValidationSchema

type ValidationSchema interface {
	PatchValidation(any) error
}

Jump to

Keyboard shortcuts

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