Documentation ¶
Overview ¶
Package binding deserializes data from HTTP requests into a struct ready for your application to use (without reflection). It also facilitates data validation and error handling.
Index ¶
- Constants
- Variables
- type Binder
- type Error
- type Errors
- func Bind(req *http.Request, userStruct FieldMapper) Errors
- func Form(req *http.Request, userStruct FieldMapper) Errors
- func Json(req *http.Request, userStruct FieldMapper) Errors
- func MultipartForm(req *http.Request, userStruct FieldMapper) Errors
- func Validate(req *http.Request, userStruct FieldMapper) Errors
- type Field
- type FieldMap
- type FieldMapper
- 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) // If no TimeFormat is specified for a time.Time field, this // format will be used by default when parsing. TimeFormat = time.RFC3339 )
Functions ¶
This section is empty.
Types ¶
type Binder ¶
type Binder interface { // Bind populates the type with data in []string which comes from the // HTTP request. The first argument is the field name. Bind(string, []string, Errors) Errors }
Binder is an interface which can deserialize itself from a slice of string coming from the request. Implement this interface so the type can be populated from form data in HTTP requests.
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"` }
Error is a powerful implementation of the built-in error interface that allows for error classification, custom error messages associated with specific fields, or with no associations at all.
type Errors ¶
type Errors []Error
Errors may be generated during deserialization, binding, or validation. It implements the built-in error interface.
func Bind ¶
func Bind(req *http.Request, userStruct FieldMapper) Errors
Bind takes data out of the request and deserializes into a struct according to the Content-Type of the request. If no Content-Type is specified, there better be data in the query string, otherwise an error will be produced.
func Form ¶
func Form(req *http.Request, userStruct FieldMapper) Errors
Form deserializes form data out of the request into a struct you provide. This function invokes data validation after deserialization.
func Json ¶
func Json(req *http.Request, userStruct FieldMapper) Errors
Json deserializes a JSON request body into a struct you specify using the standard encoding/json package (which uses reflection). This function invokes data validation after deserialization.
func MultipartForm ¶
func MultipartForm(req *http.Request, userStruct FieldMapper) Errors
MultipartForm reads a multipart form request and deserializes its data and files into a struct you provide. Files should be deserialized into *multipart.FileHeader fields.
func Validate ¶
func Validate(req *http.Request, userStruct FieldMapper) Errors
Validate ensures that all conditions have been met on every field in the populated struct. Validation should occur after the request has been deserialized into the struct.
func (*Errors) Add ¶
Add adds an error associated with the fields indicated by fieldNames, with the given classification and message.
func (Errors) Handle ¶
func (e Errors) Handle(response http.ResponseWriter) bool
Handle writes the errors to response in JSON form if any errors are contained, and it will return true. Otherwise, nothing happens and false is returned. (The value receiver is due to issue 8: https://github.com/mholt/binding/issues/8)
type Field ¶
type Field struct { // Form is the form field name to bind from Form string // Required indicates whether the field is required. A required // field that deserializes into the zero value for that type // will generate an error. Required bool // TimeFormat specifies the time format for time.Time fields. TimeFormat string // Binder is a function that converts the incoming request value(s) // to the field type; in other words, this field is populated // by executing this function. Useful when the custom type doesn't // implement the Binder interface. Binder func(string, []string, Errors) Errors }
Field describes the properties of a struct field.
type FieldMap ¶
type FieldMap map[interface{}]interface{}
FieldMap is a map of pointers to struct fields -> field names from the request. The values could also be Field structs to specify metadata about the field.
type FieldMapper ¶
type FieldMapper interface { // FieldMap returns a map that keys field names from the request // to pointers into which the values will be deserialized. FieldMap() FieldMap }
Only types that are FieldMappers can have request data deserialized into them.
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(*http.Request, Errors) Errors }
Validator can be implemented by your type to handle some rudimentary request validation separately from your application logic.