{"action":"opened","issue":{"url":"https://api.github.com/repos/hapijs/joi/issues/1437","repository_url":"https://api.github.com/repos/hapijs/joi","labels_url":"https://api.github.com/repos/hapijs/joi/issues/1437/labels{/name}","comments_url":"https://api.github.com/repos/hapijs/joi/issues/1437/comments","events_url":"https://api.github.com/repos/hapijs/joi/issues/1437/events","html_url":"https://github.com/hapijs/joi/issues/1437","id":297588870,"number":1437,"title":"Error on validating Alternatives: Object or Array of Objects","user":{"login":"bernardocs","id":513789,"avatar_url":"https://avatars1.githubusercontent.com/u/513789?v=4","gravatar_id":"","url":"https://api.github.com/users/bernardocs","html_url":"https://github.com/bernardocs","followers_url":"https://api.github.com/users/bernardocs/followers","following_url":"https://api.github.com/users/bernardocs/following{/other_user}","gists_url":"https://api.github.com/users/bernardocs/gists{/gist_id}","starred_url":"https://api.github.com/users/bernardocs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bernardocs/subscriptions","organizations_url":"https://api.github.com/users/bernardocs/orgs","repos_url":"https://api.github.com/users/bernardocs/repos","events_url":"https://api.github.com/users/bernardocs/events{/privacy}","received_events_url":"https://api.github.com/users/bernardocs/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-02-15T20:39:48Z","updated_at":"2018-02-15T20:39:48Z","closed_at":null,"author_association":"NONE","body":"#### Context\r\n\r\n* *node version*: 8.6.0\r\n* *joi version*: 13.1.2\r\n* *environment* (node, browser): node\r\n* *used with* (hapi, standalone, ...): express-validation\r\n\r\n#### What are you trying to achieve or the steps to reproduce ?\r\n\r\nI'm simply trying to add a validation to a specific route on my Express API that accepts both a **Object** for a _single value_ or a **Array of Objects** for multiple values. I came here searching for answers and I achieved this schema:\r\n\r\n`validation.js`\r\n```js\r\nconst objSchema = Joi.object().keys({\r\n  count: Joi.number().integer().required(),\r\n  domain: Joi.string().trim().required(),\r\n  pond: Joi.string().trim().required(),\r\n  date: Joi.date().required(),\r\n  origin: Joi.boolean().required(),\r\n});\r\n\r\nconst arraySchema = Joi.array().items(objSchema).required();\r\n\r\nmodule.exports = {\r\n  // ... others schemas\r\n  create: {\r\n    body: Joi.alternatives().try(objSchema, arraySchema).required().options({}),\r\n  }\r\n};\r\n```\r\n\r\n`routes.js`\r\n```js\r\nconst eValidation = require('express-validation');\r\nconst validation = require('../validation');\r\nconst controller = require('../controller');\r\n\r\nmodule.exports = function (app) {\r\n  // ... other routes\r\n  app.post('/',\r\n    eValidation(validation.create),\r\n    controller.create);\r\n}\r\n```\r\n\r\nAnd then, making the request either by `curl` or Postman. I tried sending both a single object and a array of objects with the correct schema.\r\n\r\n#### Which result you had ?\r\nOn both trials I got this:\r\n```\r\nError: \"context\" must be an object\r\n    at internals.Alternatives.checkOptions ([work-dir]/api/node_modules/joi/lib/types/any/index.js:96:19)\r\n    at internals.Alternatives._validateWithOptions ([work-dir]/api/node_modules/joi/lib/types/any/index.js:663:18)\r\n    at module.exports.internals.Any.root.validate ([work-dir]/api/node_modules/joi/lib/index.js:139:23)\r\n    at validate ([work-dir]/api/node_modules/express-validation/lib/index.js:75:7)\r\n    at [work-dir]/api/node_modules/express-validation/lib/index.js:42:24\r\n    at Array.forEach (<anonymous>)\r\n    at [work-dir]/api/node_modules/express-validation/lib/index.js:39:55\r\n    at Layer.handle [as handle_request] ([work-dir]/api/node_modules/express/lib/router/layer.js:95:5)\r\n    at next ([work-dir]/api/node_modules/express/lib/router/route.js:137:13)\r\n    at Route.dispatch ([work-dir]/api/node_modules/express/lib/router/route.js:112:3)\r\n```\r\n#### What did you expect ?\r\nI just want to simply validate a object/array of object... :(\r\nNotice that the validation worked on the `GET` request, which demanded a simple object with 3 specific properties."}}
{"actor":{"display_login":"bernardocs"}}