{"action":"created","issue":{"url":"https://api.github.com/repos/glennsl/bs-json/issues/23","repository_url":"https://api.github.com/repos/glennsl/bs-json","labels_url":"https://api.github.com/repos/glennsl/bs-json/issues/23/labels{/name}","comments_url":"https://api.github.com/repos/glennsl/bs-json/issues/23/comments","events_url":"https://api.github.com/repos/glennsl/bs-json/issues/23/events","html_url":"https://github.com/glennsl/bs-json/issues/23","id":297043712,"number":23,"title":"optionalField Decoder ","user":{"login":"ncthbrt","id":8688217,"avatar_url":"https://avatars0.githubusercontent.com/u/8688217?v=4","gravatar_id":"","url":"https://api.github.com/users/ncthbrt","html_url":"https://github.com/ncthbrt","followers_url":"https://api.github.com/users/ncthbrt/followers","following_url":"https://api.github.com/users/ncthbrt/following{/other_user}","gists_url":"https://api.github.com/users/ncthbrt/gists{/gist_id}","starred_url":"https://api.github.com/users/ncthbrt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ncthbrt/subscriptions","organizations_url":"https://api.github.com/users/ncthbrt/orgs","repos_url":"https://api.github.com/users/ncthbrt/repos","events_url":"https://api.github.com/users/ncthbrt/events{/privacy}","received_events_url":"https://api.github.com/users/ncthbrt/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2018-02-14T10:27:11Z","updated_at":"2018-03-23T09:18:10Z","closed_at":null,"author_association":"NONE","body":"When parsing user input it's important to be able to give good error messages. For example in a http PATCH request, the `date_of_birth` field might only accept a UTC conformant string when the field is present. If a user passes in a number, we need to return with a `400 BAD REQUEST`.\r\n\r\nWhen using the `optional` decoder in combination with the field decoder, if the field exists but the inner decoder fails, the final result will be `None`. \r\n\r\nIn the previous example `{ dateOfBirth: optional(field(\"date_of_birth\", utcString)) }` when fed with the payload `{ \"date_of_birth\": 1234 }`  would simply be `{ dateOfBirth: None }`. The actual semantics that needed to be captured in this example was:\r\n-  `{ }` becomes `{ dateOfBirth: None }`\r\n-  `{ \"date_of_birth\": \"Wed, 14 Feb 2018 10:22:19 GMT\" }` becomes `{ dateOfBirth: Some(\"Wed, 14 Feb 2018 10:22:19 GMT\")`\r\n- `{ \"date_of_birth\": 123457 }` becomes `Json.Decoder.DecoderError(\"Expected a UTC string, got a number\")`\r\n\r\nAn `optionalField` decoder (or a decoder with a similar name) would solve this problem by returning `None` if the field is `undefined`, else would bubble up the error from inner decoder."},"comment":{"url":"https://api.github.com/repos/glennsl/bs-json/issues/comments/375590117","html_url":"https://github.com/glennsl/bs-json/issues/23#issuecomment-375590117","issue_url":"https://api.github.com/repos/glennsl/bs-json/issues/23","id":375590117,"user":{"login":"RasmusKlett","id":1564265,"avatar_url":"https://avatars3.githubusercontent.com/u/1564265?v=4","gravatar_id":"","url":"https://api.github.com/users/RasmusKlett","html_url":"https://github.com/RasmusKlett","followers_url":"https://api.github.com/users/RasmusKlett/followers","following_url":"https://api.github.com/users/RasmusKlett/following{/other_user}","gists_url":"https://api.github.com/users/RasmusKlett/gists{/gist_id}","starred_url":"https://api.github.com/users/RasmusKlett/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RasmusKlett/subscriptions","organizations_url":"https://api.github.com/users/RasmusKlett/orgs","repos_url":"https://api.github.com/users/RasmusKlett/repos","events_url":"https://api.github.com/users/RasmusKlett/events{/privacy}","received_events_url":"https://api.github.com/users/RasmusKlett/received_events","type":"User","site_admin":false},"created_at":"2018-03-23T09:18:10Z","updated_at":"2018-03-23T09:18:10Z","author_association":"NONE","body":"@ncthbrt You're right, that's probably a better strategy.\r\n\r\n@glennsl In my opinion the default `optional` decoder should match the JSON concept of a value which might be present. In some cases this is a field that might be `null`, in other cases it is a field which might be undefined. In no cases is it a field which might throw an error when decoding is attempted. \r\n\r\nI guess my suggestion is to have three separate decoders for these cases, so the decoders can be more strict in what they accept, warning the developer earlier when something is not as expected."}}
{"actor":{"display_login":"RasmusKlett"}}