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

Regex Validation examples? #19

Closed
toni-moreno opened this issue Apr 12, 2017 · 3 comments
Closed

Regex Validation examples? #19

toni-moreno opened this issue Apr 12, 2017 · 3 comments

Comments

@toni-moreno
Copy link

I've looked for example on validate a a binded struct like that, (https://github.com/toni-moreno/snmpcollector/blob/master/pkg/config/sqlconfig.go#L6-L46).

This is all what I've found (https://gowalker.org/github.com/go-macaron/binding#Validate). Could you show me an example on how should i use validation ,type validation with regex , by example ?

Thank you very much

@toni-moreno
Copy link
Author

I've just found and example here.

https://go-macaron.com/docs/middlewares/binding

@toni-moreno
Copy link
Author

toni-moreno commented Apr 21, 2017

Hi @unknwon I'm trying to add validation for my binding struct, adding the "binding" tag

// SnmpDeviceCfg contains all snmp related device definitions
type SnmpDeviceCfg struct {
	ID string `xorm:"'id' unique" binding:"Required"`
	Host    string `xorm:"host" binding:"Required"`
	Port    int    `xorm:"port" binding:"Required"`
       ....
       .....
}

And this is my url definition (https://github.com/toni-moreno/snmpcollector/blob/master/pkg/webui/apicfg-snmpdevice.go#L17) and my AddDevice handler (https://github.com/toni-moreno/snmpcollector/blob/master/pkg/webui/apicfg-snmpdevice.go#L40-L50)

When I have sent this simple json.on the API url

{"ID":"test4"}

The deserialization has been done correctly although required fields like "host" "port" has been defined and there are not in the input

Where is the error? why binding.Bind is not validating this incorrect input json ?

@toni-moreno toni-moreno reopened this Apr 21, 2017
@toni-moreno
Copy link
Author

Hi @unknwon after recompile again validations began working ok!
Thank you

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

No branches or pull requests

1 participant