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

[Request] Add detail to .describe for function defaults #1121

Closed
WesTyler opened this issue Mar 7, 2017 · 3 comments
Closed

[Request] Add detail to .describe for function defaults #1121

WesTyler opened this issue Mar 7, 2017 · 3 comments
Labels
feature New functionality or improvement
Milestone

Comments

@WesTyler
Copy link
Contributor

WesTyler commented Mar 7, 2017

What are you trying to achieve or the steps to reproduce ?

The .default method already supports providing a function value. I would like to expand the information returned by .describe() when the default is a function instead of a string.

const randomNumber = function () {
    return Math.random();
};
randomNumber.description = 'Generate random number';

const schema = Joi.any().default(randomNumber);
schema.validate(); // { error: null, value: 0.8081045034806544 }
schema.validate(); // { error: null, value: 0.42643964379927235 }

Which result you had ?

Using the above schema:

schema.describe();
/*
{ type: 'any', flags: { default: 'Generate random number' } }
*/

What did you expect ?

I would like to add details so that describe instead returns:

schema.describe();
/*
{ type: 'any', flags: { default: {
    description: 'Generate random number',
    function: randomNumber
} } }
*/

This would allow for this contrived example to work:

schema.describe().flags.default.function(); // 0.6658680107310528

(Full disclosure, I would like to allow for dynamic defaults in Felicity, which is built upon .describe())

Any feedback on this would be appreciated, and I'm 100% willing to add this support if approved.

@WesTyler
Copy link
Contributor Author

WesTyler commented Mar 7, 2017

If the above implementation is not acceptable, but the general goal of the request (having access to the default function in the describe), I'm open to suggestions.

One thing I hadn't decided on is this behavior for stringDefault:

const makeAString = function() { return 'made a string'; };
makeAString.decription = 'Makes strings';

const stringDefault = Joi.any().default('just a string');
const funcDefault = Joi.any().default(makeAString);

funcDefault.describe();
/*
{ type: 'any', flags: { default: {
    description: 'Makes strings',
    function: makeAString
} } }
*/

stringDefault.describe();
/*
{ type: 'any', flags: { default: {
    description: 'just a string'
} } }
--OR--
{ type: 'any', flags: { default: 'just a string' } }
*/

@AdriVanHoudt
Copy link
Contributor

If this entails a simple change @

joi/lib/any.js

Line 645 in 37b8dc6

description.flags[flag] = this._flags[flag].description;

I don't see why not

@WesTyler
Copy link
Contributor Author

WesTyler commented Mar 8, 2017

That's exactly where I was looking :)

else if (typeof this._flags[flag] === 'function') {
    description.flags[flag] = {
        description: this._flags[flag].description,
        function   : this._flags[flag]
    }
}

[Redacted due to developer cough me cough error]

Going to be pushing up a PR for this sometime this afternoon.

@Marsup Marsup added the request label Mar 18, 2017
@Marsup Marsup added this to the 11.0.0 milestone Jul 14, 2017
@Marsup Marsup self-assigned this Jul 14, 2017
@Marsup Marsup closed this as completed Jul 14, 2017
@hueniverse hueniverse added feature New functionality or improvement and removed request labels Sep 19, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Mar 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

4 participants