Skip to content

Commit

Permalink
unmarshal: no err if second json is invalid
Browse files Browse the repository at this point in the history
closes issue #161
  • Loading branch information
santhosh-tekuri committed Apr 23, 2024
1 parent 8b4b36a commit 620600d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ func unmarshal(r io.Reader) (interface{}, error) {
if err := decoder.Decode(&doc); err != nil {
return nil, err
}
if t, _ := decoder.Token(); t != nil {
return nil, fmt.Errorf("invalid character %v after top-level value", t)
if _, err := decoder.Token(); err == nil || err != io.EOF {
return nil, fmt.Errorf("invalid character after top-level value")
}
return doc, nil
}
8 changes: 8 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@ func TestInvalidSchema(t *testing.T) {
}
})

t.Run("invalid-second json", func(t *testing.T) {
if err := jsonschema.NewCompiler().AddResource("test.json", strings.NewReader("{}A")); err == nil {
t.Error("error expected")
} else {
t.Logf("%v", err)
}
})

t.Run("multiple json", func(t *testing.T) {
if err := jsonschema.NewCompiler().AddResource("test.json", strings.NewReader("{}{}")); err == nil {
t.Error("error expected")
Expand Down

0 comments on commit 620600d

Please sign in to comment.