Skip to content

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

@WesTyler

Description

@WesTyler

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew functionality or improvement

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions