github.com/astaxie/beego@v1.12.3/validation/README.md (about)

     1  validation
     2  ==============
     3  
     4  validation is a form validation for a data validation and error collecting using Go.
     5  
     6  ## Installation and tests
     7  
     8  Install:
     9  
    10  	go get github.com/astaxie/beego/validation
    11  
    12  Test:
    13  
    14  	go test github.com/astaxie/beego/validation
    15  
    16  ## Example
    17  
    18  Direct Use:
    19  
    20  	import (
    21  		"github.com/astaxie/beego/validation"
    22  		"log"
    23  	)
    24  
    25  	type User struct {
    26  		Name string
    27  		Age int
    28  	}
    29  
    30  	func main() {
    31  		u := User{"man", 40}
    32  		valid := validation.Validation{}
    33  		valid.Required(u.Name, "name")
    34  		valid.MaxSize(u.Name, 15, "nameMax")
    35  		valid.Range(u.Age, 0, 140, "age")
    36  		if valid.HasErrors() {
    37  			// validation does not pass
    38  			// print invalid message
    39  			for _, err := range valid.Errors {
    40  				log.Println(err.Key, err.Message)
    41  			}
    42  		}
    43  		// or use like this
    44  		if v := valid.Max(u.Age, 140, "ageMax"); !v.Ok {
    45  			log.Println(v.Error.Key, v.Error.Message)
    46  		}
    47  	}
    48  
    49  Struct Tag Use:
    50  
    51  	import (
    52  		"github.com/astaxie/beego/validation"
    53  	)
    54  
    55  	// validation function follow with "valid" tag
    56  	// functions divide with ";"
    57  	// parameters in parentheses "()" and divide with ","
    58  	// Match function's pattern string must in "//"
    59  	type user struct {
    60  		Id   int
    61  		Name string `valid:"Required;Match(/^(test)?\\w*@;com$/)"`
    62  		Age  int    `valid:"Required;Range(1, 140)"`
    63  	}
    64  
    65  	func main() {
    66  		valid := validation.Validation{}
    67  		// ignore empty field valid
    68  		// see CanSkipFuncs
    69  		// valid := validation.Validation{RequiredFirst:true}
    70  		u := user{Name: "test", Age: 40}
    71  		b, err := valid.Valid(u)
    72  		if err != nil {
    73  			// handle error
    74  		}
    75  		if !b {
    76  			// validation does not pass
    77  			// blabla...
    78  		}
    79  	}
    80  
    81  Use custom function:
    82  
    83  	import (
    84  		"github.com/astaxie/beego/validation"
    85  	)
    86  
    87  	type user struct {
    88  		Id   int
    89  		Name string `valid:"Required;IsMe"`
    90  		Age  int    `valid:"Required;Range(1, 140)"`
    91  	}
    92  
    93  	func IsMe(v *validation.Validation, obj interface{}, key string) {
    94  		name, ok:= obj.(string)
    95  		if !ok {
    96  			// wrong use case?
    97  			return
    98  		}
    99  
   100  		if name != "me" {
   101  			// valid false
   102  			v.SetError("Name", "is not me!")
   103  		}
   104  	}
   105  
   106  	func main() {
   107  		valid := validation.Validation{}
   108  		if err := validation.AddCustomFunc("IsMe", IsMe); err != nil {
   109  			// hadle error
   110  		}
   111  		u := user{Name: "test", Age: 40}
   112  		b, err := valid.Valid(u)
   113  		if err != nil {
   114  			// handle error
   115  		}
   116  		if !b {
   117  			// validation does not pass
   118  			// blabla...
   119  		}
   120  	}
   121  
   122  Struct Tag Functions:
   123  
   124  	Required
   125  	Min(min int)
   126  	Max(max int)
   127  	Range(min, max int)
   128  	MinSize(min int)
   129  	MaxSize(max int)
   130  	Length(length int)
   131  	Alpha
   132  	Numeric
   133  	AlphaNumeric
   134  	Match(pattern string)
   135  	AlphaDash
   136  	Email
   137  	IP
   138  	Base64
   139  	Mobile
   140  	Tel
   141  	Phone
   142  	ZipCode
   143  
   144  
   145  ## LICENSE
   146  
   147  BSD License http://creativecommons.org/licenses/BSD/