Documentation
      ¶
    
    
  
    
  
    Index ¶
- Constants
 - Variables
 - type Alpha
 - type AlphaDash
 - type AlphaNumeric
 - type Base64
 - type Email
 - type Funcs
 - type IP
 - type Length
 - type Match
 - type Max
 - type MaxSize
 - type Min
 - type MinSize
 - type Mobile
 - type NoMatch
 - type Numeric
 - type Phone
 - type Range
 - type Required
 - type Tel
 - type ValidFormer
 - type ValidFunc
 - type Validation
 - func (v *Validation) Alpha(obj interface{}, key string) *ValidationResult
 - func (v *Validation) AlphaDash(obj interface{}, key string) *ValidationResult
 - func (v *Validation) AlphaNumeric(obj interface{}, key string) *ValidationResult
 - func (v *Validation) Base64(obj interface{}, key string) *ValidationResult
 - func (v *Validation) Check(obj interface{}, checks ...Validator) *ValidationResult
 - func (v *Validation) Clear()
 - func (v *Validation) Email(obj interface{}, key string) *ValidationResult
 - func (v *Validation) Error(message string, args ...interface{}) *ValidationResult
 - func (v *Validation) ErrorMap() map[string]*ValidationError
 - func (v *Validation) HasErrors() bool
 - func (v *Validation) IP(obj interface{}, key string) *ValidationResult
 - func (v *Validation) Length(obj interface{}, n int, key string) *ValidationResult
 - func (v *Validation) Match(obj interface{}, regex *regexp.Regexp, key string) *ValidationResult
 - func (v *Validation) Max(obj interface{}, max int, key string) *ValidationResult
 - func (v *Validation) MaxSize(obj interface{}, max int, key string) *ValidationResult
 - func (v *Validation) Min(obj interface{}, min int, key string) *ValidationResult
 - func (v *Validation) MinSize(obj interface{}, min int, key string) *ValidationResult
 - func (v *Validation) Mobile(obj interface{}, key string) *ValidationResult
 - func (v *Validation) NoMatch(obj interface{}, regex *regexp.Regexp, key string) *ValidationResult
 - func (v *Validation) Numeric(obj interface{}, key string) *ValidationResult
 - func (v *Validation) Phone(obj interface{}, key string) *ValidationResult
 - func (v *Validation) Range(obj interface{}, min, max int, key string) *ValidationResult
 - func (v *Validation) Required(obj interface{}, key string) *ValidationResult
 - func (v *Validation) SetError(fieldName string, errMsg string) *ValidationError
 - func (v *Validation) Tel(obj interface{}, key string) *ValidationResult
 - func (v *Validation) Valid(obj interface{}) (b bool, err error)
 - func (v *Validation) ZipCode(obj interface{}, key string) *ValidationResult
 
- type ValidationError
 - type ValidationResult
 - type Validator
 - type ZipCode
 
Constants ¶
const (
	VALIDTAG = "valid"
)
    Variables ¶
var MessageTmpls = map[string]string{
	"Required":     "Can not be empty",
	"Min":          "Minimum is %d",
	"Max":          "Maximum is %d",
	"Range":        "Range is %d to %d",
	"MinSize":      "Minimum size is %d",
	"MaxSize":      "Maximum size is %d",
	"Length":       "Required length is %d",
	"Alpha":        "Must be valid alpha characters",
	"Numeric":      "Must be valid numeric characters",
	"AlphaNumeric": "Must be valid alpha or numeric characters",
	"Match":        "Must match %s",
	"NoMatch":      "Must not match %s",
	"AlphaDash":    "Must be valid alpha or numeric or dash(-_) characters",
	"Email":        "Must be a valid email address",
	"IP":           "Must be a valid ip address",
	"Base64":       "Must be valid base64 characters",
	"Mobile":       "Must be valid mobile number",
	"Tel":          "Must be valid telephone number",
	"Phone":        "Must be valid telephone or mobile phone number",
	"ZipCode":      "Must be valid zipcode",
}
    Functions ¶
This section is empty.
Types ¶
type AlphaDash ¶
type AlphaNumeric ¶
type AlphaNumeric struct {
	Key string
}
    func (AlphaNumeric) DefaultMessage ¶
func (a AlphaNumeric) DefaultMessage() string
func (AlphaNumeric) GetKey ¶
func (a AlphaNumeric) GetKey() string
func (AlphaNumeric) GetLimitValue ¶
func (a AlphaNumeric) GetLimitValue() interface{}
func (AlphaNumeric) IsSatisfied ¶
func (a AlphaNumeric) IsSatisfied(obj interface{}) bool
type Base64 ¶
type Email ¶
type Funcs ¶
Validate function map
type IP ¶
type Length ¶
Requires an array or string to be exactly a given length.
type Match ¶
Requires a string to match a given regex.
type Max ¶
type MaxSize ¶
Requires an array or string to be at most a given length.
type Min ¶
type MinSize ¶
Requires an array or string to be at least a given length.
type Mobile ¶
type NoMatch ¶
Requires a string to not match a given regex.
type Phone ¶
just for chinese telephone or mobile phone number
type Range ¶
Requires an integer to be within Min, Max inclusive.
type Tel ¶
type ValidFormer ¶
type ValidFormer interface {
	Valid(*Validation)
}
    type Validation ¶
type Validation struct {
	Errors    []*ValidationError
	ErrorsMap map[string]*ValidationError
}
    A Validation context manages data validation and error messages.
func (*Validation) Alpha ¶
func (v *Validation) Alpha(obj interface{}, key string) *ValidationResult
Test that the obj is [a-zA-Z] if type is string
func (*Validation) AlphaDash ¶
func (v *Validation) AlphaDash(obj interface{}, key string) *ValidationResult
Test that the obj is [0-9a-zA-Z_-] if type is string
func (*Validation) AlphaNumeric ¶
func (v *Validation) AlphaNumeric(obj interface{}, key string) *ValidationResult
Test that the obj is [0-9a-zA-Z] if type is string
func (*Validation) Base64 ¶
func (v *Validation) Base64(obj interface{}, key string) *ValidationResult
Test that the obj is base64 encoded if type is string
func (*Validation) Check ¶
func (v *Validation) Check(obj interface{}, checks ...Validator) *ValidationResult
Apply a group of validators to a field, in order, and return the ValidationResult from the first one that fails, or the last one that succeeds.
func (*Validation) Email ¶
func (v *Validation) Email(obj interface{}, key string) *ValidationResult
Test that the obj is email address if type is string
func (*Validation) Error ¶
func (v *Validation) Error(message string, args ...interface{}) *ValidationResult
Add an error to the validation context.
func (*Validation) ErrorMap ¶
func (v *Validation) ErrorMap() map[string]*ValidationError
Return the errors mapped by key. If there are multiple validation errors associated with a single key, the first one "wins". (Typically the first validation will be the more basic).
func (*Validation) IP ¶
func (v *Validation) IP(obj interface{}, key string) *ValidationResult
Test that the obj is IP address if type is string
func (*Validation) Length ¶
func (v *Validation) Length(obj interface{}, n int, key string) *ValidationResult
Test that the obj is same length to n if type is string or slice
func (*Validation) Match ¶
func (v *Validation) Match(obj interface{}, regex *regexp.Regexp, key string) *ValidationResult
Test that the obj matches regexp if type is string
func (*Validation) Max ¶
func (v *Validation) Max(obj interface{}, max int, key string) *ValidationResult
Test that the obj is less than max if obj's type is int
func (*Validation) MaxSize ¶
func (v *Validation) MaxSize(obj interface{}, max int, key string) *ValidationResult
Test that the obj is shorter than max size if type is string or slice
func (*Validation) Min ¶
func (v *Validation) Min(obj interface{}, min int, key string) *ValidationResult
Test that the obj is greater than min if obj's type is int
func (*Validation) MinSize ¶
func (v *Validation) MinSize(obj interface{}, min int, key string) *ValidationResult
Test that the obj is longer than min size if type is string or slice
func (*Validation) Mobile ¶
func (v *Validation) Mobile(obj interface{}, key string) *ValidationResult
Test that the obj is chinese mobile number if type is string
func (*Validation) NoMatch ¶
func (v *Validation) NoMatch(obj interface{}, regex *regexp.Regexp, key string) *ValidationResult
Test that the obj doesn't match regexp if type is string
func (*Validation) Numeric ¶
func (v *Validation) Numeric(obj interface{}, key string) *ValidationResult
Test that the obj is [0-9] if type is string
func (*Validation) Phone ¶
func (v *Validation) Phone(obj interface{}, key string) *ValidationResult
Test that the obj is chinese mobile or telephone number if type is string
func (*Validation) Range ¶
func (v *Validation) Range(obj interface{}, min, max int, key string) *ValidationResult
Test that the obj is between mni and max if obj's type is int
func (*Validation) Required ¶
func (v *Validation) Required(obj interface{}, key string) *ValidationResult
Test that the argument is non-nil and non-empty (if string or list)
func (*Validation) SetError ¶
func (v *Validation) SetError(fieldName string, errMsg string) *ValidationError
Set error message for one field in ValidationError
func (*Validation) Tel ¶
func (v *Validation) Tel(obj interface{}, key string) *ValidationResult
Test that the obj is chinese telephone number if type is string
func (*Validation) Valid ¶
func (v *Validation) Valid(obj interface{}) (b bool, err error)
Validate a struct. the obj parameter must be a struct or a struct pointer
func (*Validation) ZipCode ¶
func (v *Validation) ZipCode(obj interface{}, key string) *ValidationResult
Test that the obj is chinese zip code if type is string
type ValidationError ¶
type ValidationError struct {
	Message, Key, Name, Field, Tmpl string
	Value                           interface{}
	LimitValue                      interface{}
}
    type ValidationResult ¶
type ValidationResult struct {
	Error *ValidationError
	Ok    bool
}
    A ValidationResult is returned from every validation method. It provides an indication of success, and a pointer to the Error (if any).
func (*ValidationResult) Key ¶
func (r *ValidationResult) Key(key string) *ValidationResult
Get ValidationResult by given key string.
func (*ValidationResult) Message ¶
func (r *ValidationResult) Message(message string, args ...interface{}) *ValidationResult
Set ValidationResult message by string or format string with args
type Validator ¶
      
      Source Files
      ¶
    
- util.go
 - validation.go
 - validators.go