Skip to content

ilgooz/formutils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 

Repository files navigation

formutils GoDoc

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

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 a simple idiomatic RESTful API also uses formutils.

About

gorilla/schema & go-playground/validator

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages