{"action":"created","issue":{"url":"https://api.github.com/repos/hapijs/joi/issues/902","repository_url":"https://api.github.com/repos/hapijs/joi","labels_url":"https://api.github.com/repos/hapijs/joi/issues/902/labels{/name}","comments_url":"https://api.github.com/repos/hapijs/joi/issues/902/comments","events_url":"https://api.github.com/repos/hapijs/joi/issues/902/events","html_url":"https://github.com/hapijs/joi/pull/902","id":156804949,"number":902,"title":"Proposal: Add support for global refs","user":{"login":"jparkerCAA","id":2473742,"avatar_url":"https://avatars.githubusercontent.com/u/2473742?v=3","gravatar_id":"","url":"https://api.github.com/users/jparkerCAA","html_url":"https://github.com/jparkerCAA","followers_url":"https://api.github.com/users/jparkerCAA/followers","following_url":"https://api.github.com/users/jparkerCAA/following{/other_user}","gists_url":"https://api.github.com/users/jparkerCAA/gists{/gist_id}","starred_url":"https://api.github.com/users/jparkerCAA/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jparkerCAA/subscriptions","organizations_url":"https://api.github.com/users/jparkerCAA/orgs","repos_url":"https://api.github.com/users/jparkerCAA/repos","events_url":"https://api.github.com/users/jparkerCAA/events{/privacy}","received_events_url":"https://api.github.com/users/jparkerCAA/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2016-05-25T17:20:13Z","updated_at":"2016-05-25T21:48:25Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/hapijs/joi/pulls/902","html_url":"https://github.com/hapijs/joi/pull/902","diff_url":"https://github.com/hapijs/joi/pull/902.diff","patch_url":"https://github.com/hapijs/joi/pull/902.patch"},"body":"I have been hampered by the inability of a ref to refer to anything up the object tree. I have many use cases where this limitation would require me to repeat a lot of schema definition. I think refs become much more powerful if there is an option to evaluate the ref from the root of the original value passed for validation. \r\n\r\nThis is a working implementation that is simple and backwards compatible. When defining a reference, a user need only add the root prefix (similar to the context prefix) as shown below\r\n\r\n\r\n```js\r\nvar Joi = require('joi')\r\n\r\nvar schema = Joi.object().keys({\r\n  a: Joi.number(),\r\n  b: Joi.array().items({\r\n    c: Joi.when('/a', {\r\n      is: 1,\r\n      then: Joi.string().valid(['x'])\r\n    })\r\n  })\r\n});\r\n\r\nvar data = { a: 1, b: [{c: 'y'}] };\r\n\r\nJoi.validate(data, schema);\r\n\r\n// =>\r\n// { error: \r\n//    { [ValidationError: child \"b\" fails because [\"b\" at position 0 fails because [child \"c\" fails because [\"c\" must be one of [x]]]]]\r\n//      isJoi: true,\r\n//      name: 'ValidationError',\r\n//      details: [ [Object] ],\r\n//      _object: { a: 1, b: [Object] },\r\n//      annotate: [Function] },\r\n//   value: { a: 1, b: [ [Object] ] } }\r\n\r\n``` \r\n\r\n"},"comment":{"url":"https://api.github.com/repos/hapijs/joi/issues/comments/221718302","html_url":"https://github.com/hapijs/joi/pull/902#issuecomment-221718302","issue_url":"https://api.github.com/repos/hapijs/joi/issues/902","id":221718302,"user":{"login":"jparkerCAA","id":2473742,"avatar_url":"https://avatars.githubusercontent.com/u/2473742?v=3","gravatar_id":"","url":"https://api.github.com/users/jparkerCAA","html_url":"https://github.com/jparkerCAA","followers_url":"https://api.github.com/users/jparkerCAA/followers","following_url":"https://api.github.com/users/jparkerCAA/following{/other_user}","gists_url":"https://api.github.com/users/jparkerCAA/gists{/gist_id}","starred_url":"https://api.github.com/users/jparkerCAA/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jparkerCAA/subscriptions","organizations_url":"https://api.github.com/users/jparkerCAA/orgs","repos_url":"https://api.github.com/users/jparkerCAA/repos","events_url":"https://api.github.com/users/jparkerCAA/events{/privacy}","received_events_url":"https://api.github.com/users/jparkerCAA/received_events","type":"User","site_admin":false},"created_at":"2016-05-25T21:48:25Z","updated_at":"2016-05-25T21:48:25Z","body":"Understood. However, I fail to understand how this approach doesn't support that in a reasonable fashion. Given the following example: \r\n```js\r\nvar reusableSchema = Joi.ref('/a.b') \r\n\r\nvar compoundSchema1 = Joi.object({\r\n  a: Joi.object({\r\n    b: Joi.number()\r\n  }),\r\n  c: Joi.object({\r\n    d: Joi.valid(reusableSchema)\r\n  }),\r\n  e: Joi.object({\r\n    a: Joi.object({\r\n      b: Joi.any(),\r\n      f: Joi.valid(reusableSchema)\r\n    })\r\n  })\r\n})\r\n\r\n```\r\n\r\nIs your concern that the ref in the reused schema at ```e.a.f``` would resolve a different value for '/a.b' than when it is used at ```c.d```? "}}
{"actor":{"display_login":"jparkerCAA"}}