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

Bind not getting called on structs inside a slice #37

Open
davidspiess opened this issue Apr 13, 2022 · 2 comments
Open

Bind not getting called on structs inside a slice #37

davidspiess opened this issue Apr 13, 2022 · 2 comments

Comments

@davidspiess
Copy link

davidspiess commented Apr 13, 2022

One of the properties inside my JSON payload is a slice. In my case the children property.

type Body struct {
	Name     string   `json:"name"`
	Children Children `json:"children"`
}

func (b Body) Bind(r *http.Request) error {
	if b.Name == "" {
		return errors.New("empty body name")
	}

	return nil
}

type Children []Child

func (c Children) Bind(r *http.Request) error {
	if c == nil {
		return errors.New("no children provided")
	}

	return nil
}

type Child struct {
	Name string `json:"name"`
}

func (c Child) Bind(r *http.Request) error {
	if c.Name == "" {
		return errors.New("empty child name")
	}

	return nil
}

I implemented the Bind interface for each of the types, but the Child elements are not validated when calling render.Bind on the Body struct.

Here a full example:
https://go.dev/play/p/fGMSdjRDrtg

Is this expected or am i doing something wrong?

@davidspiess
Copy link
Author

davidspiess commented Jun 17, 2022

Bind methods on maps seem also not to be called.

type TranslatedField map[language.Tag]string

func (t TranslatedField) Bind(r *http.Request) error {
  // Never called
}

@francesconi
Copy link

Any comment on this issue would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants