Skip to content

Commit

Permalink
allow byte slice and json raw message in IsJson (#1110)
Browse files Browse the repository at this point in the history
## 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 <lorenz.vandereecken@showpad.com>
  • Loading branch information
Vesquen and Lorenz Van der Eecken committed Jun 4, 2023
1 parent ce28d7c commit fcd0d86
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
10 changes: 9 additions & 1 deletion baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -2568,9 +2568,17 @@ func isDirPath(fl FieldLevel) bool {
func isJSON(fl FieldLevel) bool {
field := fl.Field()

if field.Kind() == reflect.String {
switch field.Kind() {
case reflect.String:
val := field.String()
return json.Valid([]byte(val))
case reflect.Slice:
fieldType := field.Type()

if fieldType.ConvertibleTo(byteSliceType) {
b := field.Convert(byteSliceType).Interface().([]byte)
return json.Valid(b)
}
}

panic(fmt.Sprintf("Bad field type %T", field.Interface()))
Expand Down
2 changes: 2 additions & 0 deletions validator_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ var (
timeDurationType = reflect.TypeOf(time.Duration(0))
timeType = reflect.TypeOf(time.Time{})

byteSliceType = reflect.TypeOf([]byte{})

defaultCField = &cField{namesEqual: true}
)

Expand Down
30 changes: 29 additions & 1 deletion validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11958,7 +11958,7 @@ func TestGetTag(t *testing.T) {

func TestJSONValidation(t *testing.T) {
tests := []struct {
param string
param interface{}
expected bool
}{
{`foo`, false},
Expand All @@ -11975,6 +11975,34 @@ func TestJSONValidation(t *testing.T) {
{`true`, true},
{`null`, true},
{`"null"`, true},
{json.RawMessage(`foo`), false},
{json.RawMessage(`}{`), false},
{json.RawMessage(`{]`), false},
{json.RawMessage(`{}`), true},
{json.RawMessage(`{"foo":"bar"}`), true},
{json.RawMessage(`{"foo":"bar","bar":{"baz":["qux"]}}`), true},
{json.RawMessage(`{"foo": 3 "bar": 4}`), false},
{json.RawMessage(`{"foo": 3 ,"bar": 4`), false},
{json.RawMessage(`{foo": 3, "bar": 4}`), false},
{json.RawMessage(`foo`), false},
{json.RawMessage(`1`), true},
{json.RawMessage(`true`), true},
{json.RawMessage(`null`), true},
{json.RawMessage(`"null"`), true},
{[]byte(`foo`), false},
{[]byte(`}{`), false},
{[]byte(`{]`), false},
{[]byte(`{}`), true},
{[]byte(`{"foo":"bar"}`), true},
{[]byte(`{"foo":"bar","bar":{"baz":["qux"]}}`), true},
{[]byte(`{"foo": 3 "bar": 4}`), false},
{[]byte(`{"foo": 3 ,"bar": 4`), false},
{[]byte(`{foo": 3, "bar": 4}`), false},
{[]byte(`foo`), false},
{[]byte(`1`), true},
{[]byte(`true`), true},
{[]byte(`null`), true},
{[]byte(`"null"`), true},
}

validate := New()
Expand Down

0 comments on commit fcd0d86

Please sign in to comment.