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

Feature Proposal: Ability to customize the default values for individual parameters #887

Open
ZachHaber opened this issue Nov 27, 2023 · 0 comments

Comments

@ZachHaber
Copy link

ZachHaber commented Nov 27, 2023

Issue

I'd like to be able to set up a separate default on the Swagger from the one in Joi.

My use case is this: I want to add a default solely in the Swagger docs for a query parameter, so that all new uses where people use the "Try it out" option to get the url to use, they will have the new default. This helps with a slow migration to make that the default in Joi as well and drop the old behavior.

If the customization allows for removing a default as well, it would help workaround issues like #874 from user-land where the default is a complex object and won't work in the swagger ui.

Proposed Solution:

Change properties.js#343 to:

- let propertyDefault = Hoek.reach(joiObj, '_flags.default');
+ let propertyDefault = Utilites.getJoiMetaProperty(joiObj, 'swaggerDefault') ?? Hoek.reach(joiObj, '_flags.default');

Edit: Utilities.getJoiMetaProperty should likely be updated to check for nulls instead of truthyness. - currently a meta value of false would return undefined instead of false

This one-liner would require documenting with the swagerDefault that an empty array will prevent any defaults from being set, as that wouldn't be intuitive to users.

You could also make it so that an undefined swaggerDefault would continue to the normal default, but a null one would also prevent a default from being set, or some other variation of this (like checking if the swaggerDefault meta key exists at all and using it if it does).


Edit2: Probably best to go with this second option - as if you send in an empty array, the UI for the defaultValue when "Try it Out" is not on will show "Default value : List []" as the default, instead of nothing.

It might also make sense to add a swaggerDefaultText to override the "Default value : ..." text when needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant