mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 12:50:55 +00:00
add validation README
This commit is contained in:
parent
a242f61b8e
commit
ddb9ed39a5
50
validation/README.md
Normal file
50
validation/README.md
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
## LICENSE
|
||||||
|
|
||||||
|
BSD License http://creativecommons.org/licenses/BSD/
|
@ -31,7 +31,7 @@ func TestRequired(t *testing.T) {
|
|||||||
t.Error("empty slice should be false")
|
t.Error("empty slice should be false")
|
||||||
}
|
}
|
||||||
if !valid.Required([]interface{}{"ok"}, "slice").Ok {
|
if !valid.Required([]interface{}{"ok"}, "slice").Ok {
|
||||||
t.Error("slice should be equal true")
|
t.Error("slice should be true")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user