ddb9ed39 by miraclesu

add validation README

1 parent a242f61b
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 import (
19 "github.com/astaxie/beego/validation"
20 "log"
21 )
22
23 type User struct {
24 Name string
25 Age int
26 }
27
28 func main() {
29 u := User{"man", 40}
30 valid := validation.Validation{}
31 valid.Required(u.Name, "name")
32 valid.MaxSize(u.Name, 15, "nameMax")
33 valid.Range(u.Age, 0, 140, "age")
34 if valid.HasErrors {
35 // validation does not pass
36 // print invalid message
37 for _, err := range valid.Errors {
38 log.Println(err.Key, err.Message)
39 }
40 }
41 // or use like this
42 if v := valid.Max(u.Age, 140); !v.Ok {
43 log.Println(v.Error.Key, v.Error.Message)
44 }
45 }
46
47
48 ## LICENSE
49
50 BSD License http://creativecommons.org/licenses/BSD/
...@@ -31,7 +31,7 @@ func TestRequired(t *testing.T) { ...@@ -31,7 +31,7 @@ func TestRequired(t *testing.T) {
31 t.Error("empty slice should be false") 31 t.Error("empty slice should be false")
32 } 32 }
33 if !valid.Required([]interface{}{"ok"}, "slice").Ok { 33 if !valid.Required([]interface{}{"ok"}, "slice").Ok {
34 t.Error("slice should be equal true") 34 t.Error("slice should be true")
35 } 35 }
36 } 36 }
37 37
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!