Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ilgooz/formutils
gorilla/schema & go-playground/validator
https://github.com/ilgooz/formutils
Last synced: about 1 month ago
JSON representation
gorilla/schema & go-playground/validator
- Host: GitHub
- URL: https://github.com/ilgooz/formutils
- Owner: ilgooz
- Created: 2015-09-30T02:30:57.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-10-15T07:03:24.000Z (about 9 years ago)
- Last Synced: 2024-04-17T20:13:43.512Z (7 months ago)
- Language: Go
- Homepage: http://godoc.org/github.com/ilgooz/formutils
- Size: 152 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
### formutils [![GoDoc](https://godoc.org/github.com/golang/gddo?status.svg)](http://godoc.org/github.com/ilgooz/formutils)
> Parse & validate your *http.Request.Form by using gorilla/schema & go-playground/validator then response your invalid fields with a pretty formated JSON error message optionally## Example
```go
func CreateUserHandler(w http.ResponseWriter, r *http.Request) {
fields := createUserForm{}// parse & validate your form and response errors with a pretty json error message
// if not all fields are valid
// e.g.
//
// HTTP 400
// {
// "message": "Invalid Data",
// "fields": {
// "email": "must be a valid email address",
// "password": "must be min 3 chars length"
// }
// }
if formutils.ParseSend(w, r, &fields) {
// oh! some fields are not valid, exit your handler
return
}// OR use formutils.Parse(r, &fields) instead if you don't want to response
// with an error message automatically.
// Handle your invalids manually
// invalids, err := formutils.Parse(r, &fields)// everything is OK, fields should be filled with their values
fmt.Println(fields)
}type createUserForm struct {
Name string `schema:"name"`
Email string `schema:"email" validate:"email,required"`
Password string `schema:"password" validate:"min=3,required"`
}
```[Stack](http://github.com/ilgooz/stack) *a simple idiomatic RESTful API* also uses formutils.