diff --git a/README.md b/README.md index c679e7b..e7ca378 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Package validator ================ [![Join the chat at https://gitter.im/go-playground/validator](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/go-playground/validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -![Project status](https://img.shields.io/badge/version-9.3.1-green.svg) +![Project status](https://img.shields.io/badge/version-9.3.2-green.svg) [![Build Status](https://semaphoreci.com/api/v1/joeybloggs/validator/branches/v9/badge.svg)](https://semaphoreci.com/joeybloggs/validator) [![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=v9&service=github)](https://coveralls.io/github/go-playground/validator?branch=v9) [![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator) diff --git a/validator.go b/validator.go index d011efe..7b1fd54 100644 --- a/validator.go +++ b/validator.go @@ -186,7 +186,7 @@ func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns // Var - doesn't make much sense to do it that way, should call 'Struct', but no harm... // VarWithField - this allows for validating against each field withing the struct against a specific value // pretty handly in certain situations - if len(ns) > 0 { + if len(cf.name) > 0 { ns = append(append(ns, cf.altName...), '.') structNs = append(append(structNs, cf.name...), '.') } diff --git a/validator_test.go b/validator_test.go index 4347c94..8f58d1c 100644 --- a/validator_test.go +++ b/validator_test.go @@ -6635,11 +6635,7 @@ func TestStructFiltered(t *testing.T) { } p3 := func(ns []byte) bool { - if bytes.HasSuffix(ns, []byte("SubTest.Test")) { - return false - } - - return true + return !bytes.HasSuffix(ns, []byte("SubTest.Test")) } // p4 := []string{ @@ -6956,3 +6952,64 @@ func TestAlphanumericUnicodeValidation(t *testing.T) { } } } + +func TestArrayStructNamespace(t *testing.T) { + + validate := New() + validate.RegisterTagNameFunc(func(fld reflect.StructField) string { + name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0] + + if name == "-" { + return "" + } + + return name + }) + + type child struct { + Name string `json:"name" validate:"required"` + } + var input struct { + Children []child `json:"children" validate:"required,gt=0,dive"` + } + input.Children = []child{{"ok"}, {""}} + + errs := validate.Struct(input) + NotEqual(t, errs, nil) + + ve := errs.(ValidationErrors) + Equal(t, len(ve), 1) + AssertError(t, errs, "children[1].name", "Children[1].Name", "name", "Name", "required") +} + +func TestMapStructNamespace(t *testing.T) { + + validate := New() + validate.RegisterTagNameFunc(func(fld reflect.StructField) string { + name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0] + + if name == "-" { + return "" + } + + return name + }) + + type child struct { + Name string `json:"name" validate:"required"` + } + var input struct { + Children map[int]child `json:"children" validate:"required,gt=0,dive"` + } + input.Children = map[int]child{ + 0: {Name: "ok"}, + 1: {Name: ""}, + } + + errs := validate.Struct(input) + NotEqual(t, errs, nil) + + ve := errs.(ValidationErrors) + Equal(t, len(ve), 1) + AssertError(t, errs, "children[1].name", "Children[1].Name", "name", "Name", "required") +}