hapijs / joi

The most powerful data validation library for JS
Other
20.87k stars 1.51k forks source link

building Joi from spec fails when example object contains type property #2480

Open rainder opened 3 years ago

rainder commented 3 years ago

Support plan

Context

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.

brianle1301 commented 3 years ago

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

ChrisSMendoza commented 1 year ago

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