{"action":"opened","issue":{"url":"https://api.github.com/repos/christianalfoni/cerebral/issues/77","labels_url":"https://api.github.com/repos/christianalfoni/cerebral/issues/77/labels{/name}","comments_url":"https://api.github.com/repos/christianalfoni/cerebral/issues/77/comments","events_url":"https://api.github.com/repos/christianalfoni/cerebral/issues/77/events","html_url":"https://github.com/christianalfoni/cerebral/issues/77","id":102543039,"number":77,"title":"Validating the state","user":{"login":"saulshanabrook","id":1186124,"avatar_url":"https://avatars.githubusercontent.com/u/1186124?v=3","gravatar_id":"","url":"https://api.github.com/users/saulshanabrook","html_url":"https://github.com/saulshanabrook","followers_url":"https://api.github.com/users/saulshanabrook/followers","following_url":"https://api.github.com/users/saulshanabrook/following{/other_user}","gists_url":"https://api.github.com/users/saulshanabrook/gists{/gist_id}","starred_url":"https://api.github.com/users/saulshanabrook/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/saulshanabrook/subscriptions","organizations_url":"https://api.github.com/users/saulshanabrook/orgs","repos_url":"https://api.github.com/users/saulshanabrook/repos","events_url":"https://api.github.com/users/saulshanabrook/events{/privacy}","received_events_url":"https://api.github.com/users/saulshanabrook/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-08-22T14:54:11Z","updated_at":"2015-08-22T14:54:11Z","closed_at":null,"body":"In `baobab` you can [set up](https://github.com/Yomguithereal/baobab/tree/v2#options) a `validation` function that is called on every state change and can reject changes that do not pass the validation:\r\n\r\n```javascript\r\nfunction validationFunction(previousState, newState, affectedPaths) {\r\n  // Perform validation here and return an error if\r\n  // the tree is invalid\r\n  if (!valid)\r\n    return new Error('Invalid tree because of reasons.');\r\n}\r\n\r\nvar tree = new Baobab({...}, {validate: validationFunction});\r\n````\r\n\r\nI actually have a [JSON schema](json-schema.org/) set up for part of my state, so this is useful to me, to easily verify whether the actions have done some invalid.\r\n\r\nI was thinking it might make sense to include something like this in the core of cerebral. It would also open up the door to easier type checking of that state and some forced structure to it.\r\n\r\nWhat if the actions that modified the state, like `state.set`, `state.merge`, could return an error if you set up a validation function?\r\n\r\nOr maybe there would be a cerebral event called `invalid` that is emitted when an invalid state is saved.\r\n\r\nJust putting some ideas out here would love to hear what others think about it, I might be totally wrong."}}