Access global object property from relative property key
- 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): no
#### Context
* *node version*: V14.latest
* *module version*: 17.latest
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...): with express, but the issue exists as standalone
* *any other relevant information*:
#### How can we help?
I'm trying to set a default value of a property based off of a global context passed through at `validate` and an existing property of the object being validated. I just can't seem to use the value of the property of the object as a key for the global context.
```js
const context = {
letters: {
a: 1,
b: 2,
c: 3,
},
};
const s = Joi.object().keys({
letter: Joi.string().valid(...['a', 'b', 'c']),
num: Joi.number().default(Joi.expression('{$letters}.{letter}'))
})
const test = {
letter: 'a',
}
console.log(s.validate(test, { context }));
```
Simple example above. I have an object called `letters` that I place into the context. The schema will look for `letter`, then try to set a default value for `num` using the global letters as the object to pull from and the letter passed in as the key. The closest I can get is `{ value: { letter: 'a', num: '[object Object].a' } }`
Contributor guide
Research direction
The report provides no repository file or test path. Start by reproducing the Joi.expression default in the supplied Node example and inspect the Joi.expression and validate entry points. Done means a relative property key selects the matching value from the global letters context, producing num: 1 for letter: 'a'.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100