hapijs / hapijs/joi

building Joi from spec fails when example object contains type property

Open
#2,480 2 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
21.2k
Forks
1.5k
Avg merge
4h 57m
Merged PRs (30d)
14

Description

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

```js
// 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()
```js
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 ()
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.