{"action":"created","issue":{"url":"https://api.github.com/repos/Vincit/objection.js/issues/81","repository_url":"https://api.github.com/repos/Vincit/objection.js","labels_url":"https://api.github.com/repos/Vincit/objection.js/issues/81/labels{/name}","comments_url":"https://api.github.com/repos/Vincit/objection.js/issues/81/comments","events_url":"https://api.github.com/repos/Vincit/objection.js/issues/81/events","html_url":"https://github.com/Vincit/objection.js/issues/81","id":133840291,"number":81,"title":"Provide access to model fields from $beforeValidate ","user":{"login":"drew-r","id":3891910,"avatar_url":"https://avatars.githubusercontent.com/u/3891910?v=3","gravatar_id":"","url":"https://api.github.com/users/drew-r","html_url":"https://github.com/drew-r","followers_url":"https://api.github.com/users/drew-r/followers","following_url":"https://api.github.com/users/drew-r/following{/other_user}","gists_url":"https://api.github.com/users/drew-r/gists{/gist_id}","starred_url":"https://api.github.com/users/drew-r/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drew-r/subscriptions","organizations_url":"https://api.github.com/users/drew-r/orgs","repos_url":"https://api.github.com/users/drew-r/repos","events_url":"https://api.github.com/users/drew-r/events{/privacy}","received_events_url":"https://api.github.com/users/drew-r/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Vincit/objection.js/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2016-02-15T23:14:24Z","updated_at":"2016-02-16T15:18:22Z","closed_at":null,"body":"Would it be possible to provide access to the model instance from `$beforeValidate` and `$beforeUpdate` when updating / patching?\r\n\r\ne.g\r\n````\r\nUser.prototype.$beforeValidate = function(jsonSchema, json, opt) {\r\n\r\n    if (this.provider === 'local' && json.password && json.password.length > 6) {\r\n       throw new objection.ValidationError({ password: 'Password too short' });\r\n    }\r\n\r\n};\r\n\r\nUser.query().first().then(function(user){\r\n      user.$query().patch({ password: '123' }).then(function(){});\r\n});\r\n\r\n````\r\nIn the example above `this` ends up being  the model constructor rather than the instance.\r\n"},"comment":{"url":"https://api.github.com/repos/Vincit/objection.js/issues/comments/184725239","html_url":"https://github.com/Vincit/objection.js/issues/81#issuecomment-184725239","issue_url":"https://api.github.com/repos/Vincit/objection.js/issues/81","id":184725239,"user":{"login":"koskimas","id":846508,"avatar_url":"https://avatars.githubusercontent.com/u/846508?v=3","gravatar_id":"","url":"https://api.github.com/users/koskimas","html_url":"https://github.com/koskimas","followers_url":"https://api.github.com/users/koskimas/followers","following_url":"https://api.github.com/users/koskimas/following{/other_user}","gists_url":"https://api.github.com/users/koskimas/gists{/gist_id}","starred_url":"https://api.github.com/users/koskimas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/koskimas/subscriptions","organizations_url":"https://api.github.com/users/koskimas/orgs","repos_url":"https://api.github.com/users/koskimas/repos","events_url":"https://api.github.com/users/koskimas/events{/privacy}","received_events_url":"https://api.github.com/users/koskimas/received_events","type":"User","site_admin":false},"created_at":"2016-02-16T15:18:22Z","updated_at":"2016-02-16T15:18:22Z","body":"@elhigu `this` is useful in `$beforeInsert`, `$beforeUpdate`. `$parse*Json` and `$format*Json` methods have no use for it, and they could be made `static`. They are not `static` so that you can do hackish stuff like this:\r\n\r\n```js\r\nsomeModel.$formatDatabaseJson = (json) => delete json.password;\r\ndoSomethingThatRequiresPasswordAndThenInsertToDatabase(someModel);\r\n```"}}