registry

package
v0.6.14 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package registry provides a basic jsonschema registry for creating / viewing jsonschema definitions

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidBase64 = fmt.Errorf("invalid base64")
View Source
var ErrInvalidDuration = fmt.Errorf("invalid duration")

ErrInvalidDuration is returned when the duration is not in valid format

View Source
var ErrInvalidHTTPCode = fmt.Errorf("invalid http code")
View Source
var ErrInvalidHTTPMethod = fmt.Errorf("invalid http method")
View Source
var ErrInvalidHostport = fmt.Errorf("invalid hostport")
View Source
var ErrInvalidIPCIDR = fmt.Errorf("invalid ip or cidr")

ErrInvalidIPCIDR is returned when the ip or cidr is not in valid format

View Source
var ErrInvalidName = fmt.Errorf("invalid name format")
View Source
var ErrInvalidRFC3339Time = fmt.Errorf("invalid RFC3339 time")

ErrInvalidRFC3339Time is returned when the time is not in RFC3339 format

View Source
var ErrInvalidRegexp = fmt.Errorf("invalid regular expression")
View Source
var ErrInvalidURL = fmt.Errorf("invalid url")
View Source
var ErrNilValue = errors.New("nil value")
View Source
var ErrUnsupportedFormat = errors.New("unsupported format")
View Source
var ErrValidatePanic = errors.New("call validate panic")

ErrValidatePanic is the error when the validate function panics

View Source
var ErrorSchemaNotFound = errors.New("schema not found")

Functions

func BuildDefinitionMapFromSchema

func BuildDefinitionMapFromSchema(s *genjs.Schema) genjs.Definitions

BuildDefinitionMapFromSchema takes a JSON schema as input and builds a mapping of definitions within that schema. It creates a map where the key is the reference to the definition (using JSON schema reference syntax) and the value is the actual schema definition itself

func CreatePropMap

func CreatePropMap(init map[string]*genjs.Schema) *orderedmap.OrderedMap[string, *genjs.Schema]

CreatePropMap is a helper for constructing inline OrderedMaps

func GetSchema

func GetSchema(t reflect.Type) (*genjs.Schema, error)

GetSchema returns the json schema of t.

func GetSchemaFromType

func GetSchemaFromType[T any](t T) (string, error)

GetSchemaFromType is a function that takes any type `T` as input and generates a JSON schema representation of that type using the `jsonschema.Reflect` function. It then marshals this schema into an indented JSON string using `json.MarshalIndent` and returns this string along with any error that may occur during the process. This function allows you to easily obtain the JSON schema representation of a given type for further processing or validation purposes.

func LoadSchema

func LoadSchema(inputSchema interface{}) (*genjs.Schema, error)

LoadSchema takes an input of type interface{} and attempts to load and parse a JSON schema from it. It first creates a deep copy of the input schema using the deepcopy package. It then checks the type of the copied schema - if it is a slice of interfaces, it merges all the schemas in the slice into a single map. If it is already a map, it directly assigns it to the `schema` variable. If the type is not recognized as either a slice or a map, it returns `nil` indicating that no schema could be loaded

func Merge

func Merge(dst *map[string]interface{}, schemas []interface{}) error

Merge takes a pointer to a map of string to interface as the destination (`dst`) and a slice of interfaces (`schemas`) as input. It iterates over each schema in the slice and merges it into the destination map using the `mergo.Merge` function

func ModifySchema

func ModifySchema(schema *genjs.Schema, visitors ...func(s *genjs.Schema))

ModifySchema is used to apply one or more visitor functions to a JSON schema. It takes a JSON schema as input along with one or more visitor functions. The function `walk` is used internally to traverse the properties and definitions of the schema. For each property or definition encountered during the traversal, the corresponding visitor function is applied to modify that specific part of the schema.

func RoundTripThrough

func RoundTripThrough[T any, K any](input K) (K, error)

RoundTripThrough round trips input through T. It may be used to understand how various types affect JSON marshalling or apply go's defaulting to an untyped value.

func TransformSchema

func TransformSchema(s *genjs.Schema, transformers ...SchemaTransformer) *genjs.Schema

TransformSchema function takes a JSON schema represented by a `genjs.Schema` as input, along with one or more `SchemaTransformer` implementations. It then iterates over each transformer provided and applies the `Transform` method of each transformer to the input schema sequentially.

func UnmarshalInto

func UnmarshalInto[T any](input any) (T, error)

UnmarshalInto "converts" input into T by marshalling input to JSON and then unmarshalling into T.

func ValidateAgainstSchema

func ValidateAgainstSchema(schema *genjs.Schema, instance any) error

Validate takes a JSON schema and an instance of any type as input and validates the instance against the schema

Types

type CustomSchema

type CustomSchema struct {
	Genschema  *genjs.Schema
	Loadschema *loadjs.Schema
}

CustomSchema struct is defined as a type that implements the `SchemaProvider` interface. This means that the `CustomSchema` struct has a method named `Schema` that takes a `huma.Registry` as input and returns a pointer to a `huma.Schema`. In the implementation of the `Schema` method for `CustomSchema`, it returns a `huma.Schema` with a specific `Type` value of "string".

func (CustomSchema) Schema

func (c CustomSchema) Schema(r huma.Registry) *huma.Schema

type FormatFunc

type FormatFunc func(v interface{}) error

FormatFunc validates the customized format in json schema

type Registry

type Registry interface {
	// GetSchema returns a jsonschema definition by name
	GetSchema(name string) (string, error)

	// SetSchema sets a jsonschema definition by name
	SetSchema(name string, schema string) error

	// DeleteSchema deletes a jsonschema definition by name
	DeleteSchema(name string) error

	// ListSchemas returns a list of all jsonschema definitions
	ListSchemas() ([]string, error)
}

Registry is an interface that defines methods for managing JSON schema definitions

func NewRegistry

func NewRegistry() Registry

NewRegistry creates a new jsonschema registry

type SchemaProvider

type SchemaProvider interface {
	Schema(r huma.Registry) *huma.Schema
}

SchemaProvider defines an interface named `SchemaProvider`. This interface specifies a method signature `Schema` that takes a `huma.Registry` as input and returns a pointer to a `jsonschema.Schema`.

type SchemaTransformer

type SchemaTransformer interface {
	Transform(s *genjs.Schema) *genjs.Schema
}

SchemaTransformer defines an interface in Go that specifies a method signature `Transform` which takes a `*genjs.Schema` as input and returns a modified `*genjs.Schema`. This interface is intended to be implemented by types that provide transformation logic for JSON schemas. The `TransformSchema` function in the provided code snippet iterates over instances of types that implement the `SchemaTransformer` interface and applies the `Transform` method of each transformer to the input schema sequentially. This allows for flexible and customizable transformation of JSON schemas by chaining multiple transformers together.

type ValidateRecorder

type ValidateRecorder struct {
	// JSONSchemaErrs generated by vendor json schema
	JSONSchemaErrs []string `json:"jsonschemaErrs,omitempty"`
	// FormatErrs generated by the format function of the single field
	FormatErrs []string `json:"formatErrs,omitempty"`
	// GeneralErrs generated by Validate() of the Validator itself
	GeneralErrs []string `json:"generalErrs,omitempty"`
	// SystemErr stands internal error, which often means bugs
	SystemErr string `json:"systemErr,omitempty"`
}

ValidateRecorder records varied errors after validating

func Validate

func Validate(v interface{}) *ValidateRecorder

Validate validates by json schema rules, custom formats and general methods.

func (*ValidateRecorder) Error

func (vr *ValidateRecorder) Error() string

Error returns the string representation of the recorder

func (*ValidateRecorder) String

func (vr *ValidateRecorder) String() string

String marshals the recorder to json and returns the string

func (*ValidateRecorder) Valid

func (vr *ValidateRecorder) Valid() bool

Valid represents if the result is valid

type Validator

type Validator interface {
	Validate() error
}

Validator is for the types which need their own Validate function always define Validate on non-pointer level Spec.Validate instead of (*Spec).Validate

Jump to

Keyboard shortcuts

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