beego: github.com/astaxie/beego/validation Index | Files

package validation

import "github.com/astaxie/beego/validation"

Package validation for validations

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, "ageMax"); !v.Ok {
		log.Println(v.Error.Key, v.Error.Message)
	}
}

more info: http://beego.me/docs/mvc/controller/validation.md

Index

Package Files

util.go validation.go validators.go

Constants

const (
    // ValidTag struct tag
    ValidTag = "valid"
)

Variables

var CanSkipFuncs = map[string]struct{}{
    "Email":   {},
    "IP":      {},
    "Mobile":  {},
    "Tel":     {},
    "Phone":   {},
    "ZipCode": {},
}

CanSkipFuncs will skip valid if RequiredFirst is true and the struct field's value is empty

var (

    // ErrInt64On32 show 32 bit platform not support int64
    ErrInt64On32 = fmt.Errorf("not support int64 on 32-bit platform")
)
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",
}

MessageTmpls store commond validate template

func AddCustomFunc Uses

func AddCustomFunc(name string, f CustomFunc) error

AddCustomFunc Add a custom function to validation The name can not be:

Clear
HasErrors
ErrorMap
Error
Check
Valid
NoMatch

If the name is same with exists function, it will replace the origin valid function

func SetDefaultMessage Uses

func SetDefaultMessage(msg map[string]string)

SetDefaultMessage set default messages if not set, the default messages are

"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",

type Alpha Uses

type Alpha struct {
    Key string
}

Alpha check the alpha

func (Alpha) DefaultMessage Uses

func (a Alpha) DefaultMessage() string

DefaultMessage return the default Length error message

func (Alpha) GetKey Uses

func (a Alpha) GetKey() string

GetKey return the m.Key

func (Alpha) GetLimitValue Uses

func (a Alpha) GetLimitValue() interface{}

GetLimitValue return the limit value

func (Alpha) IsSatisfied Uses

func (a Alpha) IsSatisfied(obj interface{}) bool

IsSatisfied judge whether obj is valid

type AlphaDash Uses

type AlphaDash struct {
    NoMatch
    Key string
}

AlphaDash check not Alpha

func (AlphaDash) DefaultMessage Uses

func (a AlphaDash) DefaultMessage() string

DefaultMessage return the default AlphaDash error message

func (AlphaDash) GetKey Uses

func (a AlphaDash) GetKey() string

GetKey return the n.Key

func (AlphaDash) GetLimitValue Uses

func (a AlphaDash) GetLimitValue() interface{}

GetLimitValue return the limit value

type AlphaNumeric Uses

type AlphaNumeric struct {
    Key string
}

AlphaNumeric check alpha and number

func (AlphaNumeric) DefaultMessage Uses

func (a AlphaNumeric) DefaultMessage() string

DefaultMessage return the default Length error message

func (AlphaNumeric) GetKey Uses

func (a AlphaNumeric) GetKey() string

GetKey return the a.Key

func (AlphaNumeric) GetLimitValue Uses

func (a AlphaNumeric) GetLimitValue() interface{}

GetLimitValue return the limit value

func (AlphaNumeric) IsSatisfied Uses

func (a AlphaNumeric) IsSatisfied(obj interface{}) bool

IsSatisfied judge whether obj is valid

type Base64 Uses

type Base64 struct {
    Match
    Key string
}

Base64 check struct

func (Base64) DefaultMessage Uses

func (b Base64) DefaultMessage() string

DefaultMessage return the default Base64 error message

func (Base64) GetKey Uses

func (b Base64) GetKey() string

GetKey return the b.Key

func (Base64) GetLimitValue Uses

func (b Base64) GetLimitValue() interface{}

GetLimitValue return the limit value

type CustomFunc Uses

type CustomFunc func(v *Validation, obj interface{}, key string)

CustomFunc is for custom validate function

type Email Uses

type Email struct {
    Match
    Key string
}

Email check struct

func (Email) DefaultMessage Uses

func (e Email) DefaultMessage() string

DefaultMessage return the default Email error message

func (Email) GetKey Uses

func (e Email) GetKey() string

GetKey return the n.Key

func (Email) GetLimitValue Uses

func (e Email) GetLimitValue() interface{}

GetLimitValue return the limit value

type Error Uses

type Error struct {
    Message, Key, Name, Field, Tmpl string
    Value                           interface{}
    LimitValue                      interface{}
}

Error show the error

func (*Error) Error Uses

func (e *Error) Error() string

Implement Error interface. Return e.String()

func (*Error) String Uses

func (e *Error) String() string

String Returns the Message.

type Funcs Uses

type Funcs map[string]reflect.Value

Funcs Validate function map

func (Funcs) Call Uses

func (f Funcs) Call(name string, params ...interface{}) (result []reflect.Value, err error)

Call validate values with named type string

type IP Uses

type IP struct {
    Match
    Key string
}

IP check struct

func (IP) DefaultMessage Uses

func (i IP) DefaultMessage() string

DefaultMessage return the default IP error message

func (IP) GetKey Uses

func (i IP) GetKey() string

GetKey return the i.Key

func (IP) GetLimitValue Uses

func (i IP) GetLimitValue() interface{}

GetLimitValue return the limit value

type Length Uses

type Length struct {
    N   int
    Key string
}

Length Requires an array or string to be exactly a given length.

func (Length) DefaultMessage Uses

func (l Length) DefaultMessage() string

DefaultMessage return the default Length error message

func (Length) GetKey Uses

func (l Length) GetKey() string

GetKey return the m.Key

func (Length) GetLimitValue Uses

func (l Length) GetLimitValue() interface{}

GetLimitValue return the limit value

func (Length) IsSatisfied Uses

func (l Length) IsSatisfied(obj interface{}) bool

IsSatisfied judge whether obj is valid

type Match Uses

type Match struct {
    Regexp *regexp.Regexp
    Key    string
}

Match Requires a string to match a given regex.

func (Match) DefaultMessage Uses

func (m Match) DefaultMessage() string

DefaultMessage return the default Match error message

func (Match) GetKey Uses

func (m Match) GetKey() string

GetKey return the m.Key

func (Match) GetLimitValue Uses

func (m Match) GetLimitValue() interface{}

GetLimitValue return the limit value

func (Match) IsSatisfied Uses

func (m Match) IsSatisfied(obj interface{}) bool

IsSatisfied judge whether obj is valid

type Max Uses

type Max struct {
    Max int
    Key string
}

Max validate struct

func (Max) DefaultMessage Uses

func (m Max) DefaultMessage() string

DefaultMessage return the default max error message

func (Max) GetKey Uses

func (m Max) GetKey() string

GetKey return the m.Key

func (Max) GetLimitValue Uses

func (m Max) GetLimitValue() interface{}

GetLimitValue return the limit value, Max

func (Max) IsSatisfied Uses

func (m Max) IsSatisfied(obj interface{}) bool

IsSatisfied judge whether obj is valid not support int64 on 32-bit platform

type MaxSize Uses

type MaxSize struct {
    Max int
    Key string
}

MaxSize Requires an array or string to be at most a given length.

func (MaxSize) DefaultMessage Uses

func (m MaxSize) DefaultMessage() string

DefaultMessage return the default MaxSize error message

func (MaxSize) GetKey Uses

func (m MaxSize) GetKey() string

GetKey return the m.Key

func (MaxSize) GetLimitValue Uses

func (m MaxSize) GetLimitValue() interface{}

GetLimitValue return the limit value

func (MaxSize) IsSatisfied Uses

func (m MaxSize) IsSatisfied(obj interface{}) bool

IsSatisfied judge whether obj is valid

type Min Uses

type Min struct {
    Min int
    Key string
}

Min check struct

func (Min) DefaultMessage Uses

func (m Min) DefaultMessage() string

DefaultMessage return the default min error message

func (Min) GetKey Uses

func (m Min) GetKey() string

GetKey return the m.Key

func (Min) GetLimitValue Uses

func (m Min) GetLimitValue() interface{}

GetLimitValue return the limit value, Min

func (Min) IsSatisfied Uses

func (m Min) IsSatisfied(obj interface{}) bool

IsSatisfied judge whether obj is valid not support int64 on 32-bit platform

type MinSize Uses

type MinSize struct {
    Min int
    Key string
}

MinSize Requires an array or string to be at least a given length.

func (MinSize) DefaultMessage Uses

func (m MinSize) DefaultMessage() string

DefaultMessage return the default MinSize error message

func (MinSize) GetKey Uses

func (m MinSize) GetKey() string

GetKey return the m.Key

func (MinSize) GetLimitValue Uses

func (m MinSize) GetLimitValue() interface{}

GetLimitValue return the limit value

func (MinSize) IsSatisfied Uses

func (m MinSize) IsSatisfied(obj interface{}) bool

IsSatisfied judge whether obj is valid

type Mobile Uses

type Mobile struct {
    Match
    Key string
}

Mobile check struct

func (Mobile) DefaultMessage Uses

func (m Mobile) DefaultMessage() string

DefaultMessage return the default Mobile error message

func (Mobile) GetKey Uses

func (m Mobile) GetKey() string

GetKey return the m.Key

func (Mobile) GetLimitValue Uses

func (m Mobile) GetLimitValue() interface{}

GetLimitValue return the limit value

type NoMatch Uses

type NoMatch struct {
    Match
    Key string
}

NoMatch Requires a string to not match a given regex.

func (NoMatch) DefaultMessage Uses

func (n NoMatch) DefaultMessage() string

DefaultMessage return the default NoMatch error message

func (NoMatch) GetKey Uses

func (n NoMatch) GetKey() string

GetKey return the n.Key

func (NoMatch) GetLimitValue Uses

func (n NoMatch) GetLimitValue() interface{}

GetLimitValue return the limit value

func (NoMatch) IsSatisfied Uses

func (n NoMatch) IsSatisfied(obj interface{}) bool

IsSatisfied judge whether obj is valid

type Numeric Uses

type Numeric struct {
    Key string
}

Numeric check number

func (Numeric) DefaultMessage Uses

func (n Numeric) DefaultMessage() string

DefaultMessage return the default Length error message

func (Numeric) GetKey Uses

func (n Numeric) GetKey() string

GetKey return the n.Key

func (Numeric) GetLimitValue Uses

func (n Numeric) GetLimitValue() interface{}

GetLimitValue return the limit value

func (Numeric) IsSatisfied Uses

func (n Numeric) IsSatisfied(obj interface{}) bool

IsSatisfied judge whether obj is valid

type Phone Uses

type Phone struct {
    Mobile
    Tel
    Key string
}

Phone just for chinese telephone or mobile phone number

func (Phone) DefaultMessage Uses

func (p Phone) DefaultMessage() string

DefaultMessage return the default Phone error message

func (Phone) GetKey Uses

func (p Phone) GetKey() string

GetKey return the p.Key

func (Phone) GetLimitValue Uses

func (p Phone) GetLimitValue() interface{}

GetLimitValue return the limit value

func (Phone) IsSatisfied Uses

func (p Phone) IsSatisfied(obj interface{}) bool

IsSatisfied judge whether obj is valid

type Range Uses

type Range struct {
    Min
    Max
    Key string
}

Range Requires an integer to be within Min, Max inclusive.

func (Range) DefaultMessage Uses

func (r Range) DefaultMessage() string

DefaultMessage return the default Range error message

func (Range) GetKey Uses

func (r Range) GetKey() string

GetKey return the m.Key

func (Range) GetLimitValue Uses

func (r Range) GetLimitValue() interface{}

GetLimitValue return the limit value, Max

func (Range) IsSatisfied Uses

func (r Range) IsSatisfied(obj interface{}) bool

IsSatisfied judge whether obj is valid not support int64 on 32-bit platform

type Required Uses

type Required struct {
    Key string
}

Required struct

func (Required) DefaultMessage Uses

func (r Required) DefaultMessage() string

DefaultMessage return the default error message

func (Required) GetKey Uses

func (r Required) GetKey() string

GetKey return the r.Key

func (Required) GetLimitValue Uses

func (r Required) GetLimitValue() interface{}

GetLimitValue return nil now

func (Required) IsSatisfied Uses

func (r Required) IsSatisfied(obj interface{}) bool

IsSatisfied judge whether obj has value

type Result Uses

type Result struct {
    Error *Error
    Ok    bool
}

Result is returned from every validation method. It provides an indication of success, and a pointer to the Error (if any).

func (*Result) Key Uses

func (r *Result) Key(key string) *Result

Key Get Result by given key string.

func (*Result) Message Uses

func (r *Result) Message(message string, args ...interface{}) *Result

Message Set Result message by string or format string with args

type Tel Uses

type Tel struct {
    Match
    Key string
}

Tel check telephone struct

func (Tel) DefaultMessage Uses

func (t Tel) DefaultMessage() string

DefaultMessage return the default Tel error message

func (Tel) GetKey Uses

func (t Tel) GetKey() string

GetKey return the t.Key

func (Tel) GetLimitValue Uses

func (t Tel) GetLimitValue() interface{}

GetLimitValue return the limit value

type ValidFormer Uses

type ValidFormer interface {
    Valid(*Validation)
}

ValidFormer valid interface

type ValidFunc Uses

type ValidFunc struct {
    Name   string
    Params []interface{}
}

ValidFunc Valid function type

type Validation Uses

type Validation struct {
    // if this field set true, in struct tag valid
    // if the struct field vale is empty
    // it will skip those valid functions, see CanSkipFuncs
    RequiredFirst bool

    Errors    []*Error
    ErrorsMap map[string][]*Error
}

A Validation context manages data validation and error messages.

func (*Validation) AddError Uses

func (v *Validation) AddError(key, message string)

AddError adds independent error message for the provided key

func (*Validation) Alpha Uses

func (v *Validation) Alpha(obj interface{}, key string) *Result

Alpha Test that the obj is [a-zA-Z] if type is string

func (*Validation) AlphaDash Uses

func (v *Validation) AlphaDash(obj interface{}, key string) *Result

AlphaDash Test that the obj is [0-9a-zA-Z_-] if type is string

func (*Validation) AlphaNumeric Uses

func (v *Validation) AlphaNumeric(obj interface{}, key string) *Result

AlphaNumeric Test that the obj is [0-9a-zA-Z] if type is string

func (*Validation) Base64 Uses

func (v *Validation) Base64(obj interface{}, key string) *Result

Base64 Test that the obj is base64 encoded if type is string

func (*Validation) CanSkipAlso Uses

func (v *Validation) CanSkipAlso(skipFunc string)

func (*Validation) Check Uses

func (v *Validation) Check(obj interface{}, checks ...Validator) *Result

Check 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) Clear Uses

func (v *Validation) Clear()

Clear Clean all ValidationError.

func (*Validation) Email Uses

func (v *Validation) Email(obj interface{}, key string) *Result

Email Test that the obj is email address if type is string

func (*Validation) Error Uses

func (v *Validation) Error(message string, args ...interface{}) *Result

Error Add an error to the validation context.

func (*Validation) ErrorMap Uses

func (v *Validation) ErrorMap() map[string][]*Error

ErrorMap 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) HasErrors Uses

func (v *Validation) HasErrors() bool

HasErrors Has ValidationError nor not.

func (*Validation) IP Uses

func (v *Validation) IP(obj interface{}, key string) *Result

IP Test that the obj is IP address if type is string

func (*Validation) Length Uses

func (v *Validation) Length(obj interface{}, n int, key string) *Result

Length Test that the obj is same length to n if type is string or slice

func (*Validation) Match Uses

func (v *Validation) Match(obj interface{}, regex *regexp.Regexp, key string) *Result

Match Test that the obj matches regexp if type is string

func (*Validation) Max Uses

func (v *Validation) Max(obj interface{}, max int, key string) *Result

Max Test that the obj is less than max if obj's type is int

func (*Validation) MaxSize Uses

func (v *Validation) MaxSize(obj interface{}, max int, key string) *Result

MaxSize Test that the obj is shorter than max size if type is string or slice

func (*Validation) Min Uses

func (v *Validation) Min(obj interface{}, min int, key string) *Result

Min Test that the obj is greater than min if obj's type is int

func (*Validation) MinSize Uses

func (v *Validation) MinSize(obj interface{}, min int, key string) *Result

MinSize Test that the obj is longer than min size if type is string or slice

func (*Validation) Mobile Uses

func (v *Validation) Mobile(obj interface{}, key string) *Result

Mobile Test that the obj is chinese mobile number if type is string

func (*Validation) NoMatch Uses

func (v *Validation) NoMatch(obj interface{}, regex *regexp.Regexp, key string) *Result

NoMatch Test that the obj doesn't match regexp if type is string

func (*Validation) Numeric Uses

func (v *Validation) Numeric(obj interface{}, key string) *Result

Numeric Test that the obj is [0-9] if type is string

func (*Validation) Phone Uses

func (v *Validation) Phone(obj interface{}, key string) *Result

Phone Test that the obj is chinese mobile or telephone number if type is string

func (*Validation) Range Uses

func (v *Validation) Range(obj interface{}, min, max int, key string) *Result

Range Test that the obj is between mni and max if obj's type is int

func (*Validation) RecursiveValid Uses

func (v *Validation) RecursiveValid(objc interface{}) (bool, error)

RecursiveValid Recursively validate a struct. Step1: Validate by v.Valid Step2: If pass on step1, then reflect obj's fields Step3: Do the Recursively validation to all struct or struct pointer fields

func (*Validation) Required Uses

func (v *Validation) Required(obj interface{}, key string) *Result

Required Test that the argument is non-nil and non-empty (if string or list)

func (*Validation) SetError Uses

func (v *Validation) SetError(fieldName string, errMsg string) *Error

SetError Set error message for one field in ValidationError

func (*Validation) Tel Uses

func (v *Validation) Tel(obj interface{}, key string) *Result

Tel Test that the obj is chinese telephone number if type is string

func (*Validation) Valid Uses

func (v *Validation) Valid(obj interface{}) (b bool, err error)

Valid Validate a struct. the obj parameter must be a struct or a struct pointer

func (*Validation) ZipCode Uses

func (v *Validation) ZipCode(obj interface{}, key string) *Result

ZipCode Test that the obj is chinese zip code if type is string

type Validator Uses

type Validator interface {
    IsSatisfied(interface{}) bool
    DefaultMessage() string
    GetKey() string
    GetLimitValue() interface{}
}

Validator interface

type ZipCode Uses

type ZipCode struct {
    Match
    Key string
}

ZipCode check the zip struct

func (ZipCode) DefaultMessage Uses

func (z ZipCode) DefaultMessage() string

DefaultMessage return the default Zip error message

func (ZipCode) GetKey Uses

func (z ZipCode) GetKey() string

GetKey return the z.Key

func (ZipCode) GetLimitValue Uses

func (z ZipCode) GetLimitValue() interface{}

GetLimitValue return the limit value

Package validation imports 7 packages (graph) and is imported by 282 packages. Updated 2019-10-23. Refresh now. Tools for package owners.