joi
joi copied to clipboard
alternatives should return errors
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.
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);