Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Json_decode.ml
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ let oneOf decoders json =
| [] ->
let formattedErrors =
"\n- "
^ Js.Array.join ~sep:"\n- " (Array.of_list (List.rev errors))
^ Js.Array.join ~sep:"\n- " (Array.of_list (List.rev_map error_to_string errors))
in
error
({j|All decoders given to oneOf failed. Here are all the errors: $formattedErrors\nAnd the JSON being decoded: |j}
Expand Down
13 changes: 13 additions & 0 deletions src/__tests__/Json_decode_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ let () =
test "single-character string" (fun () ->
expect @@ string (Encode.char 'a') |> toEqual "a");

test "object as string" (fun () ->
try
let (_ : string) = string (Encode.jsonDict (Js.Dict.empty ())) in
fail "should throw"
with DecodeError Json_error "Expected string, got {}" -> pass);

Test.throws string [ Bool; Float; Int; Null; Array; Object ]);

describe "date" (fun () ->
Expand Down Expand Up @@ -709,6 +715,13 @@ let () =
test "int" (fun () ->
expect @@ (either int (field "x" int)) (Encode.int 23) |> toEqual 23);

test "object as string in either" (fun () ->
try
let a = Encode.jsonDict (Js.Dict.empty ()) in
let (_ : string) = either string string a in
fail "should throw"
with DecodeError (Json_error "All decoders given to oneOf failed. Here are all the errors: \n- Expected string, got {}\n- Expected string, got {}\nAnd the JSON being decoded: {}") -> pass);

Test.throws
(either int (field "x" int))
[ Bool; Float; String; Null; Array; Object; Char ]);
Expand Down