{"action":"created","issue":{"url":"https://api.github.com/repos/hapijs/joi/issues/1036","repository_url":"https://api.github.com/repos/hapijs/joi","labels_url":"https://api.github.com/repos/hapijs/joi/issues/1036/labels{/name}","comments_url":"https://api.github.com/repos/hapijs/joi/issues/1036/comments","events_url":"https://api.github.com/repos/hapijs/joi/issues/1036/events","html_url":"https://github.com/hapijs/joi/issues/1036","id":189480438,"number":1036,"title":"Recursive complex object structure with property type control.","user":{"login":"CodeMedic42","id":7494201,"avatar_url":"https://avatars.githubusercontent.com/u/7494201?v=3","gravatar_id":"","url":"https://api.github.com/users/CodeMedic42","html_url":"https://github.com/CodeMedic42","followers_url":"https://api.github.com/users/CodeMedic42/followers","following_url":"https://api.github.com/users/CodeMedic42/following{/other_user}","gists_url":"https://api.github.com/users/CodeMedic42/gists{/gist_id}","starred_url":"https://api.github.com/users/CodeMedic42/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CodeMedic42/subscriptions","organizations_url":"https://api.github.com/users/CodeMedic42/orgs","repos_url":"https://api.github.com/users/CodeMedic42/repos","events_url":"https://api.github.com/users/CodeMedic42/events{/privacy}","received_events_url":"https://api.github.com/users/CodeMedic42/received_events","type":"User","site_admin":false},"labels":[{"id":24848505,"url":"https://api.github.com/repos/hapijs/joi/labels/question","name":"question","color":"d4c5f9","default":true}],"state":"open","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},"assignees":[{"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":9,"created_at":"2016-11-15T19:15:20Z","updated_at":"2016-11-16T15:41:32Z","closed_at":null,"body":"#### Context\r\n\r\n* *node version*: 6.3.1\r\n* *joi version*: 9.2.0\r\n* *environment* (node, browser): node\r\n* *used with* (hapi, standalone, ...): standalone\r\n* *any other relevant information*: Nope\r\n\r\n#### What are you trying to achieve or the steps to reproduce ?\r\n\r\nI have the same structure in many areas and have created the following function.\r\n\r\n```\r\nconst standardPropertyPattern = /^[\\w\\d]+$/;\r\n\r\nfunction buildDataSchema(...args) {\r\n    const dataSchema = Joi.object().pattern(standardPropertyPattern, Joi.valid(\r\n        ...args,\r\n        Joi.array().items(Joi.lazy(() => dataSchema)),\r\n        Joi.lazy(() => dataSchema)));\r\n\r\n    return dataSchema;\r\n}\r\n```\r\n\r\nUsage looks like\r\n```\r\n\r\nconst schema1 = buildDataSchema(Joi.string(), Joi.number());\r\nconst schema2 = buildDataSchema(Joi.string(), Joi.number(), Joi.boolean(), null);\r\nconst schema3 = buildDataSchema(Joi.string());\r\n\r\n// etc...\r\n```\r\n\r\nBasically this allows me to control the types of the leaves of the object tree as well as limit the property name format.\r\n\r\nUnfortunately the object.pattern feature does not seem to like Joi.valid.\r\n\r\n#### Which result you had ?\r\n\r\nThe data I am validating is:\r\n\r\n```\r\nconst testData = {\r\n  user: 'foo',\r\n  test: {\r\n    foo: [1, 2, 3],\r\n    bar: true\r\n  },\r\n  other: null \r\n};\r\n```\r\n\r\nI then call ...\r\n\r\n`const ret = Joi.validate(testdata, schema2);`\r\n\r\nI am getting the following message for \r\n\r\n`ret.error.message`\r\n\r\n> \"user\" must be one of [[object Object], [object Object], [object Object], null, [object Object], [object Object]]\r\n\r\n#### What did you expect ?\r\n\r\nI expected the given data to pass validation."},"comment":{"url":"https://api.github.com/repos/hapijs/joi/issues/comments/260978851","html_url":"https://github.com/hapijs/joi/issues/1036#issuecomment-260978851","issue_url":"https://api.github.com/repos/hapijs/joi/issues/1036","id":260978851,"user":{"login":"CodeMedic42","id":7494201,"avatar_url":"https://avatars.githubusercontent.com/u/7494201?v=3","gravatar_id":"","url":"https://api.github.com/users/CodeMedic42","html_url":"https://github.com/CodeMedic42","followers_url":"https://api.github.com/users/CodeMedic42/followers","following_url":"https://api.github.com/users/CodeMedic42/following{/other_user}","gists_url":"https://api.github.com/users/CodeMedic42/gists{/gist_id}","starred_url":"https://api.github.com/users/CodeMedic42/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/CodeMedic42/subscriptions","organizations_url":"https://api.github.com/users/CodeMedic42/orgs","repos_url":"https://api.github.com/users/CodeMedic42/repos","events_url":"https://api.github.com/users/CodeMedic42/events{/privacy}","received_events_url":"https://api.github.com/users/CodeMedic42/received_events","type":"User","site_admin":false},"created_at":"2016-11-16T15:41:31Z","updated_at":"2016-11-16T15:41:31Z","body":"As you say it, I see it. You know sometimes you just have to ask the question so you can get the answer yourself. But in this case your help was what I needed. Thanks!\r\n\r\n```\r\nfunction buildDataSchema2() {\r\n    let dataSchema = null;\r\n    var items = []\r\n\r\n    items.push(Joi.array().items(items));\r\n    items.push(Joi.lazy(() => dataSchema));\r\n    Array.prototype.push.apply(items, arguments);\r\n\r\n    dataSchema = Joi.object().pattern(standardPropertyPattern, Joi.alternatives().try(items));\r\n\r\n    return dataSchema;\r\n}\r\n```"}}
{"actor":{"display_login":"CodeMedic42"}}