Documentation
¶
Overview ¶
Package registry provides a basic jsonschema registry for creating / viewing jsonschema definitions
Index ¶
- Variables
- func BuildDefinitionMapFromSchema(s *genjs.Schema) genjs.Definitions
- func CreatePropMap(init map[string]*genjs.Schema) *orderedmap.OrderedMap[string, *genjs.Schema]
- func GetSchema(t reflect.Type) (*genjs.Schema, error)
- func GetSchemaFromType[T any](t T) (string, error)
- func LoadSchema(inputSchema interface{}) (*genjs.Schema, error)
- func Merge(dst *map[string]interface{}, schemas []interface{}) error
- func ModifySchema(schema *genjs.Schema, visitors ...func(s *genjs.Schema))
- func RoundTripThrough[T any, K any](input K) (K, error)
- func TransformSchema(s *genjs.Schema, transformers ...SchemaTransformer) *genjs.Schema
- func UnmarshalInto[T any](input any) (T, error)
- func ValidateAgainstSchema(schema *genjs.Schema, instance any) error
- type CustomSchema
- type FormatFunc
- type Registry
- type SchemaProvider
- type SchemaTransformer
- type ValidateRecorder
- type Validator
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidBase64 = fmt.Errorf("invalid base64")
var ErrInvalidDuration = fmt.Errorf("invalid duration")
ErrInvalidDuration is returned when the duration is not in valid format
var ErrInvalidHTTPCode = fmt.Errorf("invalid http code")
var ErrInvalidHTTPMethod = fmt.Errorf("invalid http method")
var ErrInvalidHostport = fmt.Errorf("invalid hostport")
var ErrInvalidIPCIDR = fmt.Errorf("invalid ip or cidr")
ErrInvalidIPCIDR is returned when the ip or cidr is not in valid format
var ErrInvalidName = fmt.Errorf("invalid name format")
var ErrInvalidRFC3339Time = fmt.Errorf("invalid RFC3339 time")
ErrInvalidRFC3339Time is returned when the time is not in RFC3339 format
var ErrInvalidRegexp = fmt.Errorf("invalid regular expression")
var ErrInvalidURL = fmt.Errorf("invalid url")
var ErrNilValue = errors.New("nil value")
var ErrUnsupportedFormat = errors.New("unsupported format")
var ErrValidatePanic = errors.New("call validate panic")
ErrValidatePanic is the error when the validate function panics
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 GetSchemaFromType ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
UnmarshalInto "converts" input into T by marshalling input to JSON and then unmarshalling into T.
Types ¶
type CustomSchema ¶
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
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 ¶
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