alternatives should return errors
- 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 -> we have temporary workaround (patch-package)
* *is this issue affecting a production system?* (yes/no): yes
#### Context
* *module version*: 17.4.0
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...): enjoi, asyncapi specification
#### What problem are you trying to solve?
I generate validators using enjoi and asyncapi. All of validators use `alternatives` (its caused by asyncapi specification). My problem is that it wont return errors.
#### Do you have a new or modified API suggestion to solve the problem?
Im using `patch-package` to overwrite one file to achieve expected behaviour. Here is commit file which I use.
```patch
diff --git a/node_modules/joi/lib/types/alternatives.js b/node_modules/joi/lib/types/alternatives.js
index 875f2b0..50bd9d9 100755
--- a/node_modules/joi/lib/types/alternatives.js
+++ b/node_modules/joi/lib/types/alternatives.js
@@ -46,6 +46,7 @@ module.exports = Any.extend({
if (schema._flags.match) {
const matched = [];
+ const errors = [];
for (let i = 0; i < schema.$_terms.matches.length; ++i) {
const item = schema.$_terms.matches[i];
@@ -57,20 +58,21 @@ module.exports = Any.extend({
matched.push(result.value);
}
else {
+ errors.push(result.errors.toString());
localState.restore();
}
}
if (matched.length === 0) {
- return { errors: error('alternatives.any') };
+ return { errors: error('alternatives.any', { errors }) };
}
if (schema._flags.match === 'one') {
- return matched.length === 1 ? { value: matched[0] } : { errors: error('alternatives.one') };
+ return matched.length === 1 ? { value: matched[0] } : { errors: error('alternatives.one', { errors }) };
}
if (matched.length !== schema.$_terms.matches.length) {
- return { errors: error('alternatives.all') };
+ return { errors: error('alternatives.all', { errors }) };
}
const allobj = schema.$_terms.matches.reduce((acc, v) => acc && v.schema.type === 'object', true);
```
Contributor guide
Research direction
Start in node_modules/joi/lib/types/alternatives.js at the alternatives match handling shown in the issue, and reproduce the missing errors through the reported enjoi and AsyncAPI validator setup. Done means failed alternatives expose the relevant underlying branch errors for the affected match modes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100