render icon indicating copy to clipboard operation
render copied to clipboard

`Bind` not getting called on structs inside a slice

Open davidspiess opened this issue 3 years ago • 2 comments

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 avatar Apr 13 '22 13:04 davidspiess

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
}

davidspiess avatar Jun 17 '22 11:06 davidspiess

Any comment on this issue would be appreciated.

francesconi avatar Dec 30 '22 13:12 francesconi