{"action":"created","issue":{"url":"https://api.github.com/repos/epoberezkin/ajv/issues/322","repository_url":"https://api.github.com/repos/epoberezkin/ajv","labels_url":"https://api.github.com/repos/epoberezkin/ajv/issues/322/labels{/name}","comments_url":"https://api.github.com/repos/epoberezkin/ajv/issues/322/comments","events_url":"https://api.github.com/repos/epoberezkin/ajv/issues/322/events","html_url":"https://github.com/epoberezkin/ajv/issues/322","id":182988971,"number":322,"title":"Pass options to validate function at runtime","user":{"login":"mrhooray","id":2237317,"avatar_url":"https://avatars.githubusercontent.com/u/2237317?v=3","gravatar_id":"","url":"https://api.github.com/users/mrhooray","html_url":"https://github.com/mrhooray","followers_url":"https://api.github.com/users/mrhooray/followers","following_url":"https://api.github.com/users/mrhooray/following{/other_user}","gists_url":"https://api.github.com/users/mrhooray/gists{/gist_id}","starred_url":"https://api.github.com/users/mrhooray/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mrhooray/subscriptions","organizations_url":"https://api.github.com/users/mrhooray/orgs","repos_url":"https://api.github.com/users/mrhooray/repos","events_url":"https://api.github.com/users/mrhooray/events{/privacy}","received_events_url":"https://api.github.com/users/mrhooray/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-10-14T08:12:50Z","updated_at":"2016-10-14T11:06:59Z","closed_at":null,"body":"Thanks for making this awesome library.\r\n\r\nWe have a use case that need to do both following operations given a schema\r\n- validate & filter data with compile time option `removeAdditional: true`\r\n- dry run above process without actually removing fields, just to see whether there are errors\r\n\r\nWe were on v4.0.6 and the approach we took was to compile two validate functions for each schema, one with `removeAdditional: true` and the other with `removeAdditional: 'dryRun'` and modify [related part in properties.jst](https://github.com/epoberezkin/ajv/blob/master/lib/dot/properties.jst#L94-L97) into following:\r\n```\r\n       {{? $noAdditional }}\r\n         {{? $removeAdditional === 'dryRun' }}\r\n         {{?? $removeAdditional }}\r\n           delete {{=$data}}[key{{=$lvl}}];\r\n         {{??}}\r\n```\r\n\r\nIt works pretty well except doubling the number of validators in memory. We're looking into ways to turn dry run from compile time option into runtime/validation time option to reduce memory usage:\r\n```js\r\nvar ajv = new Ajv({removeAdditional: true});\r\nvar validate = Ajv.compile(schema);\r\n// actually removing fields\r\nvalidate(data);\r\n// dry run\r\nvalidate(\r\n  data,\r\n  /*dataPath*/,\r\n  /*parentData*/,\r\n  /*parentDataProperty*/,\r\n  /*option e.g. {dryRun: true}*/\r\n); \r\n```\r\n\r\nGot lost a few times on how to pass that runtime option to sub/referenced validate functions(recursively?). Could you give some hints on how to do that properly, or is there a better way to do that at runtime?"},"comment":{"url":"https://api.github.com/repos/epoberezkin/ajv/issues/comments/253771811","html_url":"https://github.com/epoberezkin/ajv/issues/322#issuecomment-253771811","issue_url":"https://api.github.com/repos/epoberezkin/ajv/issues/322","id":253771811,"user":{"login":"mrhooray","id":2237317,"avatar_url":"https://avatars.githubusercontent.com/u/2237317?v=3","gravatar_id":"","url":"https://api.github.com/users/mrhooray","html_url":"https://github.com/mrhooray","followers_url":"https://api.github.com/users/mrhooray/followers","following_url":"https://api.github.com/users/mrhooray/following{/other_user}","gists_url":"https://api.github.com/users/mrhooray/gists{/gist_id}","starred_url":"https://api.github.com/users/mrhooray/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mrhooray/subscriptions","organizations_url":"https://api.github.com/users/mrhooray/orgs","repos_url":"https://api.github.com/users/mrhooray/repos","events_url":"https://api.github.com/users/mrhooray/events{/privacy}","received_events_url":"https://api.github.com/users/mrhooray/received_events","type":"User","site_admin":false},"created_at":"2016-10-14T11:06:59Z","updated_at":"2016-10-14T11:06:59Z","body":"Yeah in a fork. Wanted to explore passing runtime options to validate function since I'm not vary familiar with internals, especially on iterate through validate functions and attach properties recursively.\r\n\r\nFollowing seems to work but want to double check with you to see whether it's correct or there's better ways to do it.\r\n\r\nTemplate Change:\r\n```\r\n      {{? $noAdditional }}\r\n        {{? $removeAdditional }}\r\n           if (!validate.dryRun) {\r\n             delete {{=$data}}[key{{=$lvl}}];\r\n           }\r\n        {{??}}\r\n\r\n```\r\n\r\nBefore running validate function, recursively attach the flag to each validate function\r\n```js\r\nvar ajv = new Ajv({removeAdditional: true});\r\nvar validate = Ajv.compile(schema);\r\n// set dry run\r\nmarkDryRun(validate, true)\r\n// reset\r\nmarkDryRun(validate, undefined)\r\n\r\nfunction markDryRun(v, dryRun) {\r\n    if (v.dryRun === dryRun) {\r\n        return;\r\n    }\r\n\r\n    var i;\r\n\r\n    v.dryRun = dryRun;\r\n\r\n    for (i = 0; i < v.refVal.length; i++) {\r\n        if (typeof v.refVal[i] === 'function') {\r\n            markDryRun(v.refVal[i], dryRun);\r\n        }\r\n    }\r\n}\r\n```"}}
{"actor":{"display_login":"mrhooray"}}