hapijs / hapijs/joi

extension object fails validation when used twice

Open
#2,514 5 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

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

#### Context

* *node version*: 14.x
* *module version with issue*: 17.3.x
* *last module version without issue*: 16.x
* *environment* (e.g. node, browser, native): all
* *used with* (e.g. hapi application, another framework, standalone, ...): standalone
* *any other relevant information*: input validation issue

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

Trying to pass an extension object instance (rather than a generator function) to Joi twice results in a validation error [due to a modification made by Joi](https://github.com/sideway/joi/blob/9cffab90039c313ca62d473d01993b492dd4d007/lib/extend.js#L111).

```js
import joi from 'https://cdn.skypack.dev/joi@~17.3.0';

const extension = {
type: 'foo',
base: joi.string(),
rules: {
bar: {
args: [{
name: 'baz',
assert: joi.string()
}],
validate(value, helpers, args, rule){
return { value }
}
}
}
}

// assume this is in the first library that uses Joi
const Joi = joi.extend(extension)

// assume this is another library or another direct usage. this call will produce an error
const Joi2 = joi.extend(extension)
```

#### What was the result you got?
Validation error.

#### What result did you expect?
For extension objects to be reusable without errors. Extend seems as if it should not have side effects on the extension objects passed.

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.