{"modulePath":"github.com/kernel164/gogs","version":"v0.5.5","isLatest":false,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package binding\n\n## Constants\n\n```go\nconst (\n\tBindingRequireError         string = \"Required\"\n\tBindingAlphaDashError       string = \"AlphaDash\"\n\tBindingAlphaDashDotError    string = \"AlphaDashDot\"\n\tBindingMinSizeError         string = \"MinSize\"\n\tBindingMaxSizeError         string = \"MaxSize\"\n\tBindingEmailError           string = \"Email\"\n\tBindingUrlError             string = \"Url\"\n\tBindingDeserializationError string = \"DeserializationError\"\n\tBindingIntegerTypeError     string = \"IntegerTypeError\"\n\tBindingBooleanTypeError     string = \"BooleanTypeError\"\n\tBindingFloatTypeError       string = \"FloatTypeError\"\n)\n```\n\n## Variables\n\n```go\nvar (\n\t// Maximum amount of memory to use when parsing a multipart form.\n\t// Set this to whatever value you prefer; default is 10 MB.\n\tMaxMemory = int64(1024 * 1024 * 10)\n)\n```\n\n## Functions\n\n```go\nfunc Bind(obj interface{}, ifacePtr ...interface{}) macaron.Handler\n```\nBind accepts a copy of an empty struct and populates it with values from the request (if deserialization is successful). It wraps up the functionality of the Form and Json middleware according to the Content-Type of the request, and it guesses if no Content-Type is specified. Bind invokes the ErrorHandler middleware to bail out if errors occurred. If you want to perform your own error handling, use Form or Json middleware directly. An interface pointer can be added as a second argument in order to map the struct to a specific interface.\n\n```go\nfunc BindIgnErr(obj interface{}, ifacePtr ...interface{}) macaron.Handler\n```\nBindIgnErr will do the exactly same thing as Bind but without any error handling, which user has freedom to deal with them. This allows user take advantages of validation.\n\n```go\nfunc ErrorHandler(errs Errors, resp http.ResponseWriter)\n```\nErrorHandler simply counts the number of errors in the context and, if more than 0, writes a 400 Bad Request response and a JSON payload describing the errors with the \"Content-Type\" set to \"application/json\". Middleware remaining on the stack will not even see the request if, by this point, there are any errors. This is a \"default\" handler, of sorts, and you are welcome to use your own instead. The Bind middleware invokes this automatically for convenience.\n\n```go\nfunc Form(formStruct interface{}, ifacePtr ...interface{}) macaron.Handler\n```\nForm is middleware to deserialize form-urlencoded data from the request. It gets data from the form-urlencoded body, if present, or from the query string. It uses the http.Request.ParseForm() method to perform deserialization, then reflection is used to map each field into the struct with the proper type. Structs with primitive slice types (bool, float, int, string) can support deserialization of repeated form keys, for example: key=val1\u0026key=val2\u0026key=val3 An interface pointer can be added as a second argument in order to map the struct to a specific interface.\n\n```go\nfunc Json(jsonStruct interface{}, ifacePtr ...interface{}) macaron.Handler\n```\nJson is middleware to deserialize a JSON payload from the request into the struct that is passed in. The resulting struct is then validated, but no error handling is actually performed here. An interface pointer can be added as a second argument in order to map the struct to a specific interface.\n\n```go\nfunc MultipartForm(formStruct interface{}, ifacePtr ...interface{}) macaron.Handler\n```\n\n```go\nfunc Validate(obj interface{}) macaron.Handler\n```\nValidate is middleware to enforce required fields. If the struct passed in is a Validator, then the user-defined Validate method is executed, and its errors are mapped to the context. This middleware performs no error handling: it merely detects them and maps them.\n\n## Types\n\n```go\ntype Errors struct {\n\tOverall map[string]string `json:\"overall\"`\n\tFields  map[string]string `json:\"fields\"`\n}\n```\nErrors represents the contract of the response body when the binding step fails before getting to the application.\n\n```go\nfunc (this *Errors) Combine(other Errors)\n```\n\n```go\nfunc (err Errors) Count() int\n```\nTotal errors is the sum of errors with the request overall and errors on individual fields.\n\n```go\ntype Validator interface {\n\tValidate(*macaron.Context, *Errors, i18n.Locale)\n}\n```\nImplement the Validator interface to define your own input validation before the request even gets to your application. The Validate method will be executed during the validation phase.\n\n","path":"github.com/kernel164/gogs/modules/middleware/binding","name":"binding","synopsis":"","isRedistributable":true}
