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

Validating numeric field with value 0 #9

Closed
Entrio opened this issue Nov 9, 2017 · 3 comments
Closed

Validating numeric field with value 0 #9

Entrio opened this issue Nov 9, 2017 · 3 comments
Assignees
Labels

Comments

@Entrio
Copy link

Entrio commented Nov 9, 2017

When validating a field with rule "required" and "numeric", when the value of 0 is provided, required validation error is trigered.

Go version: 1.9
Framework: echo
Here is an example

Json struct

type serviceRequest struct {
Owner string json:"owner"
Service int json:"service"
Operation string json:"operation"
AgentTransaction string json:"ati"
}

Here is the request handling function

func AgentServiceAction(c echo.Context) (err error) {

var structure serviceRequest
rules := govalidator.MapData{
	"owner":     []string{"required", "between:10,16"},
	"service":   []string{"required", "numeric"},
	"operation": []string{"required", "in:start,confirm,check"},
	"ati":       []string{"required", "alpha_num", "max:64", "min:1"},
}
opts := govalidator.Options{
	Request: c.Request(), // request object
	Data:    &structure,  //Structure to validate
	Rules:   rules,       // rules map
}

v := govalidator.New(opts)
e := v.ValidateJSON()

fmt.Println(v)
fmt.Println(e)

verr := map[string]interface{}{"validationError": e}

return c.JSON(200, response.JsonResponse{
	Code:    300,
	Message: "All good!",
	Data:    verr,
})

}

Post data:

{
"ati":"324t5fc45w6bh6r7unr758rtinr67",
"operation":"start",
"owner":"871029399119",
"service":0
}

The received response:

{
"code": 300,
"success": false,
"message": "All good!",
"data": {
"validationError": {
"service": [
"The service field is required"
]
}
}
}

@thedevsaddam thedevsaddam self-assigned this Nov 9, 2017
@Entrio
Copy link
Author

Entrio commented Dec 4, 2017

Is there an ETA on this?

@thedevsaddam
Copy link
Owner

thedevsaddam commented Dec 4, 2017

@Entrio I am working on this in fix/numeric-field branch. Hopefully, it'll complete by next week.

@thedevsaddam
Copy link
Owner

@Entrio Here is a fix. Hope it'll work fine.

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