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

BindQuery does not work for binding:required fields #1513

Closed
mdnight opened this issue Aug 24, 2018 · 3 comments
Closed

BindQuery does not work for binding:required fields #1513

mdnight opened this issue Aug 24, 2018 · 3 comments

Comments

@mdnight
Copy link

mdnight commented Aug 24, 2018

  • gin version: 1.3.0
  • operating system: arch linux

Description

For some reason it's impossible to use binding:"required" for query params

type LimitOffset struct {
	Offset int `form:"offset" binding:"required"`
	Limit  int `form:"limit" binding:"required"`
}

func CategoryBooks(conn *mgo.Collection, c *gin.Context) {
	var query_params LimitOffset
	if err := c.BindQuery(&query_params); err != nil {
		badRequestResponse(c, err)
		return
	}
        ...
}

response:
{"description":"Key: 'LimitOffset.Offset' Error:Field validation for 'Offset' failed on the 'required' tag","status":"error"}

url with query params:

http://localhost:8000/categories/Python?offset=0&limit=2

@syssam
Copy link
Contributor

syssam commented Aug 25, 2018

I try it.I'm fine.

@chainhelen
Copy link
Contributor

@mdnight Can you provide mini codes to help reproduce this issue?

@thinkerou
Copy link
Member

@mdnight I use the follow code it's ok.

package main

import (
	"fmt"

	"github.com/gin-gonic/gin"
)

type LimitOffset struct {
	Offset int `form:"offset" binding:"required"`
	Limit int `form:"limit" binding:"required"`
}

func main() {
	r := gin.Default()
	r.GET("/t", func(c *gin.Context) {
		var query_params LimitOffset
		if err := c.BindQuery(&query_params); err != nil {
			fmt.Println(err)
			return
		}
		fmt.Println("ok")
	})
	r.Run()
}

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

4 participants