v2.23.0
Overview
Pointers for Non-Param Fields
It's now possible to use pointers for non-param fields in input structs without Huma complaining. For example, here the User
is not a path/query/header param and is populated from the Authorization
header value for use later:
type EndpointInput struct {
Token string `header:"Authorization"`
User *User
}
func (i *EndpointInput) Resolve(ctx huma.Context) []error {
user, token_valid := ValidateToken(i.Token) // user is nil if token is missing or invalid
i.User = user
return nil
}
Hidden Field Validation
Hidden fields are now validated properly if they are present in the input. For example:
huma.Put(api, "/demo", func(ctx context.Context, input *struct{
Body struct {
Field1 string `json:"field1"
Field2 int `json:"field2" hidden:"true" minimum:"10"`
}
}) (*MyResponse, error) {
// If `input.Field2` is sent by the client, the request will fail
// if its value is below 10 due to the validation schema.
return &MyResponse{...}, nil
})
Prevent Overwriting Schema Validations
All validations now take the existing value of the validator as input when generating the schema, which means a SchemaProvider
or SchemaTransformer
output won't get overwritten when generating schemas. This fixes a bug that was partially fixed but missed several important fields like pattern
.
Non-Addressable Resolver
It's now possible to use non-addressable types which implement Resolver
, such as custom primitive types as map keys. This is currently a little less efficient as a pointer to the type needs to be generated, but at least it is now possible and performance can be improved in the future.
Use the Status Code from NewError
When providing your own custom huma.NewError
function, the resulting error's status code was ignored. This has been fixed to be used as the output status code, enabling the function to modify the status code before going out on the wire.
NewError with a Context
It's now possible to replace huma.NewErrorWithContext
so your error generation function has access to the underlying request context.
NewWithPrefix & Servers
When using humago.NewWithPrefix
and not providing any servers, a single server entry is now generated for you with the given prefix.
Support url.URL
Parameters
You can now use a URL as an input path/query/header parameter and it will be parsed/validated for you.
Request Body Generation Improvements
Like response body generation, the request body generation has been improved to generate missing pieces of the body OpenAPI structure. This enables you to easily e.g. add a description but have Huma still generate the JSON Schema for you. Example:
func (tr TEERouter) RegisterRoutes(api huma.API) {
operation := huma.Operation{
Method: http.MethodPost,
Path: "/tee",
Summary: "TEE",
Description: "TEE description",
RequestBody: &huma.RequestBody{
Description: "My custom request schema",
},
}
huma.Register(api, operation, tr.CalculateTEE)
}
What's Changed
- Allow using pointers for non param fields in input structs by @lsdch in #565
- fix: validate hidden fields by @danielgtaylor in #573
- fix: prevent overwriting schema validations by @danielgtaylor in #575
- fix: support non-addressable resolver values by @danielgtaylor in #580
- fix: use status code returned from NewError when writing errors by @danielgtaylor in #581
- add NewErrorWithContext by @nunoo in #582
- Add default Server object when calling humago.NewWithPrefix by @yursan9 in #579
- fix: unsupported param type url.URL by @ddl-ebrown in #584
- fix: allow setting request description and still gen schema by @danielgtaylor in #587
New Contributors
- @yursan9 made their first contribution in #579
- @ddl-ebrown made their first contribution in #584
Full Changelog: v2.22.1...v2.23.0