hapijs / hapijs/joi

Enums With Invalid

Open
#2,759 4 comments 0 reactions 1 assignee Claimed by @Marsup View on GitHub
support
Dominant language
JavaScript
Stars
21.2k
Forks
1.5k
Avg merge
4h 57m
Merged PRs (30d)
14

Description

#### Context

* *node version*: 16.13.1
* *module version*: 17.6.0
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...): standalone
* *any other relevant information*:

#### How can we help?

Hi, as mentioned in #2526, it seems like Joi.valid() is what we should use for checking aginst an enum. I also understand that mixing the use of invalid() and valid() could easily result in errors.

Say I have a request with two inputs, prev and current, both of them need to adhere to some sort of enum. Since this request signals a change in state, prev and current must be different. Is there any way to achieve this goal?

```js
const some = Joi.object({
prev: Joi.string().valid(
"enum1",
"enum2"
).required(),
current: Joi.string().invalid(Joi.ref("prev")).valid(
"enum1",
"enum2"
).required(),
}).options({
abortEarly: true
});
```

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.