Documentation ¶
Overview ¶
Package check provides request form checking.
Example ¶
package main import ( "fmt" "github.com/gowww/check" ) func main() { checker := check.Checker{ "email": {check.Required, check.Email}, "phone": {check.Phone}, "picture": {check.MaxFileSize(5000000), check.Image}, } errs := checker.CheckValues(map[string][]string{ "name": {"foobar"}, "phone": {"0012345678901"}, }) if errs.NotEmpty() { fmt.Println(errs) } }
Output:
Index ¶
- Variables
- func Alpha(errs Errors, form *multipart.Form, key string)
- func Alphanumeric(errs Errors, form *multipart.Form, key string)
- func Email(errs Errors, form *multipart.Form, key string)
- func Image(errs Errors, form *multipart.Form, key string)
- func Integer(errs Errors, form *multipart.Form, key string)
- func Latitude(errs Errors, form *multipart.Form, key string)
- func Longitude(errs Errors, form *multipart.Form, key string)
- func Number(errs Errors, form *multipart.Form, key string)
- func Phone(errs Errors, form *multipart.Form, key string)
- func Required(errs Errors, form *multipart.Form, key string)
- func URL(errs Errors, form *multipart.Form, key string)
- type Checker
- type Error
- type ErrorID
- type Errors
- func (e Errors) Add(key string, err *Error)
- func (e Errors) Empty() bool
- func (e Errors) First(key string) *Error
- func (e Errors) Has(key string) bool
- func (e Errors) JSON() interface{}
- func (e Errors) Merge(e2 Errors)
- func (e Errors) NotEmpty() bool
- func (e Errors) String() string
- func (e Errors) StringMap() map[string][]string
- func (e Errors) T(t *i18n.Translator) TranslatedErrors
- type Rule
- func FileType(types ...string) Rule
- func Max(max float64) Rule
- func MaxFileSize(max int64) Rule
- func MaxLen(max int) Rule
- func Min(min float64) Rule
- func MinFileSize(min int64) Rule
- func MinLen(min int) Rule
- func Range(min, max float64) Rule
- func RangeFileSize(min, max int64) Rule
- func RangeLen(min, max int) Rule
- func Same(keys ...string) Rule
- func Unique(db *sql.DB, table, column, placeholder string) Rule
- type TranslatedErrors
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrBadFileType = &ErrorID{ID: "badFileType", Locales: map[language.Tag]string{ language.English: "Only these file types are accepted: %v.", language.French: "Seul ces types de fichier sont acceptés: %v.", }} ErrIllogical = &ErrorID{ID: "illogical", Locales: map[language.Tag]string{ language.English: "This value is illogical.", language.French: "Cette valeur est illogique.", }} ErrInvalid = &ErrorID{ID: "invalid", Locales: map[language.Tag]string{ language.English: "This value is invalid.", language.French: "Cette valeur est invalide.", }} ErrMax = &ErrorID{ID: "max", Locales: map[language.Tag]string{ language.English: "The maximal value is %v.", language.French: "La valeur maximale est de %v", }} ErrMaxFileSize = &ErrorID{ID: "maxFileSize", Locales: map[language.Tag]string{ language.English: "File size is over %v.", language.French: "La taille du fichier dépasse %v.", }} ErrMaxLen = &ErrorID{ID: "maxLen", Locales: map[language.Tag]string{ language.English: "The value exceeds %v characters.", language.French: "La valeur dépasse %v caractères.", }} ErrMin = &ErrorID{ID: "min", Locales: map[language.Tag]string{ language.English: "The minimal value is %v.", language.French: "La valeur minimale est de %v", }} ErrMinFileSize = &ErrorID{ID: "minFileSize", Locales: map[language.Tag]string{ language.English: "File size must be at least %v.", language.French: "La taille du fichier doit être d'au moins %v.", }} ErrMinLen = &ErrorID{ID: "minLen", Locales: map[language.Tag]string{ language.English: "The value must have more than %v characters.", language.French: "La veleur doit comporter au moins %v caractères.", }} ErrNotAlpha = &ErrorID{ID: "notAlpha", Locales: map[language.Tag]string{ language.English: "It's not a letters-only string.", language.French: "Ce n'est pas une suite de lettres (uniquement).", }} ErrNotAlphanumeric = &ErrorID{ID: "notAlphanumeric", Locales: map[language.Tag]string{ language.English: "It's not an alphanumeric-only string.", language.French: "Ce n'est pas une suite alphanumérique (uniquement).", }} ErrNotEmail = &ErrorID{ID: "notEmail", Locales: map[language.Tag]string{ language.English: "It's not an email.", language.French: "Ce n'est pas un e-mail.", }} ErrNotFloat = &ErrorID{ID: "notFloat", Locales: map[language.Tag]string{ language.English: "It's not a floating point number.", language.French: "Ce n'est pas un nombre à virgule.", }} ErrNotImage = &ErrorID{ID: "notImage", Locales: map[language.Tag]string{ language.English: "It's not an image.", language.French: "Ce n'est pas une image.", }} ErrNotInteger = &ErrorID{ID: "notInteger", Locales: map[language.Tag]string{ language.English: "It's not a integer number.", language.French: "Ce n'est pas un nombre entier.", }} ErrNotLatitude = &ErrorID{ID: "notLatitude", Locales: map[language.Tag]string{ language.English: "It's not a latitude.", language.French: "Ce n'est pas une latitude.", }} ErrNotLongitude = &ErrorID{ID: "notLongitude", Locales: map[language.Tag]string{ language.English: "It's not a longitude.", language.French: "Ce n'est pas une longitude.", }} ErrNotNumber = &ErrorID{ID: "notNumber", Locales: map[language.Tag]string{ language.English: "It's not a number.", language.French: "Ce n'est pas un nombre.", }} ErrNotPhone = &ErrorID{ID: "notPhone", Locales: map[language.Tag]string{ language.English: "It's not a phone number.", language.French: "Ce n'est pas un numéro de téléphone.", }} ErrNotSame = &ErrorID{ID: "notSame", Locales: map[language.Tag]string{ language.English: "The value must equals these fields: %v.", language.French: "La valeur doit être identique aux champs suivants: %v.", }} ErrNotURL = &ErrorID{ID: "notURL", Locales: map[language.Tag]string{ language.English: "It's not a web address.", language.French: "Ce n'est pas une adresse web.", }} ErrNotUnique = &ErrorID{ID: "notUnique", Locales: map[language.Tag]string{ language.English: "This value already exists.", language.French: "Cette valeur existe déjà.", }} ErrRequired = &ErrorID{ID: "required", Locales: map[language.Tag]string{ language.English: "A value is required.", language.French: "Une valeur est requise.", }} ErrWrongPassword = &ErrorID{ID: "password", Locales: map[language.Tag]string{ language.English: "The password is wrong.", language.French: "Le mot de passe est incorrect.", }} )
Error identifiers. The first locale in Locales map is used when no one matched.
Functions ¶
func Alphanumeric ¶
Alphanumeric rule checks that value contains alphaumeric characters only.
Types ¶
type Checker ¶
A Checker contains keys with their checking rules.
func (Checker) Check ¶
Check makes the check for a multipart.Form (values and files) and returns errors.
Result is guaranteed to be non-nil.
func (Checker) CheckFiles ¶
func (c Checker) CheckFiles(files map[string][]*multipart.FileHeader) Errors
CheckFiles makes the check for a files map (key to multiple files) and returns errors.
Result is guaranteed to be non-nil.
func (Checker) CheckRequest ¶
CheckRequest makes the check for an HTTP request and returns errors.
Request data can have multiple values with the same key (or field). In this case, all values are checked and if one fails, the error is set for the whole key.
Result is guaranteed to be non-nil.
type Error ¶
type Error struct { Error *ErrorID Args []interface{} }
An Error is a checking error from a rule, with rule's variables.
type Errors ¶
Errors is a map of keys and their errors.
func (Errors) JSON ¶
func (e Errors) JSON() interface{}
JSON returns the errors map under the "errors" key, ready to be encoded.
func (Errors) T ¶
func (e Errors) T(t *i18n.Translator) TranslatedErrors
T returns a tranlated Errors map. If t is nil, built-in translations are used.
type Rule ¶
A Rule is a checking function to be used inside a Checker. It receives the errors map to add encountered errors, the whole form for relative checks, and the specific key to check.
func MaxFileSize ¶
MaxFileSize rule checks if file has max or less bytes.
func MinFileSize ¶
MinFileSize rule checks if file has min or more bytes.
func RangeFileSize ¶
RangeFileSize rule checks if file length is inside a range.
type TranslatedErrors ¶
TranslatedErrors is a map of keys and their translated errors.
func (TranslatedErrors) First ¶
func (e TranslatedErrors) First(key string) string
First returns the first translated error for key. If the key doesn't exist, an empty string.
func (TranslatedErrors) Has ¶
func (e TranslatedErrors) Has(key string) bool
Has tells if the translated errors map contains a key.