Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about 'required by default' #458

Closed
jpincas opened this issue May 30, 2024 · 5 comments
Closed

Question about 'required by default' #458

jpincas opened this issue May 30, 2024 · 5 comments
Labels
question Further information is requested

Comments

@jpincas
Copy link

jpincas commented May 30, 2024

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

@danielgtaylor
Copy link
Owner

@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!

@danielgtaylor danielgtaylor added the question Further information is requested label Jun 3, 2024
@jpincas
Copy link
Author

jpincas commented Jun 4, 2024

Thanks Daniel. I'll experiment with those options.

@jpincas
Copy link
Author

jpincas commented Jun 4, 2024

@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?

@danielgtaylor
Copy link
Owner

@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.

@jpincas
Copy link
Author

jpincas commented Jun 6, 2024

OK understood - thanks for your help.

@jpincas jpincas closed this as completed Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants