{"action":"created","issue":{"url":"https://api.github.com/repos/hapijs/joi/issues/537","labels_url":"https://api.github.com/repos/hapijs/joi/issues/537/labels{/name}","comments_url":"https://api.github.com/repos/hapijs/joi/issues/537/comments","events_url":"https://api.github.com/repos/hapijs/joi/issues/537/events","html_url":"https://github.com/hapijs/joi/issues/537","id":55524651,"number":537,"title":"multiple 'when' condition support appears broken","user":{"login":"elainemacurdy","id":6642039,"avatar_url":"https://avatars.githubusercontent.com/u/6642039?v=3","gravatar_id":"","url":"https://api.github.com/users/elainemacurdy","html_url":"https://github.com/elainemacurdy","followers_url":"https://api.github.com/users/elainemacurdy/followers","following_url":"https://api.github.com/users/elainemacurdy/following{/other_user}","gists_url":"https://api.github.com/users/elainemacurdy/gists{/gist_id}","starred_url":"https://api.github.com/users/elainemacurdy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/elainemacurdy/subscriptions","organizations_url":"https://api.github.com/users/elainemacurdy/orgs","repos_url":"https://api.github.com/users/elainemacurdy/repos","events_url":"https://api.github.com/users/elainemacurdy/events{/privacy}","received_events_url":"https://api.github.com/users/elainemacurdy/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/hapijs/joi/labels/non-issue","name":"non-issue","color":"c7def8"}],"state":"closed","locked":false,"assignee":{"login":"Marsup","id":796194,"avatar_url":"https://avatars.githubusercontent.com/u/796194?v=3","gravatar_id":"","url":"https://api.github.com/users/Marsup","html_url":"https://github.com/Marsup","followers_url":"https://api.github.com/users/Marsup/followers","following_url":"https://api.github.com/users/Marsup/following{/other_user}","gists_url":"https://api.github.com/users/Marsup/gists{/gist_id}","starred_url":"https://api.github.com/users/Marsup/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Marsup/subscriptions","organizations_url":"https://api.github.com/users/Marsup/orgs","repos_url":"https://api.github.com/users/Marsup/repos","events_url":"https://api.github.com/users/Marsup/events{/privacy}","received_events_url":"https://api.github.com/users/Marsup/received_events","type":"User","site_admin":false},"milestone":null,"comments":12,"created_at":"2015-01-26T19:19:14Z","updated_at":"2015-06-09T06:34:30Z","closed_at":"2015-03-24T14:44:53Z","body":"Joi version: 5.1\r\n\r\nHello. I'm trying to achieve something like this:\r\n\r\n```js\r\n{\r\n  'a': Joi.string().valid('foo', 'bar'),\r\n  'b': Joi.number()\r\n      .when('a', { is: 'foo', then: Joi.required() })\r\n      .when('a', { is: 'bar', then: Joi.forbidden() })\r\n}\r\n```\r\n\r\nHowever stepping through the code, it appears to exit after the first `when` condition, whether . I added the following unit test to test/any.js. The first set of tests is a simple example: the second `when` clause making the field `forbidden` is ignored. The second set of tests is based on the example in conditionalRequire.js, and fails erroneously on the last two tests.\r\n\r\n```js\r\n        it('supports multiple when clauses', function (done) {\r\n            // very simple test\r\n            var schema = {\r\n                a: Joi.string().valid('foo', 'bar'),\r\n                b: Joi.number()\r\n                    .when('a', { is: 'foo', then: Joi.required() })\r\n                    .when('a', { is: 'bar', then: Joi.forbidden() })\r\n            };\r\n\r\n            Helper.validate(schema, [\r\n                [{ a: 'foo' }, false], // a is required: pass\r\n                [{ a: 'bar' }, true], // a is forbidden: pass\r\n                [{ a: 'foo', b: 10 }, true], // a is required: pass\r\n                [{ a: 'bar', b: 10 }, false] // a is forbidden: FAIL\r\n            ]);\r\n\r\n\r\n            // test based on examples/conditionalRequire.js\r\n\r\n            // This is a valid value for integer rating 1 - 5\r\n            var intRating = Joi.number().integer().min(1).max(5);\r\n\r\n            var schema = Joi.object().keys({\r\n                // Do you know any French people? yes or no (required)\r\n                q1: Joi.boolean().required(),\r\n                // Do you know any Parisians? yes or no (required if answered yes in q1)\r\n                q2: Joi.boolean()\r\n                    .when('q1', { is: true, then: Joi.required() }),\r\n                // How many french in paris do you know? 1-6, 6-10, 11-50 or 50+ (required if answered yes in q2)\r\n                q3: Joi.string()\r\n                    .when('q2', { is: true, then: Joi.valid('1-5', '6-10', '11-50', '50+').required() }),\r\n                // Rate 20% of most friendly Parisians, from how many people you know answered in q3, individually on 1-5 rating\r\n                q4: Joi.array()\r\n                    .when('q3', {is: '1-5', then: Joi.array().min(0).max(1).includes(intRating).required() })\r\n                    .when('q3', {is: '6-10', then: Joi.forbidden() })\r\n                    .when('q3', {is: '11-50', then: Joi.array().min(0).max(10).includes(intRating).required() })\r\n                    .when('q3', {is: '50+' , then: Joi.array().min(0).includes(intRating).required() })\r\n            });\r\n\r\n            Helper.validate(schema, [\r\n                [{ // q4 is required: pass\r\n                    q1: true,\r\n                    q2: true,\r\n                    q3: '1-5',\r\n                    q4: [5]\r\n                }, true],\r\n                [{ // q4 is forbidden: pass\r\n                    q1: true,\r\n                    q2: true,\r\n                    q3: '6-10'\r\n                }, true],\r\n                [{ // q4 is required: pass\r\n                    q1: true,\r\n                    q2: true,\r\n                    q3: '11-50',\r\n                    q4: [5]\r\n                }, true],\r\n                [{ // q4 is required: pass\r\n                    q1: true,\r\n                    q2: true,\r\n                    q3: '50+',\r\n                    q4: [2]\r\n                },  true],\r\n                [{ // q4 is forbidden: FAIL\r\n                    q1: true,\r\n                    q2: true,\r\n                    q3: '6-10',\r\n                    q4: [5]\r\n                }, false],\r\n                [{ // q4 is required: FAIL\r\n                    q1: true,\r\n                    q2: true,\r\n                    q3: '11-50'\r\n                }, false]\r\n            ], done);\r\n        });\r\n    });\r\n```\r\n\r\nAfter a little digging I suspect the problem is something to do with any.js, line 268: when setting `otherwise`, it defaults to `this` if no otherwise clause is specified by the user. This means that down the food chain in alternatives.js line 36, after it fails to match the current ref, it thinks it has an `otherwise` clause to execute instead of continuing on to the next `when`.\r\n\r\nIf I'm doing this wrong or misunderstanding the intention of the feature, please educate me! It'd be lovely to get multiple conditions working."},"comment":{"url":"https://api.github.com/repos/hapijs/joi/issues/comments/110245119","html_url":"https://github.com/hapijs/joi/issues/537#issuecomment-110245119","issue_url":"https://api.github.com/repos/hapijs/joi/issues/537","id":110245119,"user":{"login":"Marsup","id":796194,"avatar_url":"https://avatars.githubusercontent.com/u/796194?v=3","gravatar_id":"","url":"https://api.github.com/users/Marsup","html_url":"https://github.com/Marsup","followers_url":"https://api.github.com/users/Marsup/followers","following_url":"https://api.github.com/users/Marsup/following{/other_user}","gists_url":"https://api.github.com/users/Marsup/gists{/gist_id}","starred_url":"https://api.github.com/users/Marsup/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Marsup/subscriptions","organizations_url":"https://api.github.com/users/Marsup/orgs","repos_url":"https://api.github.com/users/Marsup/repos","events_url":"https://api.github.com/users/Marsup/events{/privacy}","received_events_url":"https://api.github.com/users/Marsup/received_events","type":"User","site_admin":false},"created_at":"2015-06-09T06:34:30Z","updated_at":"2015-06-09T06:34:30Z","body":"The problem might be the example, not joi. Multiple whens work with the syntax I linked a few comments before. I don't have time to fix the example right now so if you do, PR welcome."}}