Documentation ¶
Overview ¶
Package binding transforms a raw request into a struct ready to be used your application. It can also perform validation on the data and handle errors.
Index ¶
- Constants
- Variables
- func Bind(obj interface{}, ifacePtr ...interface{}) martini.Handler
- func ErrorHandler(errs Errors, resp http.ResponseWriter)
- func Form(formStruct interface{}, ifacePtr ...interface{}) martini.Handler
- func Json(jsonStruct interface{}, ifacePtr ...interface{}) martini.Handler
- func MultipartForm(formStruct interface{}, ifacePtr ...interface{}) martini.Handler
- func Validate(obj interface{}) martini.Handler
- type Error
- type Errors
- type Validator
Constants ¶
const ( RequiredError = "RequiredError" ContentTypeError = "ContentTypeError" DeserializationError = "DeserializationError" TypeError = "TypeError" )
const (
StatusUnprocessableEntity = 422
)
Variables ¶
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 ¶
Bind wraps up the functionality of the Form and Json middleware according to the Content-Type and verb of the request. A Content-Type is required for POST and PUT requests. 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 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 response with an error code and a JSON payload describing the errors. The response will have a JSON content-type. 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 ¶
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 ¶
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 ¶
MultipartForm works much like Form, except it can parse multipart forms and handle file uploads. Like the other deserialization middleware handlers, you can pass in an interface to make the interface available for injection into other handlers later.
Types ¶
type Error ¶
type Error struct { // An error supports zero or more field names, because an // error can morph three ways: (1) it can indicate something // wrong with the request as a whole, (2) it can point to a // specific problem with a particular input field, or (3) it // can span multiple related input fields. FieldNames []string `json:"fieldNames,omitempty"` // The classification is like an error code, convenient to // use when processing or categorizing an error programmatically. // It may also be called the "kind" of error. Classification string `json:"classification,omitempty"` // Message should be human-readable and detailed enough to // pinpoint and resolve the problem, but it should be brief. For // example, a payload of 100 objects in a JSON array might have // an error in the 41st object. The message should help the // end user find and fix the error with their request. Message string `json:"message,omitempty"` }
type Errors ¶
type Errors []Error
Errors may be generated during deserialization, binding, or validation. This type is mapped to the context so you can inject it into your own handlers and use it in your application if you want all your errors to look the same.
func (*Errors) Add ¶
Add adds an error associated with the fields indicated by fieldNames, with the given classification and message.
type Validator ¶
type Validator interface { // Validate validates that the request is OK. It is recommended // that validation be limited to checking values for syntax and // semantics, enough to know that you can make sense of the request // in your application. For example, you might verify that a credit // card number matches a valid pattern, but you probably wouldn't // perform an actual credit card authorization here. Validate(Errors, *http.Request) Errors }
Implement the Validator interface to handle some rudimentary request validation logic so your application doesn't have to.