Skip to content

Commit 581d269

Browse files
committed
Added isJSON validation
1 parent fb6c458 commit 581d269

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

baked_in.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"crypto/sha256"
7+
"encoding/json"
78
"fmt"
89
"net"
910
"net/url"
@@ -166,6 +167,7 @@ var (
166167
"html_encoded": isHTMLEncoded,
167168
"url_encoded": isURLEncoded,
168169
"dir": isDir,
170+
"json": isJSON,
169171
}
170172
)
171173

@@ -2007,3 +2009,15 @@ func isDir(fl FieldLevel) bool {
20072009

20082010
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
20092011
}
2012+
2013+
// isJSON is the validation function for validating if the current field's value is a valid json string.
2014+
func isJSON(fl FieldLevel) bool {
2015+
field := fl.Field()
2016+
2017+
if field.Kind() == reflect.String {
2018+
val := field.String()
2019+
return json.Valid([]byte(val))
2020+
}
2021+
2022+
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
2023+
}

doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,12 @@ does any email provider accept all possibilities.
673673
674674
Usage: email
675675
676+
JSON String
677+
678+
This validates that a string value is valid JSON
679+
680+
Usage: json
681+
676682
File path
677683
678684
This validates that a string value contains a valid file path and that

translations/en/en.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
13211321
return s
13221322
},
13231323
},
1324+
{
1325+
tag: "json",
1326+
translation: "{0} must be a valid json string",
1327+
override: false,
1328+
},
13241329
}
13251330

13261331
for _, t := range translations {

translations/en/en_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ func TestTranslations(t *testing.T) {
141141
UniqueSlice []string `validate:"unique"`
142142
UniqueArray [3]string `validate:"unique"`
143143
UniqueMap map[string]string `validate:"unique"`
144+
JSONString string `validate:"json"`
144145
}
145146

146147
var test Test
@@ -632,6 +633,10 @@ func TestTranslations(t *testing.T) {
632633
ns: "Test.UniqueMap",
633634
expected: "UniqueMap must contain unique values",
634635
},
636+
{
637+
ns: "Test.JSONString",
638+
expected: "JSONString must be a valid json string",
639+
},
635640
}
636641

637642
for _, tt := range tests {

validator_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9003,3 +9003,51 @@ func TestGetTag(t *testing.T) {
90039003
Equal(t, errs, nil)
90049004
Equal(t, tag, "mytag")
90059005
}
9006+
9007+
func TestJSONValidation(t *testing.T) {
9008+
tests := []struct {
9009+
param string
9010+
expected bool
9011+
}{
9012+
{`foo`, false},
9013+
{`}{`, false},
9014+
{`{]`, false},
9015+
{`{}`, true},
9016+
{`{"foo":"bar"}`, true},
9017+
{`{"foo":"bar","bar":{"baz":["qux"]}}`, true},
9018+
{`{"foo": 3 "bar": 4}`, false},
9019+
{`{"foo": 3 ,"bar": 4`, false},
9020+
{`{foo": 3, "bar": 4}`, false},
9021+
{`foo`, false},
9022+
{`1`, true},
9023+
{`true`, true},
9024+
{`null`, true},
9025+
{`"null"`, true},
9026+
}
9027+
9028+
validate := New()
9029+
9030+
for i, test := range tests {
9031+
9032+
errs := validate.Var(test.param, "json")
9033+
9034+
if test.expected {
9035+
if !IsEqual(errs, nil) {
9036+
t.Fatalf("Index: %d json failed Error: %s", i, errs)
9037+
}
9038+
} else {
9039+
if IsEqual(errs, nil) {
9040+
t.Fatalf("Index: %d json failed Error: %s", i, errs)
9041+
} else {
9042+
val := getError(errs, "", "")
9043+
if val.Tag() != "json" {
9044+
t.Fatalf("Index: %d json failed Error: %s", i, errs)
9045+
}
9046+
}
9047+
}
9048+
}
9049+
9050+
PanicMatches(t, func() {
9051+
_ = validate.Var(2, "json")
9052+
}, "Bad field type int")
9053+
}

0 commit comments

Comments
 (0)