-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_test.go
87 lines (73 loc) · 1.44 KB
/
validate_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package echo
import (
"reflect"
"testing"
"github.com/labstack/echo/v4"
. "github.com/smartystreets/goconvey/convey"
)
func someValidatorFunc(fl FieldLevel) bool {
field := fl.Field()
switch field.Kind() {
case reflect.String:
return field.String() == "true"
default:
return false
}
}
type someValidStruct struct {
A string `validate:"required,some" message:"some error"`
}
type someInvalidStruct struct {
A int `validate:"required,some" message:"some error"`
}
type someSimpleStruct struct {
A int
}
var cases = []struct {
item interface{}
error error
}{
{
item: &someValidStruct{A: "true"},
error: nil,
},
{
item: new(someSimpleStruct),
error: nil,
},
{
item: &someValidStruct{A: "false"},
error: echo.NewHTTPError(400, "`a` some error"),
},
{
item: &someInvalidStruct{A: 1},
error: echo.NewHTTPError(400, "`a` some error"),
},
}
func TestValidate_Register(t *testing.T) {
Convey("Validator", t, func(c C) {
var (
v = NewValidator()
err = v.Register("some", someValidatorFunc)
)
c.So(err, ShouldBeNil)
c.Convey("try cases", func(c C) {
for _, test := range cases {
err = v.Validate(test.item)
switch test.error {
case nil:
c.So(err, ShouldBeNil)
default:
if ok, vErr := CheckErrors(ValidateParams{
Struct: test.item,
Errors: err,
}); ok {
c.So(vErr, ShouldBeError, test.error)
} else {
t.FailNow()
}
}
}
})
})
}