Skip to content

Commit fcd0d86

Browse files
vesquenLorenz Van der Eecken
andauthored
allow byte slice and json raw message in IsJson (#1110)
## Fixes Or Enhances * json.RawMessage and []byte can be validated as JSON **Make sure that you've checked the boxes below before you submit PR:** - [x] Tests exist or have been written that cover this particular change. @go-playground/validator-maintainers --------- Co-authored-by: Lorenz Van der Eecken <[email protected]>
1 parent ce28d7c commit fcd0d86

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

baked_in.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2568,9 +2568,17 @@ func isDirPath(fl FieldLevel) bool {
25682568
func isJSON(fl FieldLevel) bool {
25692569
field := fl.Field()
25702570

2571-
if field.Kind() == reflect.String {
2571+
switch field.Kind() {
2572+
case reflect.String:
25722573
val := field.String()
25732574
return json.Valid([]byte(val))
2575+
case reflect.Slice:
2576+
fieldType := field.Type()
2577+
2578+
if fieldType.ConvertibleTo(byteSliceType) {
2579+
b := field.Convert(byteSliceType).Interface().([]byte)
2580+
return json.Valid(b)
2581+
}
25742582
}
25752583

25762584
panic(fmt.Sprintf("Bad field type %T", field.Interface()))

validator_instance.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ var (
5353
timeDurationType = reflect.TypeOf(time.Duration(0))
5454
timeType = reflect.TypeOf(time.Time{})
5555

56+
byteSliceType = reflect.TypeOf([]byte{})
57+
5658
defaultCField = &cField{namesEqual: true}
5759
)
5860

validator_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11958,7 +11958,7 @@ func TestGetTag(t *testing.T) {
1195811958

1195911959
func TestJSONValidation(t *testing.T) {
1196011960
tests := []struct {
11961-
param string
11961+
param interface{}
1196211962
expected bool
1196311963
}{
1196411964
{`foo`, false},
@@ -11975,6 +11975,34 @@ func TestJSONValidation(t *testing.T) {
1197511975
{`true`, true},
1197611976
{`null`, true},
1197711977
{`"null"`, true},
11978+
{json.RawMessage(`foo`), false},
11979+
{json.RawMessage(`}{`), false},
11980+
{json.RawMessage(`{]`), false},
11981+
{json.RawMessage(`{}`), true},
11982+
{json.RawMessage(`{"foo":"bar"}`), true},
11983+
{json.RawMessage(`{"foo":"bar","bar":{"baz":["qux"]}}`), true},
11984+
{json.RawMessage(`{"foo": 3 "bar": 4}`), false},
11985+
{json.RawMessage(`{"foo": 3 ,"bar": 4`), false},
11986+
{json.RawMessage(`{foo": 3, "bar": 4}`), false},
11987+
{json.RawMessage(`foo`), false},
11988+
{json.RawMessage(`1`), true},
11989+
{json.RawMessage(`true`), true},
11990+
{json.RawMessage(`null`), true},
11991+
{json.RawMessage(`"null"`), true},
11992+
{[]byte(`foo`), false},
11993+
{[]byte(`}{`), false},
11994+
{[]byte(`{]`), false},
11995+
{[]byte(`{}`), true},
11996+
{[]byte(`{"foo":"bar"}`), true},
11997+
{[]byte(`{"foo":"bar","bar":{"baz":["qux"]}}`), true},
11998+
{[]byte(`{"foo": 3 "bar": 4}`), false},
11999+
{[]byte(`{"foo": 3 ,"bar": 4`), false},
12000+
{[]byte(`{foo": 3, "bar": 4}`), false},
12001+
{[]byte(`foo`), false},
12002+
{[]byte(`1`), true},
12003+
{[]byte(`true`), true},
12004+
{[]byte(`null`), true},
12005+
{[]byte(`"null"`), true},
1197812006
}
1197912007

1198012008
validate := New()

0 commit comments

Comments
 (0)