-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator_test.go
executable file
·137 lines (118 loc) · 3.13 KB
/
validator_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package validator
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/x88/null"
)
func Test_Null(t *testing.T) {
structEmpty := null.Int{}
structNotEmpty := null.IntFrom(42)
assert.NotNil(t, notEmpty(structEmpty, ``))
assert.Nil(t, notEmpty(structNotEmpty, ``))
}
func TestValidator_Validate(t *testing.T) {
testStruct := struct {
Min int `validate:"min=3"`
Max int `validate:"max=0"`
Empty int `validate:"empty=''"`
In int `validate:"in='2,3,4,5'"`
Type string `validate:"type=base64"`
CustomMsg int `validate:"min=3,msg_min=msg1{param}msg2"`
CustomAlias int `validate:"min=3,attr=custom_alias"`
}{
Min: 1,
Max: 1,
Empty: 0,
In: 1,
Type: "test_string",
CustomMsg: 1,
CustomAlias: 1,
}
errs := Validate(testStruct)
assert.Equal(t, false, errs.IsEmpty())
assert.Equal(t, ErrMin, errs["Min"])
assert.Equal(t, ErrMax, errs["Max"])
assert.Equal(t, ErrZeroValue, errs["Empty"])
assert.Equal(t, ErrInvalidValue, errs["In"])
assert.Equal(t, ErrInvalidTypedValue, errs["Type"])
assert.Equal(t, "msg13msg2", errs["CustomMsg"].Error())
assert.Equal(t, ErrMin, errs["custom_alias"])
}
func TestValidator_ParseTags(t *testing.T) {
data := []struct {
str string
tags tagList
}{
{"name=value", []tag{{"name", "value"}}},
{"under_score=value", []tag{{"under_score", "value"}}},
{"name1=value1,name2=value2",
[]tag{{"name1", "value1"}, {"name2", "value2"}}},
{"quoted='value'", []tag{{"quoted", "value"}}},
// value containing commas must be single-quoted
{"quoted='v,a,l,u,e'", []tag{{"quoted", "v,a,l,u,e"}}},
{"quoted='v,a,l,u,e',name1=val1",
[]tag{{"quoted", "v,a,l,u,e"}, {"name1", "val1"}}},
}
validator := &Validator{}
for _, cs := range data {
tags, err := validator.parseTags(cs.str)
assert.Nil(t, err, cs.str)
assert.Equal(t, cs.tags, tags, cs.str)
}
}
func TestIn(t *testing.T) {
data := []struct {
v interface{}
param string
err error
}{
// int
{1, "2,3,4", ErrInvalidValue},
{1, "1,2,3", nil},
// float
{1.1, "2.2,3,4", ErrInvalidValue},
{1.1, "1.1,2.3,3", nil},
// string
{"str1", "str2,str3,str4", ErrInvalidValue},
{"str1", "str1,str2,str3", nil},
// invalid parameters
{1.1, "2.2,3,4,not_float", ErrBadParameter},
}
for _, row := range data {
err := in(row.v, row.param)
assert.Equal(t, row.err, err, fmt.Sprintf("%#v", row))
}
}
func TestTypeValid(t *testing.T) {
data := []struct {
v interface{}
param string
err error
}{
{"not_base64", "base64", ErrInvalidTypedValue},
{"dGVzdA==", "base64", nil},
{"not_timestamp", "timestamp", ErrInvalidTypedValue},
{"2008-09-08T22:47:31-07:00", "timestamp", nil},
}
for _, row := range data {
err := typeValid(row.v, row.param)
assert.Equal(t, row.err, err, fmt.Sprintf("%#v", row))
}
}
func Example() {
testStruct := struct {
Min int `validate:"min=3"`
In string `validate:"attr=in_field,in='2,3,4,5',msg_in='not one of {param}'"`
}{
Min: 1,
In: "1",
}
errs := Validate(testStruct)
if !errs.IsEmpty() {
fmt.Println(errs["Min"])
fmt.Println(errs["in_field"])
}
// Output: less than min
// not one of 2,3,4,5
}