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
19 changes: 7 additions & 12 deletions schema/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"reflect"
)

Expand Down Expand Up @@ -55,10 +56,8 @@ func (dst *JSON) Set(src interface{}) error {
*dst = JSON{Bytes: []byte(""), Status: Null}
return nil
}
// validate this is a valid json string
err := json.Unmarshal([]byte(value), &struct{}{})
if err != nil {
return err
if !json.Valid([]byte(value)) {
return fmt.Errorf("invalid json: %s", value)
}
*dst = JSON{Bytes: []byte(value), Status: Present}
case *string:
Expand All @@ -69,10 +68,8 @@ func (dst *JSON) Set(src interface{}) error {
*dst = JSON{Bytes: []byte(""), Status: Null}
return nil
}
// validate this is a valid json
err := json.Unmarshal([]byte(*value), &struct{}{})
if err != nil {
return err
if !json.Valid([]byte(*value)) {
return fmt.Errorf("invalid json: %s", *value)
}
*dst = JSON{Bytes: []byte(*value), Status: Present}
}
Expand All @@ -85,10 +82,8 @@ func (dst *JSON) Set(src interface{}) error {
return nil
}

// validate this is a valid json
err := json.Unmarshal(value, &struct{}{})
if err != nil {
return err
if !json.Valid(value) {
return fmt.Errorf("invalid json: %s", value)
}
*dst = JSON{Bytes: value, Status: Present}
}
Expand Down
6 changes: 6 additions & 0 deletions schema/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ func TestJSONSet(t *testing.T) {
}{
{source: "", result: JSON{Bytes: []byte(""), Status: Null}},
{source: "{}", result: JSON{Bytes: []byte("{}"), Status: Present}},
{source: `"test"`, result: JSON{Bytes: []byte(`"test"`), Status: Present}},
{source: "1", result: JSON{Bytes: []byte("1"), Status: Present}},
{source: "[1, 2, 3]", result: JSON{Bytes: []byte("[1, 2, 3]"), Status: Present}},
{source: []byte("{}"), result: JSON{Bytes: []byte("{}"), Status: Present}},
{source: []byte(`"test"`), result: JSON{Bytes: []byte(`"test"`), Status: Present}},
{source: []byte("1"), result: JSON{Bytes: []byte("1"), Status: Present}},
{source: []byte("[1, 2, 3]"), result: JSON{Bytes: []byte("[1, 2, 3]"), Status: Present}},
{source: ([]byte)(nil), result: JSON{Status: Null}},
{source: (*string)(nil), result: JSON{Status: Null}},

Expand Down