mirror of https://github.com/gogits/gogs.git
Unknown
11 years ago
10 changed files with 308 additions and 227 deletions
@ -1,16 +1,103 @@ |
|||||||
gopm - Go Package Manager |
validation |
||||||
========================= |
============== |
||||||
|
|
||||||
![GPMGo_Logo](https://raw.github.com/gpmgo/gopmweb/master/static/img/gpmgo.png?raw=true) |
validation is a form validation for a data validation and error collecting using Go. |
||||||
|
|
||||||
gopm(Go Package Manager) is a Go package manage tool for search, install, update and share packages in Go. |
## Installation and tests |
||||||
|
|
||||||
**Attention** This application still in experiment, we'are working on new break version, you may use [old version](https://github.com/gpmgo/gopm/tree/v0.1.0) for now. |
Install: |
||||||
|
|
||||||
## Credits |
go get github.com/astaxie/beego/validation |
||||||
|
|
||||||
- [Go Walker](https://github.com/Unknwon/gowalker) |
Test: |
||||||
|
|
||||||
## License |
go test github.com/astaxie/beego/validation |
||||||
|
|
||||||
[Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html). |
## 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/ |
||||||
|
@ -1,9 +0,0 @@ |
|||||||
{ |
|
||||||
"app_name": "Go Package Manager", |
|
||||||
"http_port": 23456, |
|
||||||
"watch_enabled": true, |
|
||||||
"cmd_mode": true, |
|
||||||
"skip_suspend": false, |
|
||||||
"print_stack": true, |
|
||||||
"print_source": true |
|
||||||
} |
|
Loading…
Reference in new issue