Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect namespace with anonymous top level #266

Closed
cbandy opened this issue Dec 22, 2016 · 3 comments
Closed

Incorrect namespace with anonymous top level #266

cbandy opened this issue Dec 22, 2016 · 3 comments
Assignees
Labels
Milestone

Comments

@cbandy
Copy link

cbandy commented Dec 22, 2016

Package version eg. v8, v9:

gopkg.in/go-playground/validator.v9

Issue, Question or Enhancement:

When the top-level struct is anonymous, diving into a slice of structs returns FieldError with an incorrect namespace.

I started with an anonymous struct and a json TagNameFunc. Things went well:

var input struct {
  Children []string `json:"children" validate:"required,gt=0,dive,required"`
}
input.Children = []string{"ok", ""}

validate.Struct(input)

/*
  FieldError{
    Namespace(): `children[1]`
    Field():     `children[1]`
	
    StructNamespace(): `Children[1]`
    StructField():     `Children[1]`
  }
*/

When my needs grew beyond a slice of string, I refactored to a slice of struct. The namespace returned no longer has an index nor the top-level field 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"}, {""}}

validate.Struct(input)

/*
  FieldError{
    Namespace(): `child.name`       // I expected `children[1].name`
    Field():     `name`
	
    StructNamespace(): `child.Name` // I expected `Children[1].Name`
    StructField():     `Name`
  }
*/

I found that using a named top-level type can work around this, but now I also have to trim a level from the returned namespace:

type child struct {
  Name string `json:"name" validate:"required"`
}
type parent struct {
  Children []child `json:"children" validate:"required,gt=0,dive"`
}

var input parent
input.Children = []child{{"ok"}, {""}}

validate.Struct(input)

/*
  FieldError{
    Namespace(): `parent.children[1].name`
    Field():     `name`
	
    StructNamespace(): `parent.Children[1].Name`
    StructField():     `Name`
  }
*/
@deankarn
Copy link
Contributor

Hello @cbandy

I haven't seen that one before.

I immediately suspect that it because the struct used in your array is unexposed(lowercase name) which will limit the I formation the validator package can get from the struct, but I will look into this and verify exactly what's happening.

If you wanted to try with an exposed struct in the mean time that would sure speed things along :)

Thanks

@deankarn deankarn added this to the v9 milestone Dec 22, 2016
@deankarn deankarn self-assigned this Dec 22, 2016
deankarn pushed a commit that referenced this issue Dec 23, 2016
namespace was incorrect when array or map of structs, see #266
@deankarn
Copy link
Contributor

deankarn commented Dec 23, 2016

Hey @cbandy

I've corrected the issue in Release 9.3.2

It was an issue with checking the wrong namespace variable before calling nested struct validation.

please let me know if it solves the issue for you.

@cbandy
Copy link
Author

cbandy commented Dec 29, 2016

Resolved! Thank you.

fairyhunter13 added a commit to fairyhunter13/validator that referenced this issue Jul 12, 2020
namespace was incorrect when array or map of structs, see go-playground#266
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants