joi icon indicating copy to clipboard operation
joi copied to clipboard

building Joi from spec fails when example object contains type property

Open rainder opened this issue 5 years ago • 2 comments

Support plan

  • is this issue currently blocking your project? (yes/no): yes
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: 12.18.3
  • module version with issue: 17.1.1
  • last module version without issue: -
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): standalone
  • any other relevant information:

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

Trying to build Joi object from JSON specification which contains examples. When example is set to an object which contains property type - build process fails with an exception.

// succeeds
Joi.build({
  type: 'object',
  examples: [ { name: 'some-type' } ],
  keys: { name: { type: 'string' } }
});

// fails with TypeError: this.joi[desc.type] is not a function
Joi.build({
  type: 'object',
  examples: [ { type: 'some-type' } ],
  keys: { type: { type: 'string' } }
});

Failing example using .describe()

Joi.build(
  Joi.object({
    type: J.string(),
  }).example({
    type: 'some-type',
  }).describe(),
);

What was the result you got?

the error was thrown

TypeError: this.joi[desc.type] is not a function
    at internals.Builder.parse (node_modules/@hapi/joi/lib/manifest.js:263:41)
    at internals.Builder.build (node_modules/@hapi/joi/lib/manifest.js:449:25)
    at /Users/user/Developer/project/node_modules/@hapi/joi/lib/manifest.js:393:44
    at Array.map (<anonymous>)
    at internals.Builder.build (node_modules/@hapi/joi/lib/manifest.js:393:25)
    at internals.Builder.parse (node_modules/@hapi/joi/lib/manifest.js:374:31)
    at Object.exports.build (node_modules/@hapi/joi/lib/manifest.js:242:20)

What result did you expect?

Joi object should be successfully created regardless of the properties defined in the example object.

rainder avatar Oct 06 '20 14:10 rainder

@rainder Yeah Joi thought the example was a schema. A simple fix would be to change the output of describe(), but that would be a breaking change.

brianle1301 avatar Oct 07 '20 14:10 brianle1301

Does this still need to be addressed? And do you think there's a way without causing a breaking change? @brianle1301

ChrisSMendoza avatar Feb 26 '23 07:02 ChrisSMendoza