{"action":"created","comment":{"url":"https://api.github.com/repos/fastify/fast-json-stringify/pulls/comments/185006687","pull_request_review_id":116311502,"id":185006687,"diff_hunk":"@@ -670,12 +670,22 @@ function nested (laterCode, name, key, schema, externalSchema, fullSchema, subKe\n         type.forEach((type, index) => {\n           var tempSchema = {type: type}\n           var nestedResult = nested(laterCode, name, key, tempSchema, externalSchema, fullSchema, subKey)\n-          code += `\n-            ${index === 0 ? 'if' : 'else if'}(ajv.validate(${require('util').inspect(tempSchema, {depth: null})}, obj${accessor}))\n+          if (type === 'string') {\n+            code += `\n+              ${index === 0 ? 'if' : 'else if'}(obj${accessor} instanceof Date || ajv.validate(${require('util').inspect(tempSchema, {depth: null})}, obj${accessor}))\n+                ${nestedResult.code}\n+            `\n+          } else {\n+            code += `\n+              ${index === 0 ? 'if' : 'else if'}(ajv.validate(${require('util').inspect(tempSchema, {depth: null})}, obj${accessor}))\n               ${nestedResult.code}\n-          `","path":"index.js","position":15,"original_position":15,"commit_id":"0a5f86c4aaa9460966271a32e9c2392297e984da","original_commit_id":"0a5f86c4aaa9460966271a32e9c2392297e984da","user":{"login":"mcollina","id":52195,"avatar_url":"https://avatars0.githubusercontent.com/u/52195?v=4","gravatar_id":"","url":"https://api.github.com/users/mcollina","html_url":"https://github.com/mcollina","followers_url":"https://api.github.com/users/mcollina/followers","following_url":"https://api.github.com/users/mcollina/following{/other_user}","gists_url":"https://api.github.com/users/mcollina/gists{/gist_id}","starred_url":"https://api.github.com/users/mcollina/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mcollina/subscriptions","organizations_url":"https://api.github.com/users/mcollina/orgs","repos_url":"https://api.github.com/users/mcollina/repos","events_url":"https://api.github.com/users/mcollina/events{/privacy}","received_events_url":"https://api.github.com/users/mcollina/received_events","type":"User","site_admin":false},"body":"Why is this call to `ajv` needed? I would refrain from doing so. Callling ajv is costly.","created_at":"2018-04-30T14:42:33Z","updated_at":"2018-04-30T14:42:34Z","html_url":"https://github.com/fastify/fast-json-stringify/pull/84#discussion_r185006687","pull_request_url":"https://api.github.com/repos/fastify/fast-json-stringify/pulls/84","author_association":"OWNER","_links":{"self":{"href":"https://api.github.com/repos/fastify/fast-json-stringify/pulls/comments/185006687"},"html":{"href":"https://github.com/fastify/fast-json-stringify/pull/84#discussion_r185006687"},"pull_request":{"href":"https://api.github.com/repos/fastify/fast-json-stringify/pulls/84"}}},"pull_request":{"url":"https://api.github.com/repos/fastify/fast-json-stringify/pulls/84","id":183577324,"html_url":"https://github.com/fastify/fast-json-stringify/pull/84","diff_url":"https://github.com/fastify/fast-json-stringify/pull/84.diff","patch_url":"https://github.com/fastify/fast-json-stringify/pull/84.patch","issue_url":"https://api.github.com/repos/fastify/fast-json-stringify/issues/84","number":84,"state":"open","locked":false,"title":"string type arrays handling dates","user":{"login":"JimmyHurrah","id":6367753,"avatar_url":"https://avatars1.githubusercontent.com/u/6367753?v=4","gravatar_id":"","url":"https://api.github.com/users/JimmyHurrah","html_url":"https://github.com/JimmyHurrah","followers_url":"https://api.github.com/users/JimmyHurrah/followers","following_url":"https://api.github.com/users/JimmyHurrah/following{/other_user}","gists_url":"https://api.github.com/users/JimmyHurrah/gists{/gist_id}","starred_url":"https://api.github.com/users/JimmyHurrah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JimmyHurrah/subscriptions","organizations_url":"https://api.github.com/users/JimmyHurrah/orgs","repos_url":"https://api.github.com/users/JimmyHurrah/repos","events_url":"https://api.github.com/users/JimmyHurrah/events{/privacy}","received_events_url":"https://api.github.com/users/JimmyHurrah/received_events","type":"User","site_admin":false},"body":"I was trying to add response schemas to a production app using nullable dates and fastify. I noticed that type arrays would not work with string for dates while it worked fine when not using type arrays.\r\n```javascript\r\n{                                                                  \r\n   type: 'object',                                                 \r\n   properties: {                                                  \r\n     date: { type: 'string' }                                  \r\n   }                                                            \r\n}\r\n```\r\nworks for dates and will produce\r\n```javascript\r\nfunction $main(obj) {\r\n  var json = '{'\r\n  var addComma = false\r\n  if (obj['date'] !== undefined) {\r\n    if (addComma) {\r\n      json += ','\r\n    }\r\n    addComma = true\r\n    json += '\"date\":'\r\n    json += $asString(obj['date'])\r\n  }\r\n  json += '}'\r\n  return json\r\n}\r\n``` \r\nwhile\r\n```javascript\r\n{                                                                  \r\n   type: 'object',                                                 \r\n   properties: {                                                  \r\n     date: { type: ['string'] }                                  \r\n   }                                                            \r\n}\r\n```\r\ndoes not work and will produce\r\n```javascript\r\nfunction $main(obj) {\r\n  var json = '{'\r\n  var addComma = false\r\n  if (obj['date'] !== undefined) {\r\n    if (addComma) {\r\n      json += ','\r\n    }\r\n    addComma = true\r\n    json += '\"date\":'\r\n\r\n    if (ajv.validate({ type: 'string'}, obj['date']))\r\n      json += $asString(obj['date'])\r\n  }\r\n  json += '}'\r\n  return json\r\n}\r\n```\r\nsince Date is of type object ```Ajv.validate({ type: 'string'}, someDate)``` will fail and not get passed to ```$asString```\r\n\r\nWith the fix the produced function looks like this:\r\n```javascript\r\nfunction $main(obj) {\r\n  var json = '{'\r\n  var addComma = false\r\n  if (obj['date'] !== undefined) {\r\n    if (addComma) {\r\n      json += ','\r\n    }\r\n    addComma = true\r\n    json += '\"date\":'\r\n    if (obj['date'] instanceof Date || ajv.validate({ type: 'string' }, obj['date']))\r\n      json += $asString(obj['date'])\r\n    else json += null\r\n  }\r\n  json += '}'\r\n  return json\r\n}\r\n```\r\nI also added an else case so it will always produce valid json.\r\n\r\nBenchmark before\r\n```\r\nJSON.stringify array x 2,712 ops/sec ±1.00% (84 runs sampled)\r\nfast-json-stringify array x 3,440 ops/sec ±1.03% (87 runs sampled)\r\nfast-json-stringify-uglified array x 3,466 ops/sec ±0.96% (88 runs sampled)\r\nJSON.stringify long string x 11,898 ops/sec ±1.01% (85 runs sampled)\r\nfast-json-stringify long string x 11,693 ops/sec ±1.55% (87 runs sampled)\r\nfast-json-stringify-uglified long string x 11,703 ops/sec ±1.19% (87 runs sampled)\r\nJSON.stringify short string x 3,984,047 ops/sec ±1.08% (87 runs sampled)\r\nfast-json-stringify short string x 7,459,043 ops/sec ±0.84% (85 runs sampled)\r\nfast-json-stringify-uglified short string x 7,442,658 ops/sec ±1.02% (87 runs sampled)\r\nJSON.stringify obj x 1,204,803 ops/sec ±1.04% (86 runs sampled)\r\nfast-json-stringify obj x 3,049,330 ops/sec ±1.08% (90 runs sampled)\r\nfast-json-stringify-uglified obj x 3,101,949 ops/sec ±0.93% (89 runs sampled)\r\n```\r\n\r\nBenchmark after\r\n```\r\nJSON.stringify array x 2,713 ops/sec ±1.01% (86 runs sampled)\r\nfast-json-stringify array x 3,454 ops/sec ±1.41% (89 runs sampled)\r\nfast-json-stringify-uglified array x 3,482 ops/sec ±0.85% (87 runs sampled)\r\nJSON.stringify long string x 11,848 ops/sec ±1.02% (87 runs sampled)\r\nfast-json-stringify long string x 11,780 ops/sec ±0.79% (87 runs sampled)\r\nfast-json-stringify-uglified long string x 11,724 ops/sec ±1.08% (85 runs sampled)\r\nJSON.stringify short string x 3,954,375 ops/sec ±0.83% (88 runs sampled)\r\nfast-json-stringify short string x 7,493,674 ops/sec ±0.81% (85 runs sampled)\r\nfast-json-stringify-uglified short string x 7,417,784 ops/sec ±0.86% (87 runs sampled)\r\nJSON.stringify obj x 1,204,706 ops/sec ±0.94% (86 runs sampled)\r\nfast-json-stringify obj x 3,069,088 ops/sec ±1.05% (85 runs sampled)\r\nfast-json-stringify-uglified obj x 3,095,127 ops/sec ±1.00% (85 runs sampled)\r\n```","created_at":"2018-04-23T22:45:50Z","updated_at":"2018-04-30T14:42:34Z","closed_at":null,"merged_at":null,"merge_commit_sha":"74fad4e816050f310e9bb0d72bfbcfe31dbd9a18","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/fastify/fast-json-stringify/pulls/84/commits","review_comments_url":"https://api.github.com/repos/fastify/fast-json-stringify/pulls/84/comments","review_comment_url":"https://api.github.com/repos/fastify/fast-json-stringify/pulls/comments{/number}","comments_url":"https://api.github.com/repos/fastify/fast-json-stringify/issues/84/comments","statuses_url":"https://api.github.com/repos/fastify/fast-json-stringify/statuses/0a5f86c4aaa9460966271a32e9c2392297e984da","head":{"label":"JimmyHurrah:master","ref":"master","sha":"0a5f86c4aaa9460966271a32e9c2392297e984da","user":{"login":"JimmyHurrah","id":6367753,"avatar_url":"https://avatars1.githubusercontent.com/u/6367753?v=4","gravatar_id":"","url":"https://api.github.com/users/JimmyHurrah","html_url":"https://github.com/JimmyHurrah","followers_url":"https://api.github.com/users/JimmyHurrah/followers","following_url":"https://api.github.com/users/JimmyHurrah/following{/other_user}","gists_url":"https://api.github.com/users/JimmyHurrah/gists{/gist_id}","starred_url":"https://api.github.com/users/JimmyHurrah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JimmyHurrah/subscriptions","organizations_url":"https://api.github.com/users/JimmyHurrah/orgs","repos_url":"https://api.github.com/users/JimmyHurrah/repos","events_url":"https://api.github.com/users/JimmyHurrah/events{/privacy}","received_events_url":"https://api.github.com/users/JimmyHurrah/received_events","type":"User","site_admin":false},"repo":{"id":130726830,"name":"fast-json-stringify","full_name":"JimmyHurrah/fast-json-stringify","owner":{"login":"JimmyHurrah","id":6367753,"avatar_url":"https://avatars1.githubusercontent.com/u/6367753?v=4","gravatar_id":"","url":"https://api.github.com/users/JimmyHurrah","html_url":"https://github.com/JimmyHurrah","followers_url":"https://api.github.com/users/JimmyHurrah/followers","following_url":"https://api.github.com/users/JimmyHurrah/following{/other_user}","gists_url":"https://api.github.com/users/JimmyHurrah/gists{/gist_id}","starred_url":"https://api.github.com/users/JimmyHurrah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JimmyHurrah/subscriptions","organizations_url":"https://api.github.com/users/JimmyHurrah/orgs","repos_url":"https://api.github.com/users/JimmyHurrah/repos","events_url":"https://api.github.com/users/JimmyHurrah/events{/privacy}","received_events_url":"https://api.github.com/users/JimmyHurrah/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/JimmyHurrah/fast-json-stringify","description":"2x faster than JSON.stringify()","fork":true,"url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify","forks_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/forks","keys_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/keys{/key_id}","collaborators_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/teams","hooks_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/hooks","issue_events_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/issues/events{/number}","events_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/events","assignees_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/assignees{/user}","branches_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/branches{/branch}","tags_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/tags","blobs_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/git/refs{/sha}","trees_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/git/trees{/sha}","statuses_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/statuses/{sha}","languages_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/languages","stargazers_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/stargazers","contributors_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/contributors","subscribers_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/subscribers","subscription_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/subscription","commits_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/commits{/sha}","git_commits_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/git/commits{/sha}","comments_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/comments{/number}","issue_comment_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/issues/comments{/number}","contents_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/contents/{+path}","compare_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/compare/{base}...{head}","merges_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/merges","archive_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/downloads","issues_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/issues{/number}","pulls_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/pulls{/number}","milestones_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/milestones{/number}","notifications_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/labels{/name}","releases_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/releases{/id}","deployments_url":"https://api.github.com/repos/JimmyHurrah/fast-json-stringify/deployments","created_at":"2018-04-23T16:37:18Z","updated_at":"2018-04-23T22:13:49Z","pushed_at":"2018-04-23T22:13:48Z","git_url":"git://github.com/JimmyHurrah/fast-json-stringify.git","ssh_url":"git@github.com:JimmyHurrah/fast-json-stringify.git","clone_url":"https://github.com/JimmyHurrah/fast-json-stringify.git","svn_url":"https://github.com/JimmyHurrah/fast-json-stringify","homepage":"","size":131,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit"},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"fastify:master","ref":"master","sha":"708ed581ffb82c38dfcf53c03d8905b7626c42a8","user":{"login":"fastify","id":24939410,"avatar_url":"https://avatars3.githubusercontent.com/u/24939410?v=4","gravatar_id":"","url":"https://api.github.com/users/fastify","html_url":"https://github.com/fastify","followers_url":"https://api.github.com/users/fastify/followers","following_url":"https://api.github.com/users/fastify/following{/other_user}","gists_url":"https://api.github.com/users/fastify/gists{/gist_id}","starred_url":"https://api.github.com/users/fastify/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fastify/subscriptions","organizations_url":"https://api.github.com/users/fastify/orgs","repos_url":"https://api.github.com/users/fastify/repos","events_url":"https://api.github.com/users/fastify/events{/privacy}","received_events_url":"https://api.github.com/users/fastify/received_events","type":"Organization","site_admin":false},"repo":{"id":64334938,"name":"fast-json-stringify","full_name":"fastify/fast-json-stringify","owner":{"login":"fastify","id":24939410,"avatar_url":"https://avatars3.githubusercontent.com/u/24939410?v=4","gravatar_id":"","url":"https://api.github.com/users/fastify","html_url":"https://github.com/fastify","followers_url":"https://api.github.com/users/fastify/followers","following_url":"https://api.github.com/users/fastify/following{/other_user}","gists_url":"https://api.github.com/users/fastify/gists{/gist_id}","starred_url":"https://api.github.com/users/fastify/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fastify/subscriptions","organizations_url":"https://api.github.com/users/fastify/orgs","repos_url":"https://api.github.com/users/fastify/repos","events_url":"https://api.github.com/users/fastify/events{/privacy}","received_events_url":"https://api.github.com/users/fastify/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/fastify/fast-json-stringify","description":"2x faster than JSON.stringify()","fork":false,"url":"https://api.github.com/repos/fastify/fast-json-stringify","forks_url":"https://api.github.com/repos/fastify/fast-json-stringify/forks","keys_url":"https://api.github.com/repos/fastify/fast-json-stringify/keys{/key_id}","collaborators_url":"https://api.github.com/repos/fastify/fast-json-stringify/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/fastify/fast-json-stringify/teams","hooks_url":"https://api.github.com/repos/fastify/fast-json-stringify/hooks","issue_events_url":"https://api.github.com/repos/fastify/fast-json-stringify/issues/events{/number}","events_url":"https://api.github.com/repos/fastify/fast-json-stringify/events","assignees_url":"https://api.github.com/repos/fastify/fast-json-stringify/assignees{/user}","branches_url":"https://api.github.com/repos/fastify/fast-json-stringify/branches{/branch}","tags_url":"https://api.github.com/repos/fastify/fast-json-stringify/tags","blobs_url":"https://api.github.com/repos/fastify/fast-json-stringify/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/fastify/fast-json-stringify/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/fastify/fast-json-stringify/git/refs{/sha}","trees_url":"https://api.github.com/repos/fastify/fast-json-stringify/git/trees{/sha}","statuses_url":"https://api.github.com/repos/fastify/fast-json-stringify/statuses/{sha}","languages_url":"https://api.github.com/repos/fastify/fast-json-stringify/languages","stargazers_url":"https://api.github.com/repos/fastify/fast-json-stringify/stargazers","contributors_url":"https://api.github.com/repos/fastify/fast-json-stringify/contributors","subscribers_url":"https://api.github.com/repos/fastify/fast-json-stringify/subscribers","subscription_url":"https://api.github.com/repos/fastify/fast-json-stringify/subscription","commits_url":"https://api.github.com/repos/fastify/fast-json-stringify/commits{/sha}","git_commits_url":"https://api.github.com/repos/fastify/fast-json-stringify/git/commits{/sha}","comments_url":"https://api.github.com/repos/fastify/fast-json-stringify/comments{/number}","issue_comment_url":"https://api.github.com/repos/fastify/fast-json-stringify/issues/comments{/number}","contents_url":"https://api.github.com/repos/fastify/fast-json-stringify/contents/{+path}","compare_url":"https://api.github.com/repos/fastify/fast-json-stringify/compare/{base}...{head}","merges_url":"https://api.github.com/repos/fastify/fast-json-stringify/merges","archive_url":"https://api.github.com/repos/fastify/fast-json-stringify/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/fastify/fast-json-stringify/downloads","issues_url":"https://api.github.com/repos/fastify/fast-json-stringify/issues{/number}","pulls_url":"https://api.github.com/repos/fastify/fast-json-stringify/pulls{/number}","milestones_url":"https://api.github.com/repos/fastify/fast-json-stringify/milestones{/number}","notifications_url":"https://api.github.com/repos/fastify/fast-json-stringify/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/fastify/fast-json-stringify/labels{/name}","releases_url":"https://api.github.com/repos/fastify/fast-json-stringify/releases{/id}","deployments_url":"https://api.github.com/repos/fastify/fast-json-stringify/deployments","created_at":"2016-07-27T19:09:25Z","updated_at":"2018-04-28T16:55:14Z","pushed_at":"2018-04-28T09:02:19Z","git_url":"git://github.com/fastify/fast-json-stringify.git","ssh_url":"git@github.com:fastify/fast-json-stringify.git","clone_url":"https://github.com/fastify/fast-json-stringify.git","svn_url":"https://github.com/fastify/fast-json-stringify","homepage":"","size":132,"stargazers_count":569,"watchers_count":569,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":40,"mirror_url":null,"archived":false,"open_issues_count":2,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit"},"forks":40,"open_issues":2,"watchers":569,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/fastify/fast-json-stringify/pulls/84"},"html":{"href":"https://github.com/fastify/fast-json-stringify/pull/84"},"issue":{"href":"https://api.github.com/repos/fastify/fast-json-stringify/issues/84"},"comments":{"href":"https://api.github.com/repos/fastify/fast-json-stringify/issues/84/comments"},"review_comments":{"href":"https://api.github.com/repos/fastify/fast-json-stringify/pulls/84/comments"},"review_comment":{"href":"https://api.github.com/repos/fastify/fast-json-stringify/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/fastify/fast-json-stringify/pulls/84/commits"},"statuses":{"href":"https://api.github.com/repos/fastify/fast-json-stringify/statuses/0a5f86c4aaa9460966271a32e9c2392297e984da"}},"author_association":"NONE"}}
{"actor":{"display_login":"mcollina"}}