Documentation
¶
Overview ¶
Package formdata provides helpers for multipart/form-data requests with no external dependencies.
The main focus for this library is parsing, validating and accessing form-data from HTTP requests. The core element of this libary is FormData, which wraps the multipart.Form object and adds additional validation capabilities.
Validation is written to enable chaining and therefore improve code readability.
Index ¶
- Constants
- Variables
- type FormData
- func (fd *FormData) Errors() []string
- func (fd *FormData) Exists(key string) bool
- func (fd *FormData) FileExists(key string) bool
- func (fd *FormData) Get(key string) FormDataValue
- func (fd *FormData) GetFile(key string) FormDataFile
- func (fd *FormData) HasErrors() bool
- func (fd *FormData) Validate(key string) *Validation
- func (fd *FormData) ValidateFile(key string) *Validation
- type FormDataError
- type FormDataFile
- type FormDataValue
- type Validation
- func (v *Validation) HasN(count int) *Validation
- func (v *Validation) HasNMin(count int) *Validation
- func (v *Validation) Match(regex *regexp.Regexp) *Validation
- func (v *Validation) MatchAll(regex *regexp.Regexp) *Validation
- func (v *Validation) MatchAllEmail() *Validation
- func (v *Validation) MatchEmail() *Validation
- func (v *Validation) Required() *Validation
- type ValidationError
Constants ¶
const ( // DefaultParseMaxMemory is the default maxMemory value for // ParseMultipartForm used by the Parse method. DefaultParseMaxMemory int64 = 2 << 19 // 1 MiB = 1,048,576 Bytes )
Variables ¶
var ( // ErrNotMultipartFormData is returned by the Parse method to indicate that // the parsed request has a differnt Content-Type than multipart/form-data. ErrNotMultipartFormData = &FormDataError{"request Content-Type isn't multipart/form-data"} )
Functions ¶
This section is empty.
Types ¶
type FormData ¶
FormData extends multipart.Form with additional validation capabilities.
func ParseMax ¶
ParseMax parses a request body as multipart/form-data. The whole request body is parsed and up to a total of maxMemory bytes of its file parts are stored in memory, with the remainder stored on disk in temporary files.
To limit the size of the incoming request set http.MaxBytesReader before parsing.
func (*FormData) Errors ¶
Errors returns validation errors as []string. If there are no validation errors an empty []string is returned.
func (*FormData) FileExists ¶
FileExists checks if FormData.File has given key.
func (*FormData) Get ¶
func (fd *FormData) Get(key string) FormDataValue
Get gets the value array associated with the given key. If there are no values associated with the key, Get returns an empty []string.
func (*FormData) GetFile ¶
func (fd *FormData) GetFile(key string) FormDataFile
GetFile returns the *multipart.FileHeader array associated with the given key. If there are no value associated with the key, GetFile returns an empty []*multipart.FileHeader.
func (*FormData) Validate ¶
func (fd *FormData) Validate(key string) *Validation
Validate creates a field validation from a given form-data key.
func (*FormData) ValidateFile ¶
func (fd *FormData) ValidateFile(key string) *Validation
ValidateFile creates a file validation from a given form-data key.
type FormDataError ¶
type FormDataError struct {
ErrorString string
}
FormDataError represents an error working with multipart/form-data.
func (*FormDataError) Error ¶
func (fe *FormDataError) Error() string
type FormDataFile ¶
type FormDataFile []*multipart.FileHeader
FormDataFile is the value of an element in FormData.File.
func (FormDataFile) At ¶ added in v0.1.0
func (f FormDataFile) At(index int) *multipart.FileHeader
At returns a single *multipart.FileHeader of FormDataFile at the given index. If index is out of bound nil is returned.
func (FormDataFile) First ¶ added in v0.1.0
func (f FormDataFile) First() *multipart.FileHeader
First envokes FormDataFile.
type FormDataValue ¶
type FormDataValue []string
FormDataValue is the value of an element in multipart.Form.Value.
func (FormDataValue) At ¶
func (v FormDataValue) At(index int) string
At gets one element of FormDataValue at the given index. If index is out of bound an empty string is returned.
func (FormDataValue) First ¶
func (v FormDataValue) First() string
First envokes FormDataValue.GetAt(0).
type Validation ¶
type Validation struct {
// contains filtered or unexported fields
}
func (*Validation) HasN ¶
func (v *Validation) HasN(count int) *Validation
HasN validates if a value has the given number of elements.
func (*Validation) HasNMin ¶
func (v *Validation) HasNMin(count int) *Validation
HasNMin validates if a value has minimal N number of elements.
func (*Validation) Match ¶
func (v *Validation) Match(regex *regexp.Regexp) *Validation
Match validates if the first element of the value matches the given regular expression.
func (*Validation) MatchAll ¶
func (v *Validation) MatchAll(regex *regexp.Regexp) *Validation
MatchAll validates if all elements of the value matching the given regular expresssion.
func (*Validation) MatchAllEmail ¶
func (v *Validation) MatchAllEmail() *Validation
MatchAllEmail validates if all elements of the value matching an email.
func (*Validation) MatchEmail ¶
func (v *Validation) MatchEmail() *Validation
MatchEmail validates if the first element of the value matches an email.
func (*Validation) Required ¶
func (v *Validation) Required() *Validation
Required checks if a key exists in the form-data
type ValidationError ¶
type ValidationError struct {
// contains filtered or unexported fields
}
func (ValidationError) String ¶
func (ve ValidationError) String() string