Closed jpincas closed 4 months ago
@jpincas there is no switch you can flip to make everything optional by default. You can add ,omitempty
to all the fields and that will mark them as optional, or like you already mentioned using required:"false"
.
Another alternative is to provide a custom schema or use the newly released SchemaTransformer
functionality to set s.Required = []string{}
to undo all the required fields, see https://github.com/danielgtaylor/huma/releases/tag/v2.18.0.
If you don't want to do this for each input struct then you can also just write a function which goes over api.OpenAPI().Components.Schemas.Map()
and modifies each one to set the Required
slice to be empty. Hope this helps!
Thanks Daniel. I'll experiment with those options.
@danielgtaylor Using the api.OpenAPI().Components.Schemas.Map()
iteration technique - the documentation does indeed change and none of the fields are showing as required
(the effect I want to achieve), but requests are still rejected with the full list of missing required fields. Is there something else I'm missing?
@jpincas sorry I think you may be running into https://github.com/danielgtaylor/huma/blob/main/schema.go#L280. There is some caching of required property info that should probably get recomputed after you modify s.Required
. We'll need a bug fix for that.
OK understood - thanks for your help.
Hi there. Currently switching over our API to Huma. Thank you for this - it's truly a masterwork.
I've looked all over the docs and searched for the answer to this question, so apologies if the answer is somewhere obvious. For various reasons, I basically need all body fields in a request to be optional. I'd rather not have to go through all my input structs (there are about 40), adding
required:"false"
to each field. Is there any way to override the 'required by default' behaviour (https://huma.rocks/features/request-validation/, Optional/Required, Point 1) so I can just mark the ones that ARE required?Thanks