Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,15 @@ func (mv *Validator) validateField(fieldDef reflect.StructField, fieldVal reflec

// no-op if field is not a struct, interface, array, slice or map
mv.deepValidateCollection(fieldVal, m, func() string {
return fieldDef.Name
n := fieldDef.Name

if mv.printJSON {
if jn := parseName(fieldDef.Tag.Get("json")); jn != "" {
n = jn
}
}

return n
})

if len(errs) > 0 {
Expand Down
25 changes: 18 additions & 7 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"strings"
"testing"

"github.com/crossid/validator"
. "gopkg.in/check.v1"
"gopkg.in/validator.v2"
)

func Test(t *testing.T) {
Expand Down Expand Up @@ -59,15 +59,17 @@ func (i Impl2) Foo() string {
return i.F
}

type SubStruct struct {
A int `validate:"nonzero" json:"sub_a"`
B string
C float64 `validate:"nonzero,min=1" json:"c_is_a_float"`
D *string `validate:"nonzero"`
}

type TestStruct struct {
A int `validate:"nonzero" json:"a"`
B string `validate:"len=8,min=6,max=4"`
Sub struct {
A int `validate:"nonzero" json:"sub_a"`
B string
C float64 `validate:"nonzero,min=1" json:"c_is_a_float"`
D *string `validate:"nonzero"`
}
Sub SubStruct `json:"sub"`
D *Simple `validate:"nonzero"`
E I `validate:nonzero`
}
Expand Down Expand Up @@ -669,13 +671,22 @@ func (ms *MySuite) TestErrors(c *C) {
func (ms *MySuite) TestJSONPrint(c *C) {
t := TestStruct{
A: 0,
Sub: SubStruct{
A: 0,
B: "",
C: 1.0,
D: nil,
},
}
err := validator.WithPrintJSON(true).Validate(t)
c.Assert(err, NotNil)
errs, ok := err.(validator.ErrorMap)
c.Assert(ok, Equals, true)
c.Assert(errs["A"], IsNil)
c.Assert(errs["a"], HasError, validator.ErrZeroValue)
c.Assert(errs["Sub.sub_a"], IsNil)
c.Assert(errs["sub.sub_a"], HasError, validator.ErrZeroValue)

}

func (ms *MySuite) TestJSONPrintOff(c *C) {
Expand Down