@@ -9004,6 +9004,54 @@ func TestGetTag(t *testing.T) {
90049004 Equal (t , tag , "mytag" )
90059005}
90069006
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+ }
9054+
90079055func Test_hostnameport_validator (t * testing.T ) {
90089056
90099057 type Host struct {
@@ -9107,4 +9155,4 @@ func TestUppercaseValidation(t *testing.T) {
91079155 PanicMatches (t , func () {
91089156 _ = validate .Var (2 , "uppercase" )
91099157 }, "Bad field type int" )
9110- }
9158+ }
0 commit comments