README ¶
validation
validation is a form validation for a data validation and error collecting using Go.
Installation and tests
Install:
go get github.com/astaxie/beego/validation
Test:
go test github.com/astaxie/beego/validation
Example
Direct Use:
import (
"github.com/astaxie/beego/validation"
"log"
)
type User struct {
Name string
Age int
}
func main() {
u := User{"man", 40}
valid := validation.Validation{}
valid.Required(u.Name, "name")
valid.MaxSize(u.Name, 15, "nameMax")
valid.Range(u.Age, 0, 140, "age")
if valid.HasErrors {
// validation does not pass
// print invalid message
for _, err := range valid.Errors {
log.Println(err.Key, err.Message)
}
}
// or use like this
if v := valid.Max(u.Age, 140); !v.Ok {
log.Println(v.Error.Key, v.Error.Message)
}
}
Struct Tag Use:
import (
"github.com/astaxie/beego/validation"
)
// validation function follow with "valid" tag
// functions divide with ";"
// parameters in parentheses "()" and divide with ","
// Match function's pattern string must in "//"
type user struct {
Id int
Name string `valid:"Required;Match(/^(test)?\\w*@;com$/)"`
Age int `valid:"Required;Range(1, 140)"`
}
func main() {
valid := Validation{}
u := user{Name: "test", Age: 40}
b, err := valid.Valid(u)
if err != nil {
// handle error
}
if !b {
// validation does not pass
// blabla...
}
}
Struct Tag Functions:
Required
Min(min int)
Max(max int)
Range(min, max int)
MinSize(min int)
MaxSize(max int)
Length(length int)
Alpha
Numeric
AlphaNumeric
Match(pattern string)
AlphaDash
Email
IP
Base64
Mobile
Tel
Phone
ZipCode
LICENSE
BSD License http://creativecommons.org/licenses/BSD/
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 Alpha ¶
type Alpha struct {
Key string
}
func (Alpha) DefaultMessage ¶
func (Alpha) GetLimitValue ¶
func (a Alpha) GetLimitValue() interface{}
func (Alpha) IsSatisfied ¶
type AlphaDash ¶
func (AlphaDash) DefaultMessage ¶
func (AlphaDash) GetLimitValue ¶
func (a AlphaDash) GetLimitValue() interface{}
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 ¶
func (Base64) DefaultMessage ¶
func (Base64) GetLimitValue ¶
func (b Base64) GetLimitValue() interface{}
type Email ¶
func (Email) DefaultMessage ¶
func (Email) GetLimitValue ¶
func (e Email) GetLimitValue() interface{}
type IP ¶
func (IP) DefaultMessage ¶
func (IP) GetLimitValue ¶
func (i IP) GetLimitValue() interface{}
type Length ¶
Requires an array or string to be exactly a given length.
func (Length) DefaultMessage ¶
func (Length) GetLimitValue ¶
func (l Length) GetLimitValue() interface{}
func (Length) IsSatisfied ¶
type Match ¶
Requires a string to match a given regex.
func (Match) DefaultMessage ¶
func (Match) GetLimitValue ¶
func (m Match) GetLimitValue() interface{}
func (Match) IsSatisfied ¶
type Max ¶
func (Max) DefaultMessage ¶
func (Max) GetLimitValue ¶
func (m Max) GetLimitValue() interface{}
func (Max) IsSatisfied ¶
type MaxSize ¶
Requires an array or string to be at most a given length.
func (MaxSize) DefaultMessage ¶
func (MaxSize) GetLimitValue ¶
func (m MaxSize) GetLimitValue() interface{}
func (MaxSize) IsSatisfied ¶
type Min ¶
func (Min) DefaultMessage ¶
func (Min) GetLimitValue ¶
func (m Min) GetLimitValue() interface{}
func (Min) IsSatisfied ¶
type MinSize ¶
Requires an array or string to be at least a given length.
func (MinSize) DefaultMessage ¶
func (MinSize) GetLimitValue ¶
func (m MinSize) GetLimitValue() interface{}
func (MinSize) IsSatisfied ¶
type Mobile ¶
func (Mobile) DefaultMessage ¶
func (Mobile) GetLimitValue ¶
func (m Mobile) GetLimitValue() interface{}
type NoMatch ¶
Requires a string to not match a given regex.
func (NoMatch) DefaultMessage ¶
func (NoMatch) GetLimitValue ¶
func (n NoMatch) GetLimitValue() interface{}
func (NoMatch) IsSatisfied ¶
type Numeric ¶
type Numeric struct {
Key string
}
func (Numeric) DefaultMessage ¶
func (Numeric) GetLimitValue ¶
func (n Numeric) GetLimitValue() interface{}
func (Numeric) IsSatisfied ¶
type Phone ¶
just for chinese telephone or mobile phone number
func (Phone) DefaultMessage ¶
func (Phone) GetLimitValue ¶
func (p Phone) GetLimitValue() interface{}
func (Phone) IsSatisfied ¶
type Range ¶
Requires an integer to be within Min, Max inclusive.
func (Range) DefaultMessage ¶
func (Range) GetLimitValue ¶
func (r Range) GetLimitValue() interface{}
func (Range) IsSatisfied ¶
type Required ¶
type Required struct {
Key string
}
func (Required) DefaultMessage ¶
func (Required) GetLimitValue ¶
func (r Required) GetLimitValue() interface{}
func (Required) IsSatisfied ¶
type Tel ¶
func (Tel) DefaultMessage ¶
func (Tel) GetLimitValue ¶
func (t Tel) GetLimitValue() interface{}
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