You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 6, 2023. It is now read-only.
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.
When 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.
In 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:
{ } becomes { dateOfBirth: None }
{ "date_of_birth": "Wed, 14 Feb 2018 10:22:19 GMT" } becomes { dateOfBirth: Some("Wed, 14 Feb 2018 10:22:19 GMT")
{ "date_of_birth": 123457 } becomes Json.Decoder.DecoderError("Expected a UTC string, got a number")
An 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.