v2.12.0
Overview
Support json.RawMessage
There is now better support in the validator for json.RawMessage
, which turns into an empty schema (allow anything).
type Demo struct {
Field1 string `json:"field1"`
Field2 json.RawMessage `json:"field2"`
}
OpenAPI 3.0.3
It's now easy to get OpenAPI 3.0.3 from a Huma service for tools that aren't completely compatible with OpenAPI 3.1 just yet. See https://huma.rocks/features/openapi-generation/. The new specs are available at /openapi-3.0.json
and /openapi-3.0.yaml
by default. Programmatic access is available via api.OpenAPI().Downgrade()
.
Better Support for Optional / Nullable Types
Huma now has better support for optional/nullable schemas. Broadly speaking, omitempty
controls whether a field is optional/required and the use of a pointer controls nullability. As described in:
- https://huma.rocks/features/request-validation/#optional-required
- https://huma.rocks/features/request-validation/#nullable
Huma generates type arrays from pointers to simple scalar types (boolean
, integer
, number
, string
) like "type": ["string", "null"]
. This is especially useful for frontend languages like Javascript/Typescript where there is a distinction between explicit null vs. undefined and an SDK generator might produce a type union like:
// Generated Typescript example
type GreetingOutputBody = {
message: string | null;
};
In addition to the default automatic behavior for determining optional/nullable, these can be manually overridden by the user via the required:"true|false"
and nullable:"true|false"
field tags, ensuring you have full control over how the schema is generated.
Important
Types which result in a JSON array
or object
are not marked as nullable by default, due to the complexity of modeling this in JSON Schema and poor support from SDK generators for Go and some other languages. This may change in a future release. You can mark a struct as nullable manually using a _
field with a nullable:"true"
tag as described in the docs linked above.
What's Changed
- docs: add sponsors to docs homepage by @danielgtaylor in #349
- feat: support json.RawMessage by @danielgtaylor in #350
- docs: fixed typo in reference docs by @brahmlower in #354
- feat: better support for optional/nullable types by @danielgtaylor in #351
- docs: fix middleware header example by @danielgtaylor in #358
New Contributors
- @brahmlower made their first contribution in #354
Full Changelog: v2.11.0...v2.12.0