hapijs / hapijs/joi

Does joi have a short circuiting "and" mechanism?

Open
#2,675 1 comment 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

#### Support plan

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

#### Context

* *node version*: v14.17.5
* *module version*: 17.4.2 (tried other versions also)
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...): N/A
* *any other relevant information*: N/A

#### How can we help?

Is there a cleaner way to do a short circuiting "and" validation? What I really want to do is, if not `joi.string().required()` fail; otherwise, also check `joi.valid('tag').error(new SoftValidationError(''))`. This allows for a custom error if-and-only-if the prerequisites hold.

```js
joi.when({ is: joi.string().required(), then: joi.valid('tag').error(new SoftValidationError('')), otherwise: joi.string().required() })
```

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.