Skip to content

Commit 077c383

Browse files
Dean KarnDean Karn
authored andcommitted
Correct Namespace when array or map of structs
namespace was incorrect when array or map of structs, see #266
1 parent 49fccad commit 077c383

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package validator
22
================
33
<img align="right" src="https://raw.githubusercontent.com/go-playground/validator/v9/logo.png">
44
[![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)
5-
![Project status](https://img.shields.io/badge/version-9.3.1-green.svg)
5+
![Project status](https://img.shields.io/badge/version-9.3.2-green.svg)
66
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/validator/branches/v9/badge.svg)](https://semaphoreci.com/joeybloggs/validator)
77
[![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=v9&service=github)](https://coveralls.io/github/go-playground/validator?branch=v9)
88
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator)

validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns
186186
// Var - doesn't make much sense to do it that way, should call 'Struct', but no harm...
187187
// VarWithField - this allows for validating against each field withing the struct against a specific value
188188
// pretty handly in certain situations
189-
if len(ns) > 0 {
189+
if len(cf.name) > 0 {
190190
ns = append(append(ns, cf.altName...), '.')
191191
structNs = append(append(structNs, cf.name...), '.')
192192
}

validator_test.go

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6635,11 +6635,7 @@ func TestStructFiltered(t *testing.T) {
66356635
}
66366636

66376637
p3 := func(ns []byte) bool {
6638-
if bytes.HasSuffix(ns, []byte("SubTest.Test")) {
6639-
return false
6640-
}
6641-
6642-
return true
6638+
return !bytes.HasSuffix(ns, []byte("SubTest.Test"))
66436639
}
66446640

66456641
// p4 := []string{
@@ -6956,3 +6952,64 @@ func TestAlphanumericUnicodeValidation(t *testing.T) {
69566952
}
69576953
}
69586954
}
6955+
6956+
func TestArrayStructNamespace(t *testing.T) {
6957+
6958+
validate := New()
6959+
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
6960+
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
6961+
6962+
if name == "-" {
6963+
return ""
6964+
}
6965+
6966+
return name
6967+
})
6968+
6969+
type child struct {
6970+
Name string `json:"name" validate:"required"`
6971+
}
6972+
var input struct {
6973+
Children []child `json:"children" validate:"required,gt=0,dive"`
6974+
}
6975+
input.Children = []child{{"ok"}, {""}}
6976+
6977+
errs := validate.Struct(input)
6978+
NotEqual(t, errs, nil)
6979+
6980+
ve := errs.(ValidationErrors)
6981+
Equal(t, len(ve), 1)
6982+
AssertError(t, errs, "children[1].name", "Children[1].Name", "name", "Name", "required")
6983+
}
6984+
6985+
func TestMapStructNamespace(t *testing.T) {
6986+
6987+
validate := New()
6988+
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
6989+
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
6990+
6991+
if name == "-" {
6992+
return ""
6993+
}
6994+
6995+
return name
6996+
})
6997+
6998+
type child struct {
6999+
Name string `json:"name" validate:"required"`
7000+
}
7001+
var input struct {
7002+
Children map[int]child `json:"children" validate:"required,gt=0,dive"`
7003+
}
7004+
input.Children = map[int]child{
7005+
0: {Name: "ok"},
7006+
1: {Name: ""},
7007+
}
7008+
7009+
errs := validate.Struct(input)
7010+
NotEqual(t, errs, nil)
7011+
7012+
ve := errs.(ValidationErrors)
7013+
Equal(t, len(ve), 1)
7014+
AssertError(t, errs, "children[1].name", "Children[1].Name", "name", "Name", "required")
7015+
}

0 commit comments

Comments
 (0)