Documentation ¶
Index ¶
- func BaseAddress(r *http.Request) string
- func BasePath(path string) string
- func ErrFieldExists(field string) error
- func ErrFieldRequired(field string) error
- func FlashFormWithErrors(sess *sessions.Session, f Form, errs ValidationErrors)
- func FormFields(sess *sessions.Session) map[string]string
- func HTML(w http.ResponseWriter, content string, status int)
- func JSON(w http.ResponseWriter, data interface{}, status int)
- func Text(w http.ResponseWriter, content string, status int)
- func UnmarshalForm(f Form, r *http.Request) error
- func UnmarshalFormWithFile(f Form, file *File, r *http.Request) error
- func Validate(v Validator) error
- type File
- type FileValidator
- type Form
- type FormUnmarshaler
- type UnmarshalError
- type ValidationErrors
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BaseAddress ¶
BaseAddress will return the HTTP address for the given Request. This will return the Scheme of the current Request (http, or https), concatenated with the host. If the X-Forwarded-Proto, and X-Forwarded-Host headers are present in the Request, then they will be used for the Scheme and Host respectively.
func BasePath ¶
BasePath returns the last element of the given path. This will split the path using the "/" spearator. If the path is empty BasePath returns "/".
func ErrFieldExists ¶
func ErrFieldRequired ¶
func FlashFormWithErrors ¶
func FlashFormWithErrors(sess *sessions.Session, f Form, errs ValidationErrors)
FlashFormWithErrors flashes the given Form and Errors to the given session under the "form_fields" and "form_errors" keys respectively.
func FormFields ¶
FormField returns the map of form fields that has been flashed to the given session under the "form_fields" key. If the key does not exist, then an empty map is returned instead.
func HTML ¶
func HTML(w http.ResponseWriter, content string, status int)
HTML sets the Content-Type of the given ResponseWriter to text/html, and writes the given content with the given status code to the writer. This will also set the Content-Length header to the len of content.
func JSON ¶
func JSON(w http.ResponseWriter, data interface{}, status int)
JSON sets the Content-Type of the given ResponseWriter to application/json, and encodes the given interface to JSON to the given writer, with the given status code. This will also set the Content-Length header to the len of the JSON encoded data.
func Text ¶
func Text(w http.ResponseWriter, content string, status int)
Text sets the Content-Type of the given ResponseWriter to text/plain, and writes the given content with the given status code to the writer. This will also se the Content-Length header to the len of content.
func UnmarshalForm ¶
UnmarshalForm unmarshals the given request into the given form. This will return any unmarshalling errors for individual fields in a ValidationErrors type.
func UnmarshalFormWithFile ¶
UnmarshalFormWithFile will unmarshal a file from the given request, then it it will unmarshal the rest of the request data into the given form. If the file is sent as the request body itself, then the URL query parameters will be used to unmarshal the rest of the form data from.
Types ¶
type File ¶
type File struct { multipart.File // The underlying file that was uploaded. // Header is the header of the file being uploaded. Header *multipart.FileHeader // Type is the MIME type of the file, this is set during the unmarshalling // of the file by sniffing the first 512 bytes of the file. Type string // Field is the form field to unmarshal the file from, if the file is being // uploaded as part of a "multipart/form-data" request. Field string }
File is used for unmarshalling files from requests.
type FileValidator ¶
type FileValidator struct { *File // The uploaded file. // Size is the maximum size of a file. Set to 0 for no limit. Size int64 // Mimes is a list of MIME types to allow/disallow during uploading. Mimes []string // MimesAllowed delineates whether or not the above slice of MIME types // should be allowed/disallowed. Set to false to disallow, set to true to // allow. MimesAllowed bool }
FileValidator is a Validator implementation for validating file uploads.
func (*FileValidator) HasFile ¶
func (v *FileValidator) HasFile() bool
HasFile will check to see if a file has been uploaded.
func (*FileValidator) Validate ¶
func (v *FileValidator) Validate(errs ValidationErrors)
Validate will check if a file has been validated, along with whether or not it is within the size limit, and of the allowed MIME types.
type FormUnmarshaler ¶
type FormUnmarshaler struct { Form Form // The Form to decode the values to, must be a pointer. Decoder *schema.Decoder // The decoder to use for decoding form data. }
FormUnmarshaler is used for unmarshalling forms from requests.
func (FormUnmarshaler) UnmarshalRequest ¶
func (f FormUnmarshaler) UnmarshalRequest(r *http.Request) error
UnmarshalRequest will unmarshal the given request to the underlying form. If the request has the Content-Type header set to "application/json" then the request body is decoded as JSON.
type UnmarshalError ¶
func (UnmarshalError) Error ¶
func (e UnmarshalError) Error() string
type ValidationErrors ¶
ValidationErrors records any validation errors that may have occurred. Each error is kept beneath the field for which the error occurred.
func FormErrors ¶
func FormErrors(sess *sessions.Session) ValidationErrors
FormErrors returns the Errors that has been flashed to the given session under the "form_errors" key. If the key does not exist, then an empty Errors is returned instead.
func NewValidationErrors ¶
func NewValidationErrors() ValidationErrors
NewValidationErrors returns an empty ValidationErrors.
func (ValidationErrors) Add ¶
func (e ValidationErrors) Add(key string, err error)
Add adds the given error for the given key.
func (ValidationErrors) Error ¶
func (e ValidationErrors) Error() string
Error returns the string representation of the current set of errors. It will be formatted like so,
field: err err
func (ValidationErrors) First ¶
func (e ValidationErrors) First(key string) string
First returns the first error for the given key if any.
func (ValidationErrors) Merge ¶
func (e ValidationErrors) Merge(verrs ValidationErrors)
Merge merges the given set of errors into the current one.
type Validator ¶
type Validator interface { // Validate performs validation on a set of data. Each error that occurs // should be added to the given set of errors. Validate(errs ValidationErrors) }