{"action":"created","issue":{"url":"https://api.github.com/repos/jquense/yup/issues/144","repository_url":"https://api.github.com/repos/jquense/yup","labels_url":"https://api.github.com/repos/jquense/yup/issues/144/labels{/name}","comments_url":"https://api.github.com/repos/jquense/yup/issues/144/comments","events_url":"https://api.github.com/repos/jquense/yup/issues/144/events","html_url":"https://github.com/jquense/yup/issues/144","id":271653490,"number":144,"title":"`returnError` option for `validate` and `validateSync`","user":{"login":"jstrimpel","id":382516,"avatar_url":"https://avatars3.githubusercontent.com/u/382516?v=4","gravatar_id":"","url":"https://api.github.com/users/jstrimpel","html_url":"https://github.com/jstrimpel","followers_url":"https://api.github.com/users/jstrimpel/followers","following_url":"https://api.github.com/users/jstrimpel/following{/other_user}","gists_url":"https://api.github.com/users/jstrimpel/gists{/gist_id}","starred_url":"https://api.github.com/users/jstrimpel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jstrimpel/subscriptions","organizations_url":"https://api.github.com/users/jstrimpel/orgs","repos_url":"https://api.github.com/users/jstrimpel/repos","events_url":"https://api.github.com/users/jstrimpel/events{/privacy}","received_events_url":"https://api.github.com/users/jstrimpel/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2017-11-06T23:08:16Z","updated_at":"2017-11-07T01:37:42Z","closed_at":null,"author_association":"NONE","body":"Would you accept a PR for a `returnError` option for `validate` and `validateSync`? The default would be `false`. This would allow people to write their own assertion and validation methods that augment `e.message` or any part of `e` without having to wrap `validateSync` in a `try, catch` block.  \r\n\r\n```javascript\r\nconst schema = yup.object().shape({ name: yup.string().required() };)\r\nconst validate = (value) => {\r\n  const result = schema.validateSync(value, { returnError: true });\r\n  if (result instanceof Error) {\r\n     result.message = `[mine] ${result.message} for the user data type.`;\r\n  }\r\n\r\n  return result;\r\n};\r\n```"},"comment":{"url":"https://api.github.com/repos/jquense/yup/issues/comments/342347176","html_url":"https://github.com/jquense/yup/issues/144#issuecomment-342347176","issue_url":"https://api.github.com/repos/jquense/yup/issues/144","id":342347176,"user":{"login":"jstrimpel","id":382516,"avatar_url":"https://avatars3.githubusercontent.com/u/382516?v=4","gravatar_id":"","url":"https://api.github.com/users/jstrimpel","html_url":"https://github.com/jstrimpel","followers_url":"https://api.github.com/users/jstrimpel/followers","following_url":"https://api.github.com/users/jstrimpel/following{/other_user}","gists_url":"https://api.github.com/users/jstrimpel/gists{/gist_id}","starred_url":"https://api.github.com/users/jstrimpel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jstrimpel/subscriptions","organizations_url":"https://api.github.com/users/jstrimpel/orgs","repos_url":"https://api.github.com/users/jstrimpel/repos","events_url":"https://api.github.com/users/jstrimpel/events{/privacy}","received_events_url":"https://api.github.com/users/jstrimpel/received_events","type":"User","site_admin":false},"created_at":"2017-11-07T01:37:42Z","updated_at":"2017-11-07T01:37:42Z","author_association":"NONE","body":"As you probably already know try/catch blocks cannot be optimized by JS engines. I would need to run performance tests to really know the impact, but it could in the upwards of 1,000s of checks in response to each user action. Since there already are promises and catches adding another doesn't seem like a good approach for my use case. Additionally, I don't think the try/catch would make it past a code review on my end. :) \r\n\r\nMy use case might be unique and not warrant adding the option. Below is how I am accomplishing it now without a try/catch (this could probably be improved). As long as `schema._validate` doesn't go away i should be good.\r\n\r\n```\r\nexport default (value, name, schema) => {\r\n\tconst options = { strict: true };\r\n\tlet _schema = schema.resolve(options);\r\n\tlet result;\r\n\tlet err;\r\n\r\n\t_schema\r\n\t\t._validate(value, { ...options, sync: true })\r\n\t\t.then(r => result = r)\r\n\t\t.catch(e => err = e);\r\n\r\n\t\tif (err) {\r\n\t\t\terr.message = `[framework] ${err.message} for ${name} data type`;\r\n\t\t}\r\n\r\n\treturn err || result;\r\n};\r\n```"}}
{"actor":{"display_login":"jstrimpel"}}