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

go lang Field validation for 'Code' failed on the 'required' tag #1311

Open
nareshbandameedi opened this issue Mar 17, 2019 · 1 comment

Comments

Projects
None yet
1 participant
@nareshbandameedi
Copy link

commented Mar 17, 2019

Issue Description

Query parameters input validation is failing

Steps to reproduce

Request URL:'http://127.0.0.1:3015/action?code=csf&name=tbiubi-csf-rule'

Working code to debug

type ActionByRuleRequest struct {
    Code  string `json:"code" form:"code" query:"code" validate:"required,oneof=gde cse scc"`
    Name string `json:"name" form:"name" query:"name" validate:"required"` // Monitoring rule name
}
import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"strconv"
	"time"
	validator "gopkg.in/go-playground/validator.v9"
)
func GetActionByRule(c echo.Context) error {
    // Get parameters from the query string
    req := new(model.ActionByRuleRequest)
    err := c.Bind(req);
    if  err != nil {
        log.Println(err)
        error := model.Error{
            Message: err.Error(),
        }
        responseError := &model.ResponseError{
            Status: http.StatusBadRequest,
            Code:   "INVALID_INPUT",
        }
        responseError.Errors = append(responseError.Errors, error)
        return echo.NewHTTPError(http.StatusBadRequest, responseError)
    }
    // Validate input
    log.Printf("[/action_by_rule] 00000000000 get req = %#v\n", req)

    v := validator.New()

    err = v.Struct(req); 

    if err == nil {
        log.Println("Input valid")
    } else {
        log.Println("Input validation failed:", err)
     }
}

Error:

Key: 'ActionByRuleRequest.Code' Error:Field validation for 'Code' failed on the 'required' tag
Key: 'ActionByRuleRequest.Name' Error:Field validation for 'Name' failed on the 'required' tag
@stale

This comment has been minimized.

Copy link

commented May 16, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label May 16, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.