Correct Namespace when array or map of structs

namespace was incorrect when array or map of structs, see #266
pull/267/head v9.3.2
Dean Karn 8 years ago
parent 49fccadad1
commit 077c3830f3
  1. 2
      README.md
  2. 2
      validator.go
  3. 67
      validator_test.go

@ -2,7 +2,7 @@ Package validator
================
<img align="right" src="https://raw.githubusercontent.com/go-playground/validator/v9/logo.png">
[![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)

@ -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...), '.')
}

@ -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")
}

Loading…
Cancel
Save