mlms13 / bs-decode

Type-safe JSON decoding for ReasonML and OCaml
https://mlms13.github.io/bs-decode/docs/what-and-why
MIT License
103 stars 18 forks source link

optionalField produces wrong error on non-object JSON #135

Closed mlms13 closed 10 months ago

mlms13 commented 1 year ago
jsonString
|> optionalField("field", string)
|> Result.mapError(ParseError.failureToDebugString)

I would expect the above expression to produce an error to the effect of: "Expected object but found \"string\"". Instead:

Failed to decode object:
    Field \"field\" had an invalid value: Expected object but found \"string\"

This is wrong. The field "field" didn't have an invalid value... it wasn't there at all because the whole thing is not an object.

mlms13 commented 1 year ago

I found this while consolidating tests in the v2 branch, but I'll include the fix in 1.2.