binding

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2014 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BindingRequireError         string = "Required"
	BindingAlphaDashError       string = "AlphaDash"
	BindingAlphaDashDotError    string = "AlphaDashDot"
	BindingMinSizeError         string = "MinSize"
	BindingMaxSizeError         string = "MaxSize"
	BindingEmailError           string = "Email"
	BindingUrlError             string = "Url"
	BindingDeserializationError string = "DeserializationError"
	BindingIntegerTypeError     string = "IntegerTypeError"
	BindingBooleanTypeError     string = "BooleanTypeError"
	BindingFloatTypeError       string = "FloatTypeError"
)

Variables

View Source
var (
	// Maximum amount of memory to use when parsing a multipart form.
	// Set this to whatever value you prefer; default is 10 MB.
	MaxMemory = int64(1024 * 1024 * 10)
)

Functions

func Bind

func Bind(obj interface{}, ifacePtr ...interface{}) martini.Handler

Bind 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.

func BindIgnErr

func BindIgnErr(obj interface{}, ifacePtr ...interface{}) martini.Handler

BindIgnErr 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.

func ErrorHandler

func ErrorHandler(errs Errors, resp http.ResponseWriter)

ErrorHandler 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.

func Form

func Form(formStruct interface{}, ifacePtr ...interface{}) martini.Handler

Form 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&key=val2&key=val3 An interface pointer can be added as a second argument in order to map the struct to a specific interface.

func Json

func Json(jsonStruct interface{}, ifacePtr ...interface{}) martini.Handler

Json 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.

func MultipartForm

func MultipartForm(formStruct interface{}, ifacePtr ...interface{}) martini.Handler

func Validate

func Validate(obj interface{}) martini.Handler

Validate 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.

Types

type Errors

type Errors struct {
	Overall map[string]string `json:"overall"`
	Fields  map[string]string `json:"fields"`
}

Errors represents the contract of the response body when the binding step fails before getting to the application.

func (*Errors) Combine

func (this *Errors) Combine(other Errors)

func (Errors) Count

func (err Errors) Count() int

Total errors is the sum of errors with the request overall and errors on individual fields.

type Validator

type Validator interface {
	Validate(*Errors, *http.Request, martini.Context)
}

Implement 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL