{"action":"created","issue":{"url":"https://api.github.com/repos/vslinko/vstack-validator/issues/2","repository_url":"https://api.github.com/repos/vslinko/vstack-validator","labels_url":"https://api.github.com/repos/vslinko/vstack-validator/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/vslinko/vstack-validator/issues/2/comments","events_url":"https://api.github.com/repos/vslinko/vstack-validator/issues/2/events","html_url":"https://github.com/vslinko/vstack-validator/issues/2","id":138426529,"number":2,"title":"Conditional constraints","user":{"login":"ooflorent","id":168498,"avatar_url":"https://avatars.githubusercontent.com/u/168498?v=3","gravatar_id":"","url":"https://api.github.com/users/ooflorent","html_url":"https://github.com/ooflorent","followers_url":"https://api.github.com/users/ooflorent/followers","following_url":"https://api.github.com/users/ooflorent/following{/other_user}","gists_url":"https://api.github.com/users/ooflorent/gists{/gist_id}","starred_url":"https://api.github.com/users/ooflorent/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ooflorent/subscriptions","organizations_url":"https://api.github.com/users/ooflorent/orgs","repos_url":"https://api.github.com/users/ooflorent/repos","events_url":"https://api.github.com/users/ooflorent/events{/privacy}","received_events_url":"https://api.github.com/users/ooflorent/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-03-04T09:29:31Z","updated_at":"2016-03-04T12:03:51Z","closed_at":null,"body":"I'm trying to create a schema with conditional constraints and I'm unable to figure out how to do it. My data looks like this:\r\n\r\n```js\r\n{\r\n  title: \"some title\",\r\n  items: [\r\n    {type: 0, foo: \"foo\", bar: \"bar\"},\r\n    {type: 3, baz: \"baz\"},\r\n    {type: 2, qux: \"qux\"},\r\n  ]\r\n}\r\n```\r\n\r\nI tried to do this:\r\n\r\n```js\r\nschema.type(\"complex\", {\r\n  title: {\r\n    notEmpty: schema.isNotEmpty(),\r\n  },\r\n  items: {\r\n    minLength: schema.minLength(1),\r\n    values: schema.list(itemSchema),\r\n  },\r\n})\r\n```\r\n\r\nUnfortunately I don't know how `itemSchema` must be declared. I have a function which retrieve a schema based on the `type` but I can't see where I could call it...\r\n\r\n```js\r\nfunction getSchemaByType(type) {\r\n  switch (type) {\r\n    case 0: return someSchema\r\n    case 1: return otherSchema\r\n    // ...\r\n  }\r\n}\r\n```\r\n\r\nCould you point me in the right direction?"},"comment":{"url":"https://api.github.com/repos/vslinko/vstack-validator/issues/comments/192255367","html_url":"https://github.com/vslinko/vstack-validator/issues/2#issuecomment-192255367","issue_url":"https://api.github.com/repos/vslinko/vstack-validator/issues/2","id":192255367,"user":{"login":"vslinko","id":205823,"avatar_url":"https://avatars.githubusercontent.com/u/205823?v=3","gravatar_id":"","url":"https://api.github.com/users/vslinko","html_url":"https://github.com/vslinko","followers_url":"https://api.github.com/users/vslinko/followers","following_url":"https://api.github.com/users/vslinko/following{/other_user}","gists_url":"https://api.github.com/users/vslinko/gists{/gist_id}","starred_url":"https://api.github.com/users/vslinko/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vslinko/subscriptions","organizations_url":"https://api.github.com/users/vslinko/orgs","repos_url":"https://api.github.com/users/vslinko/repos","events_url":"https://api.github.com/users/vslinko/events{/privacy}","received_events_url":"https://api.github.com/users/vslinko/received_events","type":"User","site_admin":false},"created_at":"2016-03-04T12:03:51Z","updated_at":"2016-03-04T12:03:51Z","body":"Hello!\r\n\r\nI created an example for your use-case. There is some points:\r\n- `schema.isNotEmpty` is a fully prepared `Constraint`, you should provide it as value. In opposite `schema.minLength` is \"Constraint Creator\".\r\n- You can implement _almost_ anything you want using regular function composition."}}