We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 9a9f46e commit 4242e5eCopy full SHA for 4242e5e
2 files changed
schema/json.go
@@ -4,6 +4,7 @@ import (
4
"bytes"
5
"encoding/json"
6
"errors"
7
+ "fmt"
8
"reflect"
9
)
10
@@ -55,10 +56,8 @@ func (dst *JSON) Set(src interface{}) error {
55
56
*dst = JSON{Bytes: []byte(""), Status: Null}
57
return nil
58
}
- // validate this is a valid json string
59
- err := json.Unmarshal([]byte(value), &struct{}{})
60
- if err != nil {
61
- return err
+ if !json.Valid([]byte(value)) {
+ return fmt.Errorf("invalid json: %s", value)
62
63
*dst = JSON{Bytes: []byte(value), Status: Present}
64
case *string:
@@ -69,10 +68,8 @@ func (dst *JSON) Set(src interface{}) error {
69
68
70
71
72
- // validate this is a valid json
73
- err := json.Unmarshal([]byte(*value), &struct{}{})
74
75
+ if !json.Valid([]byte(*value)) {
+ return fmt.Errorf("invalid json: %s", *value)
76
77
*dst = JSON{Bytes: []byte(*value), Status: Present}
78
@@ -85,10 +82,8 @@ func (dst *JSON) Set(src interface{}) error {
85
82
86
83
87
84
88
89
- err := json.Unmarshal(value, &struct{}{})
90
91
+ if !json.Valid(value) {
92
93
*dst = JSON{Bytes: value, Status: Present}
94
schema/json_test.go
@@ -15,7 +15,13 @@ func TestJSONSet(t *testing.T) {
15
}{
16
{source: "", result: JSON{Bytes: []byte(""), Status: Null}},
17
{source: "{}", result: JSON{Bytes: []byte("{}"), Status: Present}},
18
+ {source: `"test"`, result: JSON{Bytes: []byte(`"test"`), Status: Present}},
19
+ {source: "1", result: JSON{Bytes: []byte("1"), Status: Present}},
20
+ {source: "[1, 2, 3]", result: JSON{Bytes: []byte("[1, 2, 3]"), Status: Present}},
21
{source: []byte("{}"), result: JSON{Bytes: []byte("{}"), Status: Present}},
22
+ {source: []byte(`"test"`), result: JSON{Bytes: []byte(`"test"`), Status: Present}},
23
+ {source: []byte("1"), result: JSON{Bytes: []byte("1"), Status: Present}},
24
+ {source: []byte("[1, 2, 3]"), result: JSON{Bytes: []byte("[1, 2, 3]"), Status: Present}},
25
{source: ([]byte)(nil), result: JSON{Status: Null}},
26
{source: (*string)(nil), result: JSON{Status: Null}},
27
0 commit comments