hapijs / hapijs/joi

Conditional presence

Open
#2,412 1 comment 0 reactions 0 assignees View on GitHub
feature
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 but not critical
* *is this issue affecting a production system?* (yes/no): No

#### Context

* *node version*: 10.16.3
* *module version*: 17.1.1
* *environment* (e.g. node, browser, native): Node
* *used with* (e.g. hapi application, another framework, standalone, ...): Express with Apollo GraphQL
* *any other relevant information*:

#### What problem are you trying to solve?

Current "required if" solution is not quite simple. I think that general needs should not be that complex. (Or shorthand)

```js
Joi.when("a", { is: "someValue", then: Joi.required() })
```

#### Do you have a new or modified API suggestion to solve the problem?
`c` is only required if `a` is equal to `someValue`.
We can access parent properties with `parent` and `c` with `value`.
```js
Joi.object({
a: Joi.string().required(),
b: Joi.array()
c: Joi.string().required((parent, value) => {
// parent: a, b
// value: c

return parent.a === "someValue";
})
})
```

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.