hapijs / hapijs/joi

Retrieve list of valid values given a specific key from a list of keys in a nested object

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

Description

#### Context

* *node version*: v16.14.2
* *module version*: 17.6.0
* *environment* (e.g. node, browser, native): Node
* *used with* (e.g. hapi application, another framework, standalone, ...): Standalone

#### How can we help?

Given the following schema:

```js
schema = Joi.object({
foo: Joi.string().valid('Lorem', 'Ipsom'),
bar: Joi.when('foo',[
{
is: 'Lorem',
then: Joi.string().valid(
'dolor',
'sit',
'amet'
)
},
{
is: 'Ipsom',
then: Joi.string().valid(
'Aut',
'atque',
'consectetur'
)
}
]
baz: Joi.when('bar',[
{
is: [
'dolor',
'sit',
],
then: Joi.string().valid(
'Eos',
'voluptas',
'fugiat',
)
}
{
is: 'amet',
then: Joi.string().valid(
'aut',
'accusamus',
'laboriosam',
)
}
{
is: [
'Aut',
'atque',
'consectetur'
],
then: Joi.string().valid(
'distinctio',
'dolores',
'asperiores',
)
}
]
})
```
I want to be able to given a string key, e.g 'dolor', I can retrieve the list of possible values easily. I can do this by adding an ID to the "then" statement, but this way I have to build a group reference and I cannot directly use my key to retrieve the list of possible values, I was expecting to do something like:

```js
const validValuesforBaz = this.schema.extract(`foo.${foo.Lorem}.bar.${bar.dolor}.baz`).describe()['allow'];
```

Is there a way I can use my object keys to retrieve the list of possible values? Do I need to change my schema?

Contributor guide

Open the contributing guide

Research direction

Start with the schema.extract(...) and describe()['allow'] entry points shown in the example, alongside the Joi.when conditional branches and their then schemas. Determine whether a string key can address nested conditional values without IDs or whether the schema must change; done means the supported lookup returns the expected valid values for baz.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.