{"action":"created","issue":{"url":"https://api.github.com/repos/justinrainbow/json-schema/issues/308","repository_url":"https://api.github.com/repos/justinrainbow/json-schema","labels_url":"https://api.github.com/repos/justinrainbow/json-schema/issues/308/labels{/name}","comments_url":"https://api.github.com/repos/justinrainbow/json-schema/issues/308/comments","events_url":"https://api.github.com/repos/justinrainbow/json-schema/issues/308/events","html_url":"https://github.com/justinrainbow/json-schema/pull/308","id":177094898,"number":308,"title":"Add support for type coercion","user":{"login":"shmax","id":773172,"avatar_url":"https://avatars.githubusercontent.com/u/773172?v=3","gravatar_id":"","url":"https://api.github.com/users/shmax","html_url":"https://github.com/shmax","followers_url":"https://api.github.com/users/shmax/followers","following_url":"https://api.github.com/users/shmax/following{/other_user}","gists_url":"https://api.github.com/users/shmax/gists{/gist_id}","starred_url":"https://api.github.com/users/shmax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shmax/subscriptions","organizations_url":"https://api.github.com/users/shmax/orgs","repos_url":"https://api.github.com/users/shmax/repos","events_url":"https://api.github.com/users/shmax/events{/privacy}","received_events_url":"https://api.github.com/users/shmax/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/justinrainbow/json-schema/labels/Needs%20Review","name":"Needs Review","color":"bfdadc"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-09-15T05:52:09Z","updated_at":"2016-09-29T02:56:49Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/justinrainbow/json-schema/pulls/308","html_url":"https://github.com/justinrainbow/json-schema/pull/308","diff_url":"https://github.com/justinrainbow/json-schema/pull/308.diff","patch_url":"https://github.com/justinrainbow/json-schema/pull/308.patch"},"body":"For people (like myself) that use JSON Schema to validate API requests, a common problem is handling values that come across HTTP as strings:\r\n\r\n```\r\n$query = http_build_query([\r\n   'processRefund'=>true,\r\n   'refundAmount'=>17\r\n]);\r\n\r\necho $query; // processRefund=true&refundAmount=17;\r\n\r\nparse_str($query);\r\necho $processRefund; // \"true\" (a string value)\r\necho $refundAmount; // \"17\" (also a string value).\r\n```\r\n\r\nThis will of course fail a json schema definition expecting booleans and numbers. \r\n\r\nThis change adds a new ```CHECK_MODE_COERCE``` mode that goes one step further than ```CHECK_MODE_TYPE_CAST``` by forcing strings to their expected types when appropriate in the original object so it can then be used naturally by the consuming client code.\r\n\r\n```\r\n$request = (object)[\r\n   'processRefund'=>\"true\",\r\n   'refundAmount'=>\"17\"\r\n];\r\n\r\n$checkMode = \\JsonSchema\\Constraints\\Constraint::CHECK_MODE_COERCE | JsonSchema\\Constraints\\Constraint::CHECK_MODE_TYPE_CAST;\r\n$validator = new \\JsonSchema\\Validator($checkMode);\r\n$validator->check($request, (object) [\r\n\t\"type\"=>\"object\",\r\n\t\"properties\"=>[\r\n\t\t\"processRefund\"=>[\r\n\t\t\t\"type\"=>\"boolean\"\r\n\t\t],\r\n\t\t\"refundAmount\"=>[\r\n\t\t\t\"type\"=>\"number\"\r\n\t\t]\r\n\t]\r\n]); // validates!\r\n\r\nis_bool($request->processRefund); // true\r\nis_int($request->refundAmount); // true\r\n```\r\n\r\nThis is a feature you see in some other validation libraries (https://github.com/geraintluff/jsv4-php, https://github.com/epoberezkin/ajv  ), but if you guys aren't interested it's not a problem--I really have to compliment the injection model you've developed. It would be just as easy for me to add this functionality to my own sub-classes."},"comment":{"url":"https://api.github.com/repos/justinrainbow/json-schema/issues/comments/250358333","html_url":"https://github.com/justinrainbow/json-schema/pull/308#issuecomment-250358333","issue_url":"https://api.github.com/repos/justinrainbow/json-schema/issues/308","id":250358333,"user":{"login":"shmax","id":773172,"avatar_url":"https://avatars.githubusercontent.com/u/773172?v=3","gravatar_id":"","url":"https://api.github.com/users/shmax","html_url":"https://github.com/shmax","followers_url":"https://api.github.com/users/shmax/followers","following_url":"https://api.github.com/users/shmax/following{/other_user}","gists_url":"https://api.github.com/users/shmax/gists{/gist_id}","starred_url":"https://api.github.com/users/shmax/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shmax/subscriptions","organizations_url":"https://api.github.com/users/shmax/orgs","repos_url":"https://api.github.com/users/shmax/repos","events_url":"https://api.github.com/users/shmax/events{/privacy}","received_events_url":"https://api.github.com/users/shmax/received_events","type":"User","site_admin":false},"created_at":"2016-09-29T02:56:49Z","updated_at":"2016-09-29T02:56:49Z","body":"So what happens now?"}}
{"actor":{"display_login":"shmax"}}