Documentation
¶
Index ¶
- Constants
- Variables
- func AutoRegister(api API, server any)
- func DefaultSchemaNamer(t reflect.Type, hint string) string
- func FieldSelectTransform(ctx Context, status string, v any) (any, error)
- func Register[I, O any](api API, op Operation, handler func(context.Context, *I) (*O, error))
- func SetReadDeadline(w http.ResponseWriter, deadline time.Time) error
- func Validate(r Registry, s *Schema, path *PathBuffer, mode ValidateMode, v any, ...)
- func WithOptions[Options any](f func(cmd *cobra.Command, args []string, options *Options)) func(*cobra.Command, []string)
- func WriteErr(api API, ctx Context, status int, msg string, errs ...error)
- type API
- type Adapter
- type AddOpFunc
- type AutoConfig
- type AutoConfigVar
- type CLI
- type Components
- type Config
- type Contact
- type ContentTypeFilter
- type Context
- type Encoding
- type ErrorDetail
- type ErrorDetailer
- type ErrorModel
- type Example
- type ExternalDocs
- type Format
- type Header
- type Hooks
- type Info
- type License
- type Link
- type MediaType
- type OAuthFlow
- type OAuthFlows
- type OpenAPI
- type Operation
- type Param
- type PathBuffer
- type PathItem
- type Registry
- type RequestBody
- type Resolver
- type ResolverWithPath
- type Response
- type Schema
- type SchemaLinkTransformer
- type SecurityScheme
- type Server
- type ServerVariable
- type StatusError
- func Error400BadRequest(msg string, errs ...error) StatusError
- func Error401Unauthorized(msg string, errs ...error) StatusError
- func Error403Forbidden(msg string, errs ...error) StatusError
- func Error404NotFound(msg string, errs ...error) StatusError
- func Error405MethodNotAllowed(msg string, errs ...error) StatusError
- func Error406NotAcceptable(msg string, errs ...error) StatusError
- func Error409Conflict(msg string, errs ...error) StatusError
- func Error410Gone(msg string, errs ...error) StatusError
- func Error412PreconditionFailed(msg string, errs ...error) StatusError
- func Error415UnsupportedMediaType(msg string, errs ...error) StatusError
- func Error422UnprocessableEntity(msg string, errs ...error) StatusError
- func Error429TooManyRequests(msg string, errs ...error) StatusError
- func Error500InternalServerError(msg string, errs ...error) StatusError
- func Error501NotImplemented(msg string, errs ...error) StatusError
- func Error502BadGateway(msg string, errs ...error) StatusError
- func Error503ServiceUnavailable(msg string, errs ...error) StatusError
- func Error504GatewayTimeout(msg string, errs ...error) StatusError
- func Status304NotModied() StatusError
- type StreamResponse
- type Tag
- type Transformer
- type ValidateMode
- type ValidateResult
Constants ¶
const ( TypeBoolean = "boolean" TypeInteger = "integer" TypeNumber = "number" TypeString = "string" TypeArray = "array" TypeObject = "object" )
JSON Schema type constants
Variables ¶
var DefaultCBORFormat = Format{ Marshal: func(w io.Writer, v any) error { return cborEncMode.NewEncoder(w).Encode(v) }, Unmarshal: cbor.Unmarshal, }
DefaultCBORFormat is the default CBOR formatter that can be set in the API's `Config.Formats` map.
var DefaultJSONFormat = Format{ Marshal: func(w io.Writer, v any) error { return json.NewEncoder(w).Encode(v) }, Unmarshal: json.Unmarshal, }
DefaultJSONFormat is the default JSON formatter that can be set in the API's `Config.Formats` map.
var ErrSchemaInvalid = errors.New("schema is invalid")
ErrSchemaInvalid is sent when there is a problem building the schema.
var NewError = func(status int, msg string, errs ...error) StatusError { details := make([]*ErrorDetail, len(errs)) for i := 0; i < len(errs); i++ { if converted, ok := errs[i].(ErrorDetailer); ok { details[i] = converted.ErrorDetail() } else { details[i] = &ErrorDetail{Message: errs[i].Error()} } } return &ErrorModel{ Status: status, Title: http.StatusText(status), Detail: msg, Errors: details, } }
NewError creates a new instance of an error model with the given status code, message, and errors. If the error implements the `ErrorDetailer` interface, the error details will be used. Otherwise, the error message will be used. Replace this function to use your own error type.
Functions ¶
func AutoRegister ¶
AutoRegister auto-detects operation registration methods and registers them with the given API. Any method named `Register...` will be called and passed the API as the only argument. Since registration happens at service startup, no errors are returned and methods should panic on error.
type ItemServer struct {}
func (s *ItemServer) RegisterListItems(api API) {
huma.Register(api, huma.Operation{
OperationID: "ListItems",
Method: http.MethodGet,
Path: "/items",
}, s.ListItems)
}
func main() {
api := huma.NewAPI("My Service", "1.0.0")
itemServer := &ItemServer{}
huma.AutoRegister(api, itemServer)
}
func DefaultSchemaNamer ¶
DefaultSchemaNamer provides schema names for types. It uses the type name when possible, ignoring the package name. If the type is unnamed, then the name hint is used. Note: if you plan to use types with the same name from different packages, you should implement your own namer function to prevent issues. Nested anonymous types can also present naming issues.
func FieldSelectTransform ¶
FieldSelectTransform is an example of a transform that can use an input header value to modify the response on the server, providing a GraphQL-like way to send only the fields that the client wants over the wire.
func Register ¶
Register an operation handler for an API. The handler must be a function that takes a context and a pointer to the input struct and returns a pointer to the output struct and an error. The input struct must be a struct with fields for the request path/query/header parameters and/or body. The output struct must be a struct with fields for the output headers and body of the operation, if any.
func SetReadDeadline ¶
func SetReadDeadline(w http.ResponseWriter, deadline time.Time) error
SetReadDeadline is a utility to set the read deadline on a response writer, if possible. If not, it will not incur any allocations (unlike the stdlib `http.ResponseController`).
func Validate ¶
func Validate(r Registry, s *Schema, path *PathBuffer, mode ValidateMode, v any, res *ValidateResult)
Validate an input value against a schema, collecting errors in the validation result object. If successful, `res.Errors` will be empty. It is suggested to use a `sync.Pool` to reuse the PathBuffer and ValidateResult objects, making sure to call `Reset()` on them before returning them to the pool.
Types ¶
type API ¶
type API interface {
// Adapter returns the router adapter for this API, providing a generic
// interface to get request information and write responses.
Adapter() Adapter
// OpenAPI returns the OpenAPI spec for this API. You may edit this spec
// until the server starts.
OpenAPI() *OpenAPI
// Negotiate returns the selected content type given the client's `accept`
// header and the server's supported content types. If the client does not
// send an `accept` header, then JSON is used.
Negotiate(accept string) (string, error)
// Marshal marshals the given value into the given writer. The content type
// is used to determine which format to use. Use `Negotiate` to get the
// content type from an accept header. TODO: update
Marshal(ctx Context, respKey string, contentType string, v any) error
// Unmarshal unmarshals the given data into the given value. The content type
Unmarshal(contentType string, data []byte, v any) error
}
API represents a Huma API wrapping a specific router.
type Adapter ¶
type Adapter interface {
Handle(op *Operation, handler func(ctx Context))
ServeHTTP(http.ResponseWriter, *http.Request)
}
Adapter is an interface that allows the API to be used with different HTTP routers and frameworks. It is designed to work with the standard library `http.Request` and `http.ResponseWriter` types as well as types like `gin.Context` or `fiber.Ctx` that provide both request and response functionality in one place.
type AutoConfig ¶
type AutoConfig struct {
Security string `json:"security"`
Headers map[string]string `json:"headers,omitempty"`
Prompt map[string]AutoConfigVar `json:"prompt,omitempty"`
Params map[string]string `json:"params"`
}
AutoConfig holds an API's automatic configuration settings for the CLI. These are advertised via OpenAPI extension and picked up by the CLI to make it easier to get started using an API. This struct should be put into the `OpenAPI.Extensions` map under the key `x-cli-config`. See also: https://rest.sh/#/openapi?id=autoconfiguration
type AutoConfigVar ¶
type AutoConfigVar struct {
Description string `json:"description,omitempty"`
Example string `json:"example,omitempty"`
Default interface{} `json:"default,omitempty"`
Enum []interface{} `json:"enum,omitempty"`
// Exclude the value from being sent to the server. This essentially makes
// it a value which is only used in param templates.
Exclude bool `json:"exclude,omitempty"`
}
AutoConfigVar represents a variable given by the user when prompted during auto-configuration setup of an API.
type CLI ¶
type CLI interface {
// Run the CLI. This will parse the command-line arguments and environment
// variables and then run the appropriate command. If no command is given,
// the default command will call the `OnStart` function to start a server.
Run()
// Root returns the root Cobra command. This can be used to add additional
// commands or flags. Customize it however you like.
Root() *cobra.Command
}
CLI is an optional command-line interface for a Huma service. It is provided as a convenience for quickly building a service with configuration from the environment and/or command-line options, all tied to a simple type-safe Go struct.
type Components ¶
type Components struct {
Schemas Registry `yaml:"schemas,omitempty"`
Responses map[string]*Response `yaml:"responses,omitempty"`
Parameters map[string]*Param `yaml:"parameters,omitempty"`
Examples map[string]*Example `yaml:"examples,omitempty"`
RequestBodies map[string]*RequestBody `yaml:"requestBodies,omitempty"`
Headers map[string]*Header `yaml:"headers,omitempty"`
SecuritySchemes map[string]*SecurityScheme `yaml:"securitySchemes,omitempty"`
Links map[string]*Link `yaml:"links,omitempty"`
Callbacks map[string]*PathItem `yaml:"callbacks,omitempty"`
PathItems map[string]*PathItem `yaml:"pathItems,omitempty"`
Extensions map[string]any `yaml:",inline"`
}
type Config ¶
type Config struct {
// OpenAPI spec for the API. You should set at least the `Info.Title` and
// `Info.Version` fields.
*OpenAPI
// OpenAPIPath is the path to the OpenAPI spec without extension. If set
// to `/openapi` it will allow clients to get `/openapi.json` or
// `/openapi.yaml`, for example.
OpenAPIPath string
DocsPath string
SchemasPath string
// Formats defines the supported request/response formats by content type or
// extension (e.g. `json` for `application/my-format+json`).
Formats map[string]Format
// DefaultFormat specifies the default content type to use when the client
// does not specify one. If unset, the default type will be randomly
// chosen from the keys of `Formats`.
DefaultFormat string
// Transformers are a way to modify a response body before it is serialized.
Transformers []Transformer
}
Config represents a configuration for a new API. See `huma.DefaultConfig()` as a starting point.
func DefaultConfig ¶
type Contact ¶
type Contact struct {
// Name of the contact person/organization.
Name string `yaml:"name,omitempty"`
// URL pointing to the contact information.
URL string `yaml:"url,omitempty"`
// Email address of the contact person/organization.
Email string `yaml:"email,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
Contact information to get support for the API.
type ContentTypeFilter ¶
ContentTypeFilter allows you to override the content type for responses, allowing you to return a different content type like `application/problem+json` after using the `application/json` marshaller. This should be implemented by the response body struct.
type Context ¶
type Context interface {
Operation() *Operation
Context() context.Context
Method() string
Host() string
URL() url.URL
Param(name string) string
Query(name string) string
Header(name string) string
EachHeader(cb func(name, value string))
BodyReader() io.Reader
SetReadDeadline(time.Time) error
SetStatus(code int)
SetHeader(name, value string)
AppendHeader(name, value string)
BodyWriter() io.Writer
}
Context is the current request/response context. It provides a generic interface to get request information and write responses.
type Encoding ¶
type Encoding struct {
ContentType string `yaml:"contentType,omitempty"`
Headers map[string]*Header `yaml:"headers,omitempty"`
Style string `yaml:"style,omitempty"`
Explode *bool `yaml:"explode,omitempty"`
AllowReserved bool `yaml:"allowReserved,omitempty"`
Extensions map[string]any `yaml:",inline"`
}
type ErrorDetail ¶
type ErrorDetail struct {
// Message is a human-readable explanation of the error.
Message string `json:"message,omitempty" doc:"Error message text"`
// Location is a path-like string indicating where the error occured.
// It typically begins with `path`, `query`, `header`, or `body`. Example:
// `body.items[3].tags` or `path.thing-id`.
Location string `json:"location,omitempty" doc:"Where the error occured, e.g. 'body.items[3].tags' or 'path.thing-id'"`
// Value is the value at the given location, echoed back to the client
// to help with debugging. This can be useful for e.g. validating that
// the client didn't send extra whitespace or help when the client
// did not log an outgoing request.
Value any `json:"value,omitempty" doc:"The value at the given location"`
}
ErrorDetail provides details about a specific error.
func (*ErrorDetail) Error ¶
func (e *ErrorDetail) Error() string
Error returns the error message / satisfies the `error` interface.
func (*ErrorDetail) ErrorDetail ¶
func (e *ErrorDetail) ErrorDetail() *ErrorDetail
ErrorDetail satisfies the `ErrorDetailer` interface.
type ErrorDetailer ¶
type ErrorDetailer interface {
ErrorDetail() *ErrorDetail
}
ErrorDetailer returns error details for responses & debugging.
type ErrorModel ¶
type ErrorModel struct {
// Type is a URI to get more information about the error type.
Type string `` /* 170-byte string literal not displayed */
// Title provides a short static summary of the problem. Huma will default this
// to the HTTP response status code text if not present.
Title string `` /* 165-byte string literal not displayed */
// Status provides the HTTP status code for client convenience. Huma will
// default this to the response status code if unset. This SHOULD match the
// response status code (though proxies may modify the actual status code).
Status int `json:"status,omitempty" example:"400" doc:"HTTP status code"`
// Detail is an explanation specific to this error occurrence.
Detail string `` /* 153-byte string literal not displayed */
// Instance is a URI to get more info about this error occurence.
Instance string `` /* 162-byte string literal not displayed */
// Errors provides an optional mechanism of passing additional error details
// as a list.
Errors []*ErrorDetail `json:"errors,omitempty" doc:"Optional list of individual error details"`
}
ErrorModel defines a basic error message model.
func (*ErrorModel) Add ¶
func (e *ErrorModel) Add(err error)
func (*ErrorModel) ContentType ¶
func (e *ErrorModel) ContentType(ct string) string
func (*ErrorModel) Error ¶
func (e *ErrorModel) Error() string
func (*ErrorModel) GetStatus ¶
func (e *ErrorModel) GetStatus() int
type ExternalDocs ¶
type Format ¶
type Format struct {
// Marshal a value to a given writer (e.g. response body).
Marshal func(writer io.Writer, v any) error
// Unmarshal a value into `v` from the given bytes (e.g. request body).
Unmarshal func(data []byte, v any) error
}
Format represents a request / response format. It is used to marshal and unmarshal data.
type Hooks ¶
type Hooks interface {
// OnStart sets a function to call when the service should be started. This
// is called by the default command if no command is given. The callback
// should take whatever steps are necessary to start the server, such as
// `httpServer.ListenAndServer(...)`.
OnStart(func())
// OnStop sets a function to call when the service should be stopped. This
// is called by the default command if no command is given. The callback
// should take whatever steps are necessary to stop the server, such as
// `httpServer.Shutdown(...)`.
OnStop(func())
}
Hooks is an interface for setting up callbacks for the CLI. It is used to start and stop the service.
type Info ¶
type Info struct {
// Title of the API.
Title string `yaml:"title"`
// Description of the API. CommonMark syntax MAY be used for rich text representation.
Description string `yaml:"description,omitempty"`
// TermsOfService URL for the API.
TermsOfService string `yaml:"termsOfService,omitempty"`
// Contact information to get support for the API.
Contact *Contact `yaml:"contact,omitempty"`
// License name & link for using the API.
License *License `yaml:"license,omitempty"`
// Version of the OpenAPI document (which is distinct from the OpenAPI Specification version or the API implementation version).
Version string `yaml:"version"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
Info object that provides metadata about the API. The metadata MAY be used by the clients if needed, and MAY be presented in editing or documentation generation tools for convenience.
type License ¶
type License struct {
// Name of the license.
Name string `yaml:"name"`
// Identifier SPDX license expression for the API. This field is mutually
// exclusive with the URL field.
Identifier string `yaml:"identifier,omitempty"`
// URL pointing to the license. This field is mutually exclusive with the
// Identifier field.
URL string `yaml:"url,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
License name & link for using the API.
type Link ¶
type Link struct {
Ref string `yaml:"$ref,omitempty"`
OperationRef string `yaml:"operationRef,omitempty"`
OperationID string `yaml:"operationId,omitempty"`
Parameters map[string]any `yaml:"parameters,omitempty"`
RequestBody any `yaml:"requestBody,omitempty"`
Description string `yaml:"description,omitempty"`
Server *Server `yaml:"server,omitempty"`
Extensions map[string]any `yaml:",inline"`
}
type OAuthFlows ¶
type OpenAPI ¶
type OpenAPI struct {
OpenAPI string `yaml:"openapi"`
Info *Info `yaml:"info"`
Servers []*Server `yaml:"servers,omitempty"`
JSONSchemaDialect string `yaml:"jsonSchemaDialect,omitempty"`
Paths map[string]*PathItem `yaml:"paths,omitempty"`
Webhooks map[string]*PathItem `yaml:"webhooks,omitempty"`
Components *Components `yaml:"components,omitempty"`
Security []map[string][]string `yaml:"security,omitempty"`
Tags []*Tag `yaml:"tags,omitempty"`
ExternalDocs *ExternalDocs `yaml:"externalDocs,omitempty"`
Extensions map[string]any `yaml:",inline"`
// OnAddOperation is called when an operation is added to the OpenAPI via
// `AddOperation`. You may bypass this by directly writing to the `Paths`
// map instead.
OnAddOperation []AddOpFunc `yaml:"-"`
}
func (*OpenAPI) AddOperation ¶
func (*OpenAPI) MarshalJSON ¶
type Operation ¶
type Operation struct {
// Method is the HTTP method for this operation
Method string `yaml:"-"`
// Path is the URL path for this operation
Path string `yaml:"-"`
// DefaultStatus is the default HTTP status code for this operation. It will
// be set to 200 or 204 if not specified, depending on whether the handler
// returns a response body.
DefaultStatus int `yaml:"-"`
// MaxBodyBytes is the maximum number of bytes to read from the request
// body. If not specified, the default is 1MB. Use -1 for unlimited. If
// the limit is reached, then an HTTP 413 error is returned.
MaxBodyBytes int64 `yaml:"-"`
// BodyReadTimeout is the maximum amount of time to wait for the request
// body to be read. If not specified, the default is 5 seconds. Use -1
// for unlimited. If the timeout is reached, then an HTTP 408 error is
// returned. This value supercedes the server's read timeout, and a value
// of -1 can unset the server's timeout.
BodyReadTimeout time.Duration `yaml:"-"`
// Errors is a list of HTTP status codes that the handler may return. If
// not specified, then a default error response is added to the OpenAPI.
Errors []int `yaml:"-"`
// SkipValidateParams disables validation of path, query, and header
// parameters. This can speed up request processing if you want to handle
// your own validation. Use with caution!
SkipValidateParams bool `yaml:"-"`
// SkipValidateBody disables validation of the request body. This can speed
// up request processing if you want to handle your own validation. Use with
// caution!
SkipValidateBody bool `yaml:"-"`
// Hidden will skip documenting this operation in the OpenAPI. This is
// useful for operations that are not intended to be used by clients but
// you'd still like the benefits of using Huma. Generally not recommended.
Hidden bool `yaml:"-"`
Tags []string `yaml:"tags,omitempty"`
Summary string `yaml:"summary,omitempty"`
Description string `yaml:"description,omitempty"`
ExternalDocs *ExternalDocs `yaml:"externalDocs,omitempty"`
OperationID string `yaml:"operationId,omitempty"`
Parameters []*Param `yaml:"parameters,omitempty"`
RequestBody *RequestBody `yaml:"requestBody,omitempty"`
Responses map[string]*Response `yaml:"responses,omitempty"`
Callbacks map[string]*PathItem `yaml:"callbacks,omitempty"`
Deprecated bool `yaml:"deprecated,omitempty"`
Security []map[string][]string `yaml:"security,omitempty"`
Servers []*Server `yaml:"servers,omitempty"`
Extensions map[string]any `yaml:",inline"`
}
type Param ¶
type Param struct {
Ref string `yaml:"$ref,omitempty"`
Name string `yaml:"name,omitempty"`
In string `yaml:"in,omitempty"`
Description string `yaml:"description,omitempty"`
Required bool `yaml:"required,omitempty"`
Deprecated bool `yaml:"deprecated,omitempty"`
Style string `yaml:"style,omitempty"`
Explode *bool `yaml:"explode,omitempty"`
AllowReserved bool `yaml:"allowReserved,omitempty"`
Schema *Schema `yaml:"schema,omitempty"`
Example any `yaml:"example,omitempty"`
Examples map[string]*Example `yaml:"examples,omitempty"`
Extensions map[string]any `yaml:",inline"`
}
type PathBuffer ¶
type PathBuffer struct {
// contains filtered or unexported fields
}
PathBuffer is a low-allocation helper for building a path string like `foo.bar.baz`. It is not goroutine-safe. Combined with `sync.Pool` it can result in zero allocations, and is used for validation. It is significantly better than `strings.Builder` and `bytes.Buffer` for this use case.
Path buffers can be converted to strings for use in responses or printing using either the `pb.String()` or `pb.With("field")` methods.
pb := NewPathBuffer([]byte{}, 0)
pb.Push("foo") // foo
pb.PushIndex(1) // foo[1]
pb.Push("bar") // foo[1].bar
pb.Pop() // foo[1]
pb.Pop() // foo
func NewPathBuffer ¶
func NewPathBuffer(buf []byte, offset int) *PathBuffer
NewPathBuffer creates a new path buffer given an existing byte slice. Tip: using `sync.Pool` can significantly reduce buffer allocations.
pb := NewPathBuffer([]byte{}, 0)
pb.Push("foo")
func (*PathBuffer) Bytes ¶
func (b *PathBuffer) Bytes() []byte
Bytes returns the underlying slice of bytes of the path.
func (*PathBuffer) Pop ¶
func (b *PathBuffer) Pop()
Pop the latest entry off the path.
pb.Push("foo") // foo
pb.PushIndex(1) // foo[1]
pb.Push("bar") // foo[1].bar
pb.Pop() // foo[1]
pb.Pop() // foo
func (*PathBuffer) Push ¶
func (b *PathBuffer) Push(s string)
Push an entry onto the path, adding a `.` separator as needed.
pb.Push("foo") // foo
pb.Push("bar") // foo.bar
func (*PathBuffer) PushIndex ¶
func (b *PathBuffer) PushIndex(i int)
PushIndex pushes an entry onto the path surrounded by `[` and `]`.
pb.Push("foo") // foo
pb.PushIndex(1) // foo[1]
func (*PathBuffer) Reset ¶
func (b *PathBuffer) Reset()
Reset the path buffer to empty, keeping and reusing the underlying bytes.
func (*PathBuffer) String ¶
func (b *PathBuffer) String() string
String converts the path buffer to a string.
func (*PathBuffer) With ¶
func (b *PathBuffer) With(s string) string
With is shorthand for push, convert to string, and pop. This is useful when you want the location of a field given a path buffer as a prefix.
pb.Push("foo")
pb.With("bar") // returns foo.bar
type PathItem ¶
type PathItem struct {
Ref string `yaml:"$ref,omitempty"`
Summary string `yaml:"summary,omitempty"`
Description string `yaml:"description,omitempty"`
Get *Operation `yaml:"get,omitempty"`
Put *Operation `yaml:"put,omitempty"`
Post *Operation `yaml:"post,omitempty"`
Delete *Operation `yaml:"delete,omitempty"`
Options *Operation `yaml:"options,omitempty"`
Head *Operation `yaml:"head,omitempty"`
Patch *Operation `yaml:"patch,omitempty"`
Trace *Operation `yaml:"trace,omitempty"`
Servers []*Server `yaml:"servers,omitempty"`
Parameters []*Param `yaml:"parameters,omitempty"`
Extensions map[string]any `yaml:",inline"`
}
type Registry ¶
type Registry interface {
Schema(t reflect.Type, allowRef bool, hint string) *Schema
SchemaFromRef(ref string) *Schema
TypeFromRef(ref string) reflect.Type
Map() map[string]*Schema
MarshalJSON() ([]byte, error)
MarshalYAML() (interface{}, error)
}
Registry creates and stores schemas and their references, and supports marshalling to JSON/YAML for use as an OpenAPI #/components/schemas object. Behavior is implementation-dependent, but the design allows for recursive schemas to exist while being flexible enough to support other use cases like only inline objects (no refs) or always using refs for structs.
type RequestBody ¶
type Resolver ¶
Resolver runs a `Resolve` function after a request has been parsed, enabling you to run custom validation or other code that can modify the request and / or return errors.
type ResolverWithPath ¶
type ResolverWithPath interface {
Resolve(ctx Context, prefix *PathBuffer) []error
}
ResolverWithPath runs a `Resolve` function after a request has been parsed, enabling you to run custom validation or other code that can modify the request and / or return errors. The `prefix` is the path to the current location for errors, e.g. `body.foo[0].bar`.
type Response ¶
type Response struct {
Ref string `yaml:"$ref,omitempty"`
Description string `yaml:"description,omitempty"`
Headers map[string]*Param `yaml:"headers,omitempty"`
Content map[string]*MediaType `yaml:"content,omitempty"`
Links map[string]*Link `yaml:"links,omitempty"`
Extensions map[string]any `yaml:",inline"`
}
type Schema ¶
type Schema struct {
Type string `yaml:"type,omitempty"`
Title string `yaml:"title,omitempty"`
Description string `yaml:"description,omitempty"`
Ref string `yaml:"$ref,omitempty"`
Format string `yaml:"format,omitempty"`
ContentEncoding string `yaml:"contentEncoding,omitempty"`
Default any `yaml:"default,omitempty"`
Examples []any `yaml:"examples,omitempty"`
Items *Schema `yaml:"items,omitempty"`
AdditionalProperties any `yaml:"additionalProperties,omitempty"`
Properties map[string]*Schema `yaml:"properties,omitempty"`
Enum []any `yaml:"enum,omitempty"`
Minimum *float64 `yaml:"minimum,omitempty"`
ExclusiveMinimum *float64 `yaml:"exclusiveMinimum,omitempty"`
Maximum *float64 `yaml:"maximum,omitempty"`
ExclusiveMaximum *float64 `yaml:"exclusiveMaximum,omitempty"`
MultipleOf *float64 `yaml:"multipleOf,omitempty"`
MinLength *int `yaml:"minLength,omitempty"`
MaxLength *int `yaml:"maxLength,omitempty"`
Pattern string `yaml:"pattern,omitempty"`
MinItems *int `yaml:"minItems,omitempty"`
MaxItems *int `yaml:"maxItems,omitempty"`
UniqueItems bool `yaml:"uniqueItems,omitempty"`
Required []string `yaml:"required,omitempty"`
MinProperties *int `yaml:"minProperties,omitempty"`
MaxProperties *int `yaml:"maxProperties,omitempty"`
ReadOnly bool `yaml:"readOnly,omitempty"`
WriteOnly bool `yaml:"writeOnly,omitempty"`
Deprecated bool `yaml:"deprecated,omitempty"`
Extensions map[string]any `yaml:",inline"`
// contains filtered or unexported fields
}
Schema represents a JSON Schema compatible with OpenAPI 3.1. It is extensible with your own custom properties. It supports a subset of the full JSON Schema spec, designed specifically for use with Go structs and to enable fast zero or near-zero allocation happy-path validation for incoming requests.
func SchemaFromField ¶
func (*Schema) MarshalJSON ¶
func (*Schema) PrecomputeMessages ¶
func (s *Schema) PrecomputeMessages()
type SchemaLinkTransformer ¶
type SchemaLinkTransformer struct {
// contains filtered or unexported fields
}
func NewSchemaLinkTransformer ¶
func NewSchemaLinkTransformer(prefix, schemasPath string) *SchemaLinkTransformer
func (*SchemaLinkTransformer) OnAddOperation ¶
func (t *SchemaLinkTransformer) OnAddOperation(oapi *OpenAPI, op *Operation)
type SecurityScheme ¶
type SecurityScheme struct {
Type string `yaml:"type"`
Description string `yaml:"description,omitempty"`
Name string `yaml:"name,omitempty"`
In string `yaml:"in,omitempty"`
Scheme string `yaml:"scheme,omitempty"`
BearerFormat string `yaml:"bearerFormat,omitempty"`
Flows *OAuthFlows `yaml:"flows,omitempty"`
OpenIDConnectURL string `yaml:"openIdConnectUrl,omitempty"`
Extensions map[string]any `yaml:",inline"`
}
type Server ¶
type Server struct {
// URL to the target host. This URL supports Server Variables and MAY be relative, to indicate that the host location is relative to the location where the OpenAPI document is being served. Variable substitutions will be made when a variable is named in {brackets}.
URL string `yaml:"url"`
// Description of the host designated by the URL. CommonMark syntax MAY be used for rich text representation.
Description string `yaml:"description,omitempty"`
// Variables map between a variable name and its value. The value is used for substitution in the server’s URL template.
Variables map[string]*ServerVariable `yaml:"variables,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
Server URL, optionally with variables.
type ServerVariable ¶
type ServerVariable struct {
// Enumeration of string values to be used if the substitution options are from a limited set. The array MUST NOT be empty.
Enum []string `yaml:"enum,omitempty"`
// Default value to use for substitution, which SHALL be sent if an alternate value is not supplied.
Default string `yaml:"default"`
// Description for the server variable. CommonMark syntax MAY be used for rich text representation.
Description string `yaml:"description,omitempty"`
// Extensions (user-defined properties), if any. Values in this map will
// be marshalled as siblings of the other properties above.
Extensions map[string]any `yaml:",inline"`
}
ServerVariable for server URL template substitution.
type StatusError ¶
StatusError is an error that has an HTTP status code. When returned from an operation handler, this sets the response status code.
func Error400BadRequest ¶
func Error400BadRequest(msg string, errs ...error) StatusError
Error400BadRequest returns a 400.
func Error401Unauthorized ¶
func Error401Unauthorized(msg string, errs ...error) StatusError
Error401Unauthorized returns a 401.
func Error403Forbidden ¶
func Error403Forbidden(msg string, errs ...error) StatusError
Error403Forbidden returns a 403.
func Error404NotFound ¶
func Error404NotFound(msg string, errs ...error) StatusError
Error404NotFound returns a 404.
func Error405MethodNotAllowed ¶
func Error405MethodNotAllowed(msg string, errs ...error) StatusError
Error405MethodNotAllowed returns a 405.
func Error406NotAcceptable ¶
func Error406NotAcceptable(msg string, errs ...error) StatusError
Error406NotAcceptable returns a 406.
func Error409Conflict ¶
func Error409Conflict(msg string, errs ...error) StatusError
Error409Conflict returns a 409.
func Error410Gone ¶
func Error410Gone(msg string, errs ...error) StatusError
Error410Gone returns a 410.
func Error412PreconditionFailed ¶
func Error412PreconditionFailed(msg string, errs ...error) StatusError
Error412PreconditionFailed returns a 412.
func Error415UnsupportedMediaType ¶
func Error415UnsupportedMediaType(msg string, errs ...error) StatusError
Error415UnsupportedMediaType returns a 415.
func Error422UnprocessableEntity ¶
func Error422UnprocessableEntity(msg string, errs ...error) StatusError
Error422UnprocessableEntity returns a 422.
func Error429TooManyRequests ¶
func Error429TooManyRequests(msg string, errs ...error) StatusError
Error429TooManyRequests returns a 429.
func Error500InternalServerError ¶
func Error500InternalServerError(msg string, errs ...error) StatusError
Error500InternalServerError returns a 500.
func Error501NotImplemented ¶
func Error501NotImplemented(msg string, errs ...error) StatusError
Error501NotImplemented returns a 501.
func Error502BadGateway ¶
func Error502BadGateway(msg string, errs ...error) StatusError
Error502BadGateway returns a 502.
func Error503ServiceUnavailable ¶
func Error503ServiceUnavailable(msg string, errs ...error) StatusError
Error503ServiceUnavailable returns a 503.
func Error504GatewayTimeout ¶
func Error504GatewayTimeout(msg string, errs ...error) StatusError
Error504GatewayTimeout returns a 504.
func Status304NotModied ¶
func Status304NotModied() StatusError
Status304NotModied returns a 304. This is not really an error, but provides a way to send non-default responses.
type StreamResponse ¶
type StreamResponse struct {
Body func(ctx Context)
}
StreamResponse is a response that streams data to the client. The body function will be called once the response headers have been written and the body writer is ready to be written to.
type Tag ¶
type Tag struct {
Name string `yaml:"name"`
Description string `yaml:"description,omitempty"`
ExternalDocs *ExternalDocs `yaml:"externalDocs,omitempty"`
Extensions map[string]any `yaml:",inline"`
}
type Transformer ¶
Transformer is a function that can modify a response body before it is serialized. The `status` is the HTTP status code for the response and `v` is the value to be serialized. The return value is the new value to be serialized or an error.
type ValidateMode ¶
type ValidateMode int
const ( // ModeReadFromServer is a read mode (response output) that may ignore or // reject write-only fields that are non-zero, as these write-only fields // are meant to be sent by the client. ModeReadFromServer ValidateMode = iota // ModeWriteToServer is a write mode (request input) that may ignore or // reject read-only fields that are non-zero, as these are owned by the // server and the client should not try to modify them. ModeWriteToServer )
type ValidateResult ¶
type ValidateResult struct {
Errors []error
}
ValidateResult tracks validation errors.
func (*ValidateResult) Add ¶
func (r *ValidateResult) Add(path *PathBuffer, v any, msg string)
func (*ValidateResult) Addf ¶
func (r *ValidateResult) Addf(path *PathBuffer, v any, format string, args ...any)
func (*ValidateResult) Reset ¶
func (r *ValidateResult) Reset()
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
adapters
|
|
|
examples
|
|
|
cmd
command
|
|
|
greet
command
|
|
|
protodemo
command
|
|
|
Package humatest provides testing utilities for Huma services.
|
Package humatest provides testing utilities for Huma services. |
|
Package queryparam provides utilities for efficiently working with query parameters in URLs.
|
Package queryparam provides utilities for efficiently working with query parameters in URLs. |
|
Package sse provides utilities for working with Server Sent Events (SSE).
|
Package sse provides utilities for working with Server Sent Events (SSE). |
