Skip to content

Commit

Permalink
fix: preserve correct error for optionalField
Browse files Browse the repository at this point in the history
chore: test optionalField error
  • Loading branch information
mlms13 committed Oct 15, 2023
1 parent efab581 commit 9771719
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Decode_Base.re
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,8 @@ module Make =
|> flatMap(
fun
| None => pure(None)
| Some(v) => (_ => optional(decode, v)),
)
>> T.objErr(name);
| Some(v) => (_ => optional(decode, v) |> T.objErr(name)),
);

let fallback = (decode, recovery) => alt(decode, pure(recovery));

Expand Down
5 changes: 5 additions & 0 deletions test/Decode_AsResult_OfParseError_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ describe("Inner decoders", () => {
)
);

test("optionalField (failure on outer structure)", () =>
expect(Decode.optionalField("field", Decode.string, Sample.jsonString))
|> toEqual(valErr(`ExpectedObject, Sample.jsonString))
);

test("optionalField (failure on inner decode)", () =>
expect(Decode.(optionalField("title", boolean, Sample.jsonJobCeo)))
|> toEqual(
Expand Down

0 comments on commit 9771719

Please sign in to comment.