jsonschema

package
v0.0.0-...-9a508a9 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrSchemaLoad = errors.New("remote schemas are not supported")
View Source
var ErrSchemaNotFound = errors.New("schema not found in validator")
View Source
var ErrUnknowType = errors.New("unable to create jsonschema for type")

Functions

func JSONFieldName

func JSONFieldName(f reflect.StructField) string

Types

type ArrayBuilder

type ArrayBuilder struct {
	Schema *openapi.Schema
}

ArrayBuilder provides functions for array related options on the schema.

func (ArrayBuilder) Build

func (a ArrayBuilder) Build() Schema

func (ArrayBuilder) MaxContains

func (a ArrayBuilder) MaxContains(n int) ArrayBuilder

func (ArrayBuilder) MaxItems

func (a ArrayBuilder) MaxItems(n int) ArrayBuilder

func (ArrayBuilder) MinContains

func (a ArrayBuilder) MinContains(n int) ArrayBuilder

func (ArrayBuilder) MinItems

func (a ArrayBuilder) MinItems(n int) ArrayBuilder

func (ArrayBuilder) UniqueItems

func (a ArrayBuilder) UniqueItems() ArrayBuilder

type Builder

Builder provides functions to build a json schema.

func NewBuilder

func NewBuilder() Builder

NewBuilder returns an empty Builder.

func (Builder) Build

func (b Builder) Build() Schema

func (Builder) Const

func (b Builder) Const(value string) Builder

func (Builder) Default

func (b Builder) Default(value any) Builder

func (Builder) Deprecated

func (b Builder) Deprecated(value bool) Builder

func (Builder) Description

func (b Builder) Description(desc string) Builder

func (Builder) Enum

func (b Builder) Enum(values ...any) Builder

func (Builder) Examples

func (b Builder) Examples(values ...any) Builder

func (Builder) ReadOnly

func (b Builder) ReadOnly(value bool) Builder

func (Builder) Reference

func (b Builder) Reference(ref string) Schema

Reference will set the schema to be a $ref item pointing to the provided ref.

func (Builder) Title

func (b Builder) Title(value string) Builder

func (Builder) Type

func (b Builder) Type(typs ...Type) Builder

func (Builder) WriteOnly

func (b Builder) WriteOnly(value bool) Builder

type Format

type Format string
const (
	FormatInt32               Format = openapi.Int32Format
	FormatInt64               Format = openapi.Int64Format
	FormatFloat               Format = openapi.FloatFormat
	FormatDouble              Format = openapi.DoubleFormat
	FormatPassword            Format = openapi.PasswordFormat
	FormatDateTime            Format = openapi.DateTimeFormat
	FormatTime                Format = openapi.TimeFormat
	FormatDate                Format = openapi.DateFormat
	FormatDuration            Format = openapi.DurationFormat
	FormatEmail               Format = openapi.EmailFormat
	FormatIDNEmail            Format = openapi.IDNEmailFormat
	FormatHostname            Format = openapi.HostnameFormat
	FormatIDNHostname         Format = openapi.IDNHostnameFormat
	FormatIPv4                Format = openapi.IPv4Format
	FormatIPv6                Format = openapi.IPv6Format
	FormatUUID                Format = openapi.UUIDFormat
	FormatURI                 Format = openapi.URIFormat
	FormatURIReference        Format = openapi.URIReferenceFormat
	FormatIRI                 Format = openapi.IRIFormat
	FormatIRIReference        Format = openapi.IRIReferenceFormat
	FormatURITemplate         Format = openapi.URITemplateFormat
	FormatJsonPointer         Format = openapi.JsonPointerFormat
	FormatRelativeJsonPointer Format = openapi.RelativeJsonPointerFormat
	FormatRegex               Format = openapi.RegexFormat
)

type NumberBuilder

type NumberBuilder struct {
	Schema *openapi.Schema
}

NumberBuilder provides functions for number related options on the schema.

func (NumberBuilder) Build

func (nb NumberBuilder) Build() Schema

func (NumberBuilder) ExclusiveMaximum

func (nb NumberBuilder) ExclusiveMaximum(n int) NumberBuilder

func (NumberBuilder) ExclusiveMinimum

func (nb NumberBuilder) ExclusiveMinimum(n int) NumberBuilder

func (NumberBuilder) Maximum

func (nb NumberBuilder) Maximum(n int) NumberBuilder

func (NumberBuilder) Minimum

func (nb NumberBuilder) Minimum(n int) NumberBuilder

func (NumberBuilder) MultipleOf

func (nb NumberBuilder) MultipleOf(n int) NumberBuilder

type ObjectBuilder

type ObjectBuilder struct {
	Schema *openapi.Schema
}

ObjectBuilder provides functions for object related options on the schema.

func (ObjectBuilder) Build

func (o ObjectBuilder) Build() Schema

func (ObjectBuilder) MaxProperties

func (o ObjectBuilder) MaxProperties(n int) ObjectBuilder

func (ObjectBuilder) MinProperties

func (o ObjectBuilder) MinProperties(n int) ObjectBuilder

func (ObjectBuilder) Property

func (o ObjectBuilder) Property(name string, schema Schema) ObjectBuilder

func (ObjectBuilder) Required

func (o ObjectBuilder) Required(vals ...string) ObjectBuilder

type Option

type Option func(Schema) Schema

func Name

func Name(name string) Option

func NoRef

func NoRef() Option

NoRef will cause the schema to always be used directly instead of a reference to it.

type Schema

type Schema struct {
	openapi.Schema
	// contains filtered or unexported fields
}

Schema represents a json schema object.

https://json-schema.org/overview/what-is-jsonschema

func New

func New() Schema

New returns an empty Schema.

func NewDateTimeSchema

func NewDateTimeSchema() Schema

NewDateTimeSchema returns a Schema representing strings in the `date-time` format.

func (*Schema) GetType

func (s *Schema) GetType() []string

func (Schema) MarshalJSON

func (s Schema) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Schema) Name

func (s *Schema) Name() string

func (*Schema) NoRef

func (s *Schema) NoRef() bool

func (*Schema) Property

func (s *Schema) Property(name string) Builder

Property returns a Builder for the property matching the supplied name. The returned builder will modify the schema directly.

This function panics if no property exists on the schema with the given name.

type Schemer

type Schemer struct {
	// DefaultStructRequire will mark all struct fields
	// on the object schema as required unless it is a pointer.
	DefaultStructRequire bool

	// RefPath determines whether or not the schema will have
	// any $ref items in it. When empty all schemas will be inlined.
	// When set $ref will be $RefPath$TypeName.
	//
	// Defaults to `/schemas`.
	RefPath string

	// GetTypeName is used to get a name for the schema
	// from a given type.
	//
	// This defaults to the name from reflect.Type Name.
	GetTypeName func(reflect.Type) string
	// contains filtered or unexported fields
}

Schemer creates Schema from provided types.

func NewSchemer

func NewSchemer() Schemer

NewSchemer returns a Schemer with the default values set.

func (Schemer) Get

func (s Schemer) Get(obj any) (Schema, error)

Get returns a Schema from the provided type.

func (Schemer) GetSchemaByRef

func (s Schemer) GetSchemaByRef(wantRef string) (Schema, bool)

Get returns a Schema from the provided type.

func (Schemer) Has

func (s Schemer) Has(obj any) bool

func (Schemer) NewRef

func (s Schemer) NewRef(name string) string

NewRef returns string with Schemer.RefPath prefixed to it.

func (Schemer) Set

func (s Schemer) Set(obj any, schema Schema, options ...Option) Schema

Set updates the provided types schema to the supplied one.

type StringBuilder

type StringBuilder struct {
	Schema *openapi.Schema
}

StringBuilder provides functions for string related options on the schema.

func (StringBuilder) Build

func (s StringBuilder) Build() Schema

func (StringBuilder) Format

func (s StringBuilder) Format(value Format) StringBuilder

func (StringBuilder) Length

func (s StringBuilder) Length(n int) StringBuilder

func (StringBuilder) MaxLength

func (s StringBuilder) MaxLength(n int) StringBuilder

func (StringBuilder) MinLength

func (s StringBuilder) MinLength(n int) StringBuilder

func (StringBuilder) Pattern

func (s StringBuilder) Pattern(value string) StringBuilder

type Type

type Type string
const (
	TypeString  Type = openapi.StringType
	TypeNumber  Type = openapi.NumberType
	TypeInteger Type = openapi.IntegerType
	TypeObject  Type = openapi.ObjectType
	TypeArray   Type = openapi.ArrayType
	TypeBoolean Type = openapi.BooleanType
	TypeNull    Type = openapi.NullType
)

type ValidationError

type ValidationError struct {
	OriginalError *jsonschema.ValidationError

	Causes   []ValidationError
	Message  string
	Location string
}

ValidationError represents any errors that occurred during validation of a json object against a schema.

func (ValidationError) Error

func (ve ValidationError) Error() string

func (ValidationError) String

func (ve ValidationError) String() string

type Validator

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

Validator compiles json schemas and validate input against them.

func NewValidator

func NewValidator() *Validator

NewValidator returns a new Validator.

func (*Validator) Add

func (c *Validator) Add(name, schema string) error

Add compiles and stores the schema under the given name.

func (*Validator) Validate

func (c *Validator) Validate(name string, input []byte) error

Validate validates the input against the compiled schema matching the name given.

Jump to

Keyboard shortcuts

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