{"action":"created","issue":{"url":"https://api.github.com/repos/ansman/validate.js/issues/10","labels_url":"https://api.github.com/repos/ansman/validate.js/issues/10/labels{/name}","comments_url":"https://api.github.com/repos/ansman/validate.js/issues/10/comments","events_url":"https://api.github.com/repos/ansman/validate.js/issues/10/events","html_url":"https://github.com/ansman/validate.js/issues/10","id":47096073,"number":10,"title":"Promises and rejections","user":{"login":"sebastianseilund","id":744493,"avatar_url":"https://avatars.githubusercontent.com/u/744493?v=3","gravatar_id":"","url":"https://api.github.com/users/sebastianseilund","html_url":"https://github.com/sebastianseilund","followers_url":"https://api.github.com/users/sebastianseilund/followers","following_url":"https://api.github.com/users/sebastianseilund/following{/other_user}","gists_url":"https://api.github.com/users/sebastianseilund/gists{/gist_id}","starred_url":"https://api.github.com/users/sebastianseilund/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sebastianseilund/subscriptions","organizations_url":"https://api.github.com/users/sebastianseilund/orgs","repos_url":"https://api.github.com/users/sebastianseilund/repos","events_url":"https://api.github.com/users/sebastianseilund/events{/privacy}","received_events_url":"https://api.github.com/users/sebastianseilund/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2014-10-28T23:48:45Z","updated_at":"2015-01-19T20:09:16Z","closed_at":null,"body":"Thanks for a cool validation module.\r\n\r\nOne thing that bugs me is how `validate.async()` rejects with the errors. This is not analogous to how `validate()` works. It returns no matter the validation result, either with `undefined` for success or a hash of errors if not successful. It does not throw the errors. So `validate.async` should also do the same: Resolve with either `undefined` if successful or a hash of errors if not successful.\r\n\r\nRejecting the promise corresponds to `throw`ing in `validate()`. It indicates an application error, not a validation error.\r\n\r\nThis goes for both `validate.async` and the promise custom async validators return.\r\n\r\nFirst of all nothing should reject with random objects or strings. You should reject with a proper `Error` instance.\r\n\r\nSecondly, it becomes a problem when the validation rejection gets mixed up with application errors. Worst case scenario is that an application error containing sensitive information is leaked to a user. Here's an example of that:\r\n\r\n```js\r\nvalidate.validators.userExists = function(userId) {\r\n  return db.query(\"SELECT id FROM users WHERE ?\", [userId])\r\n    .then(function(rows) {\r\n      if (rows.length === 0) {\r\n        throw 'User does not exist';\r\n      }\r\n    })\r\n}\r\n```\r\n\r\nIf `db.query` rejects, the db error object will be included in the final validation errors hash.\r\n\r\nI would gladly submit a PR to fix this, if you want to take the library this way (I definitely think you should)."},"comment":{"url":"https://api.github.com/repos/ansman/validate.js/issues/comments/70552867","html_url":"https://github.com/ansman/validate.js/issues/10#issuecomment-70552867","issue_url":"https://api.github.com/repos/ansman/validate.js/issues/10","id":70552867,"user":{"login":"sebastianseilund","id":744493,"avatar_url":"https://avatars.githubusercontent.com/u/744493?v=3","gravatar_id":"","url":"https://api.github.com/users/sebastianseilund","html_url":"https://github.com/sebastianseilund","followers_url":"https://api.github.com/users/sebastianseilund/followers","following_url":"https://api.github.com/users/sebastianseilund/following{/other_user}","gists_url":"https://api.github.com/users/sebastianseilund/gists{/gist_id}","starred_url":"https://api.github.com/users/sebastianseilund/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sebastianseilund/subscriptions","organizations_url":"https://api.github.com/users/sebastianseilund/orgs","repos_url":"https://api.github.com/users/sebastianseilund/repos","events_url":"https://api.github.com/users/sebastianseilund/events{/privacy}","received_events_url":"https://api.github.com/users/sebastianseilund/received_events","type":"User","site_admin":false},"created_at":"2015-01-19T20:09:16Z","updated_at":"2015-01-19T20:09:16Z","body":"@dkushner The `.catch(ErrorType, function() {...})` API does not work with all promise libraries nor [ES6 Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) (i.e. catching only errors of specific types). \r\n\r\n@ansman What did you think about rejecting with an instance of `validate.ValidationError`? :)"}}