-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators_test.go
78 lines (59 loc) · 1.21 KB
/
validators_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
package model_test
import (
"testing"
"github.com/cheekybits/is"
"github.com/cheekybits/model"
)
func TestStrict(t *testing.T) {
is := is.New(t)
m := model.M{
"name": {},
"number": {},
}.Before(model.Strict)
d := map[string]interface{}{
"name": "Mat",
"number": 123,
"extra": true,
}
_, errs := m.Do(d)
is.Equal(len(errs), 1)
is.Equal(errs["model"][0].Error(), "unexpected 'extra'")
}
func TestIsNumber(t *testing.T) {
is := is.New(t)
m := model.M{
"number": {model.IsNumber},
}
d := map[string]interface{}{
"number": 123,
}
_, errs := m.Do(d)
is.Equal(len(errs), 0)
d = map[string]interface{}{
"number": "123",
}
_, errs = m.Do(d)
is.Equal(len(errs), 1)
is.Equal(errs["number"][0].Error(), "must be a number")
}
func TestIsRequired(t *testing.T) {
is := is.New(t)
m := model.M{
"number": {model.IsRequired},
}
d := map[string]interface{}{
"number": 123,
}
_, errs := m.Do(d)
is.Equal(len(errs), 0)
d = map[string]interface{}{
"number": nil,
}
_, errs = m.Do(d)
is.Equal(len(errs), 1)
is.Equal(errs["number"][0].Error(), "is required")
d = map[string]interface{}{}
_, errs = m.Do(d)
is.Equal(len(errs), 1)
is.Equal(errs["number"][0].Error(), "is required")
}